/*
 * @Author: zhanyoulin<zhanyoulin456@163.com>
 * @Date: 2023-06-23 11:04:27
 * @LastEditors: zengzhe
 * @LastEditTime: 2024-12-27 15:27:57
 * @Description:
 */
import path from 'path'

export function isDevFn(mode: string): boolean {
  return mode === 'development'
}

export function isProdFn(mode: string): boolean {
  return mode === 'production'
}

export function isTestFn(mode: string): boolean {
  return mode === 'test'
}

/**
 * Whether to generate package preview
 */
export function isReportMode(): boolean {
  return process.env.PUBLIC_REPORT === 'true'
}

// Read all environment variable configuration files to process.env
export function wrapperEnv(envConf: Recordable): Recordable {
  const ret: Recordable = {}

  for (const envName of Object.keys(envConf)) {
    // 只处理 process.env 开头的配置
    if (envName.startsWith('process.env.')) {
      // 移除 process.env. 前缀
      const key = envName.replace('process.env.', '')
      // 移除值两端的引号
      let value = envConf[envName].replace(/^["'](.+)["']$/, '$1')

      // 转换特定类型的值
      value = value === 'true' ? true : value === 'false' ? false : value

      if (value === 'null') {
        value = null
      }

      if (!isNaN(Number(value))) {
        value = Number(value)
      }

      ret[key] = value
    }
  }
  return ret
}

/**
 * Get user root directory
 * @param dir file path
 */
export function getRootPath(...dir: string[]) {
  return path.resolve(process.cwd(), ...dir)
}
