import { defineConfig, loadEnv } from 'vite'
import Uni from '@dcloudio/vite-plugin-uni'
import UniHelperManifest from '@uni-helper/vite-plugin-uni-manifest'
import UniHelperPages from '@uni-helper/vite-plugin-uni-pages'
import UniHelperLayouts from '@uni-helper/vite-plugin-uni-layouts'
import UniHelperComponents from '@uni-helper/vite-plugin-uni-components'
import AutoImport from 'unplugin-auto-import/vite'
import { WotResolver } from '@uni-helper/vite-plugin-uni-components/resolvers'

// https://vitejs.dev/config/
export default async ({ mode }) => {
  // 加载环境变量
  const env = loadEnv(mode, process.cwd())
  const UnoCSS = (await import('unocss/vite')).default

  return defineConfig({
    plugins: [
      UniHelperManifest(),
      UniHelperPages({
        dts: 'src/uni-pages.d.ts',
      }),
      UniHelperLayouts(),
      UniHelperComponents({
        dts: 'src/components.d.ts',
        directoryAsNamespace: true,
        resolvers: [WotResolver()],
      }),
      Uni(),
      AutoImport({
        imports: ['vue', '@vueuse/core', 'uni-app'],
        dts: 'src/auto-imports.d.ts',
        dirs: ['src/composables', 'src/stores', 'src/utils'],
        vueTemplate: true,
      }),
      UnoCSS(),
    ],
    server: {
      proxy: {
        '/api/v1': {
          target: env.VITE_APP_BASE_URL || 'https://www.quannengcha.com', // 使用环境变量
          changeOrigin: true,
        },
      },
    },
    // 定义环境变量类型
    define: {
      'process.env': env,
    }
  })
}
