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 envDir = './.env';
  const env = loadEnv(configEnv.mode, envDir, 'REQUEST_');
  return {
    envDir,
    envPrefix: 'SIX_',
    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: {
        '/fdhb-tablet': {
          target: env.REQUEST_API_PROXY_URL,
          secure: false,
          changeOrigin: false,
          logLevel: 'debug',
        },
        '/file': {
          target: env.REQUEST_API_PROXY_URL,
          secure: false,
          changeOrigin: false,
          logLevel: 'debug',
        },
      },
    },
  };
});
