import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '')
  const isCapacitor = env.VITE_ROUTER_MODE === 'hash'

  return {
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
        dts: 'src/auto-imports.d.ts',
      }),
      Components({
        resolvers: [ElementPlusResolver({ importStyle: 'css', directives: true })],
        dts: 'src/components.d.ts',
      }),
    ],
    base: isCapacitor ? './' : '/',
    build: {
      outDir: isCapacitor ? 'dist' : '../src/Jiaowu.Api/wwwroot',
      emptyOutDir: true,
    },
    server: {
      port: 5173,
      proxy: {
        '/api': {
          target: 'http://localhost:5255',
          changeOrigin: true,
        },
        '/health': {
          target: 'http://localhost:5255',
          changeOrigin: true,
        },
      },
    },
  }
})
