import path, { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

import { defineConfig } from '@vben/vite-config'

import AutoImport from 'unplugin-auto-import/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'

const __dirname = dirname(fileURLToPath(import.meta.url))

export default defineConfig(async () => {
  return {
    application: {
      injectGlobalScss: false,
    },
    vite: {
      css: {
        preprocessorOptions: {
          scss: {
            additionalData: `
            @use "styles/theme.scss" as *;
            @use "styles/variate.scss" as *;
            `,
            api: 'modern',
            loadPaths: [path.resolve(__dirname, './src')],
          },
        },
      },
      server: {
        port: 2345,
        host: '0.0.0.0',
        open: true,
        proxy: {
          '/api': {
            target: 'http://43.136.52.196:9053',
            // target: 'https://admin.xuexiaole.com',
            changeOrigin: true,
          },
        },
      },
      plugins: [
        AutoImport({
          // 自动导入Vue相关函数，如ref, reactive等
          imports: ['vue', 'vue-router', 'pinia'],
          // 可以选择auto-import.d.ts生成的位置，使用ts建议设置为'src/auto-import.d.ts'
          dts: 'src/auto-import.d.ts',
          // 自动导入目录下的模块
          dirs: ['src/store'],
          // 自动导入的API
          vueTemplate: true,
          resolvers: [ElementPlusResolver()],
        }),
        Components({
          // 自动导入组件目录
          dirs: ['src/components'],
          // 组件的有效文件扩展名
          extensions: ['vue'],
          // 配置type文件生成位置，使用ts建议设置为'src/components.d.ts'
          dts: 'src/components.d.ts',
          // 自动导入组件库解析器
          resolvers: [ElementPlusResolver()],
        }),
      ],
    },
  }
})
