import { createViteConfig } from '@vtj/cli';
import { createDevTools } from '@vtj/local';
import AutoImport from 'unplugin-auto-import/vite';
import { defineConfig, loadEnv } from 'vite';
import mkcert from 'vite-plugin-mkcert';

export default defineConfig(({ mode }) => {
  console.log('mode', mode);
  const env = loadEnv(mode, process.cwd(), ['VITE_']);
  const isDev = env.VITE_NODE_ENV === 'development';

  const config = createViteConfig({
    // proxy,
    plugins: [
      createDevTools(),
      isDev && mkcert(),
      AutoImport({
        imports: [
          'vue',
          'pinia',
          'vue-router',
          {
            from: 'pinia',
            imports: ['storeToRefs', 'defineStore'],
            type: true
          }
        ],
        dts: 'src/auto-imports.d.ts', // 生成类型声明文件
        eslintrc: {
          enabled: true // 生成 eslint 配置
        }
      })
    ]
  });
  return {
    ...config,
    server: {
      cors: true,
      https: true,
      port: isDev ? Number(env.VITE_PORT) : undefined,
      host: true
    },
    define: {
      // 注入环境变量到客户端
      __APP_ENV__: JSON.stringify(env)
    }
  };
});
