import { defineConfig, loadEnv, type ConfigEnv, type UserConfig } from 'vite'
import { initVitePlugins } from './plugins'
import { initProxy } from './plugins/proxy'
import { resolve } from 'path'
import { wrapperEnv } from './getEnv'
const pathSrc = resolve(__dirname, 'src')
// https://vite.dev/config/
export default defineConfig(({ mode, command }: ConfigEnv): UserConfig => {
  const root = process.cwd()
  const env = loadEnv(mode, root)
  const viteEnv = wrapperEnv(env)
  return {
    server: {
      port: viteEnv.VITE_PORT,
      open: viteEnv.VITE_OPEN,
      proxy: initProxy(viteEnv.VITE_PROXY)
    },
    resolve: {
      alias: {
        '@': pathSrc
      }
    },
    plugins: initVitePlugins(viteEnv, command === 'build'),
    build: {
      outDir: 'dist',
      minify: 'esbuild',
      sourcemap: false
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/assets/styles/variables.scss" as *;`
        }
      }
    },
    optimizeDeps: {
      include: [
        'vue',
        'vue-router',
        'pinia',
        'axios',
        '@vueuse/core',
        'element-plus',
        '@element-plus/icons-vue',
        'element-plus/es',
        'element-plus/es/components/base/style/css',
        'element-plus/es/components/config-provider/style/css',
        'element-plus/es/components/button/style/css',
        'element-plus/es/components/form/style/css',
        'element-plus/es/components/form-item/style/css',
        'element-plus/es/components/input/style/css',
        'element-plus/es/components/container/style/css',
        'element-plus/es/components/footer/style/css',
        'element-plus/es/components/main/style/css',
        'element-plus/es/components/header/style/css',
        'element-plus/es/components/message/style/css',
        'element-plus/es/components/menu/style/css',
        'element-plus/es/components/sub-menu/style/css',
        'element-plus/es/components/menu-item/style/css',
        'element-plus/es/components/loading/style/css',
        'element-plus/es/components/table/style/css',
        'element-plus/es/components/table-column/style/css',
        'element-plus/es/components/tag/style/css',
        'element-plus/es/components/icon/style/css',
        'element-plus/es/components/radio/style/css',
        'element-plus/es/components/avatar/style/css',
        'element-plus/es/components/drawer/style/css',
        'element-plus/es/components/switch/style/css',
        'element-plus/es/components/pagination/style/css',
        'element-plus/es/components/space/style/css',
        'element-plus/es/components/tooltip/style/css'
      ]
    }
  }
})
