import {
  defineConfig,
  loadEnv,
  type ProxyOptions,
} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import VueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import processHtml from './plugin/html-process.ts'
import zipDist from './plugin/zip-dist.ts'
import path from 'node:path'
import ElementPlus from 'unplugin-element-plus/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { fileWatcher } from './plugin/file-watcher.ts'
import IconfontProcess from './plugin/iconfont-process.ts'
import ColorProcess from './plugin/color-process.ts'

// https://vite.dev/config/
export default defineConfig((configEnv) => {
  const env = loadEnv(configEnv.mode, process.cwd(), '')
  return {
    base: env.VITE_APP_BASE_URL,
    build: {
      rollupOptions: {
        treeshake: true,
      },
    },
    css: {
      modules: {
        localsConvention: 'camelCase',
      },
    },
    plugins: [
      vue(),
      vueJsx(),
      VueDevTools(),
      ElementPlus({}),
      AutoImport({
        imports: [ 'vue', 'vue-router', 'pinia' ],
        dts: './src/dts/auto-imports.d.ts',
        resolvers: [ ElementPlusResolver() ],
      }),
      Components({
        dts: './src/dts/components.d.ts',
        dirs: [ './src/widgets' ],
        resolvers: [ ElementPlusResolver() ],
      }),
      processHtml(env.VITE_APP_NAME, '<script src="https://map.qq.com/api/gljs?v=1.exp&key=3TGBZ-ZK7K5-CMNIK-ICIGZ-K6TYQ-HTBTZ"></script>'),
      fileWatcher(IconfontProcess, ColorProcess),
      zipDist(),
    ],
    resolve: {
      extensions: [ '.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json' ],
      alias: [
        {
          find: '@',
          replacement: path.resolve(__dirname, 'src'),
        },
      ],
    },
    server: {
      host: '0.0.0.0',
      port: 5173,
      proxy: {
        [env.VITE_HTTP_MAP_SERVER_BASE_URL]: {
          proxyTimeout: 10000,
          target: env.VITE_HTTP_MAP_SERVER_PROXY_TARGET,
          secure: false,
          changeOrigin: true,
          rewrite: (path) => (env.VITE_HTTP_MAP_SERVER_BASE_URL == null || env.VITE_HTTP_MAP_SERVER_BASE_URL == '/' ? path : path.replace(new RegExp(env.VITE_HTTP_MAP_SERVER_BASE_URL), '')),
        } as ProxyOptions,
        [env.VITE_HTTP_SERVER_BASE_URL]: {
          proxyTimeout: 10000,
          target: env.VITE_HTTP_PROXY_TARGET,
          secure: false,
          changeOrigin: true,
          rewrite: (path) => (env.VITE_HTTP_SERVER_BASE_URL == null || env.VITE_HTTP_SERVER_BASE_URL == '/' ? path : path.replace(new RegExp(env.VITE_HTTP_SERVER_BASE_URL), '')),
        } as ProxyOptions,
        [env.VITE_WS_SERVER_BASE_URL]: {
          proxyTimeout: 10000,
          ws: true,
          secure: false,
          changeOrigin: true,
          target: env.VITE_WS_PROXY_TARGET,
          rewrite: (path) => (env.VITE_WS_SERVER_BASE_URL == null || env.VITE_WS_SERVER_BASE_URL == '/' ? path : path.replace(new RegExp(env.VITE_WS_SERVER_BASE_URL), '')),
        } as ProxyOptions,
      },
    },
  }
})
