import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'
import { bumpVersion } from './plugins/bump-version'

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '')
  const apiBaseUrl = env.VITE_API_BASE_URL || '/api'

  return {
    plugins: [
      vue(),
      vueDevTools(),

      // 自动导入 Vue / Vue Router / Pinia / VueUse / Vant 的 API，省去手动 import
      AutoImport({
        imports: [
          'vue',
          'vue-router',
          'pinia',
          '@vueuse/core',
          { vant: ['showToast', 'showDialog', 'showConfirmDialog', 'showLoadingToast', 'closeToast'] },
        ],
        dts: 'src/auto-imports.d.ts',
        vueTemplate: true,
      }),

      // 自动导入组件（Vant 按需加载）
      Components({
        resolvers: [VantResolver()],
        dts: 'src/components.d.ts',
      }),

      // 打包时自增版本号，并将 plugin.properties 输出到 dist/
      bumpVersion(mode),
    ],

    server: {
      host: '0.0.0.0',
      port: 8080,
      strictPort: true,
      open: true,

      proxy: {
        '/oms': {
          target: 'http://10.56.183.104:8090',
          changeOrigin: true,
        },
      },
    },

    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },

    build: {
      outDir: 'dist/www',
    },
  }
})
