import { URL, fileURLToPath } from 'node:url'
import { type ConfigEnv, defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'

// https://vitejs.dev/config/
export default ({ mode }: ConfigEnv) => {
  const env = loadEnv(mode, './env', 'LEAP')

  const {
    LEAP_PROXY_CONTEXT,
    LEAP_PROXY_HOST,
    LEAP_ROUTER_TYPE,
  } = env

  return defineConfig({
    base: LEAP_ROUTER_TYPE === 'hash' ? './' : '/',
    envDir: './env',
    publicDir: 'src/assets',
    envPrefix: ['LEAP_', 'APP_'],
    build: {
      manifest: true,
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
        /** Delete below */
        '@ldesign/desktop-public-next': fileURLToPath(new URL('../public/src', import.meta.url)),
        '@ldesign/desktop-next/es': fileURLToPath(new URL('../basic/src', import.meta.url)),
        '@ldesign/desktop-next': fileURLToPath(new URL('../basic/src', import.meta.url)),
        '@ldesign/desktop-profession-next': fileURLToPath(new URL('../profession/src', import.meta.url)),
        /** End */
      },
    },
    server: {
      host: true,
      port: 17100,
      open: false,
      proxy: {
        [`^/${LEAP_PROXY_CONTEXT}/.*`]: {
          target: LEAP_PROXY_HOST,
          changeOrigin: false,
          rewrite: path => path.replace(/^\//, ''),
        },
      },
    },
    plugins: [
      vue(),
      vueJsx(),
    ],
  })
}
