import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import path from 'node:path'
import AutoImport from 'unplugin-auto-import/vite'
import Icons from 'unplugin-icons/vite'
import { IduxResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import {
  defineConfig,
  loadEnv,
  ProxyOptions,
  UserConfigFnObject
} from 'vite'
import VueDevTools from 'vite-plugin-vue-devtools'
import processHtml from './plugin/html-process'
import zipDist from './plugin/zip-dist'
import { viteStaticCopy } from 'vite-plugin-static-copy'
import { fileWatcher } from './plugin/file-watcher'
import iconfontProcess from './plugin/iconfont-process'

let viteConfig: UserConfigFnObject = 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(),
      AutoImport({
        imports: [ 'vue', 'vue-router', 'pinia' ],
        dts: './src/dts/auto-imports.d.ts',
        resolvers: [ IduxResolver() ],
      }),
      Components({
        dts: './src/dts/components.d.ts',
        resolvers: [ IduxResolver() ],
      }),
      processHtml(env.VITE_APP_NAME),
      zipDist(),
      Icons(),
      fileWatcher({
        file: './public/iconfont/ali/iconfont.json',
        fn: iconfontProcess('./src/components/iconfont'),
      }),
      viteStaticCopy({
        targets: [
          {
            src: './node_modules/@idux/components/icon/assets/*.svg',
            dest: 'idux-icons',
          },
        ],
      }),
      /* visualizer({
       gzipSize: true,
       brotliSize: true,
       emitFile: false,
       filename: "dist/visualizer.html",
       open:true
       }), */
    ],
    resolve: {
      extensions: [ '.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json', '.vue' ],
      alias: [
        /*         {
         find: 'vue',
         replacement: 'vue/dist/vue.esm-bundler.js',
         }, */
        {
          find: '@',
          replacement: path.resolve(__dirname, 'src'),
        },
      ],
    },
    server: {
      host: '0.0.0.0',
      port: 80,
      proxy: {
        /*         [env.VITE_OSS_UPLOAD_BASE_URL]: {
          proxyTimeout: 10000,
         target: env.VITE_OSS_UPLOAD_PROXY_TARGET,
         // target: 'http://cdn-zsy.oss-cn-shanghai.aliyuncs.com',
         rewrite: path => {
         const s = env.VITE_OSS_UPLOAD_BASE_URL == null || env.VITE_OSS_UPLOAD_BASE_URL == '/' ? path : path.replace(new RegExp(env.VITE_OSS_UPLOAD_BASE_URL), '')
         console.log('-----------', '[', s, ']', path)
         return s
         },
         } as ProxyOptions, */
        [env.VITE_HTTP_SERVER_BASE_URL]: {
          proxyTimeout: 10000,
          target: env.VITE_HTTP_PROXY_TARGET,
          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]: {
          ws: 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_HTTP_SERVER_BASE_URL), ''),
        } as ProxyOptions,
      },
    },
  }
}

export default defineConfig(viteConfig)
