import { VantResolver }       from '@vant/auto-import-resolver';
import vue                    from '@vitejs/plugin-vue';
import vueJsx                 from '@vitejs/plugin-vue-jsx';
import { fileURLToPath, URL } from 'node:url';

import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';

import { defineConfig, loadEnv } from 'vite';
import vueDevTools               from 'vite-plugin-vue-devtools';

// https://vitejs.dev/config/
export default defineConfig((configEnv) => {
  const argv = process.argv
  const envDir = './.env';
  const env = loadEnv(configEnv.mode, envDir, 'REQUEST_');
  return {
    envDir,
    envPrefix: 'SIX_',
    define: {
      __APP_VERSION__: JSON.stringify(process.env.npm_package_version),
      __FORBID_AUTO_PROCESS_PULSE_AGENCY__: argv.includes('--legacy-pulse-agency')
    },
    css: {
      preprocessorOptions: {
        scss: {
          api: 'modern-compiler',
          importers: [],
        },
      },
    },
    plugins: [
      vue(),
      vueJsx(),
      vueDevTools(),
      AutoImport({
        dts: '@types/auto-imports.d.ts',
        imports: [ 'vue', 'vue-router', 'pinia' ],
        resolvers: [ VantResolver() ],
      }),
      Components({
        dts: '@types/components.d.ts',
        resolvers: [ VantResolver() ],
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
    server: {
      host: true,
      open: true,
      port: 55555,
      proxy: {
        '/manager': {
          target: env.REQUEST_API_PROXY_URL,
          secure: false,
          changeOrigin: true,
          logLevel: 'debug',
        },
      },
    },
  };
});
