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

import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import {readFileSync} from 'node:fs'
import {resolve} from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import {NaiveUiResolver} from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import {unheadVueComposablesImports} from '@unhead/vue'

// 从 package.json 里搞到 WebUI 版本号
const pkg = JSON.parse(readFileSync(resolve(__dirname, 'package.json'), 'utf-8'))

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
    AutoImport({
      imports: [
        'vue',
        {
          'naive-ui': ['useDialog', 'useMessage', 'useNotification', 'useLoadingBar'],
        },
        unheadVueComposablesImports,
      ],
    }),
    Components({
      resolvers: [NaiveUiResolver()],
    }),
  ],
  define: {
    __VERSION__: JSON.stringify(pkg.version),
  },
  build: {
    rolldownOptions: {
      input: {
        index: resolve(__dirname, 'index.html'),
      },
    },
    outDir: '../src/nyahome/static',
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:9000',
        changeOrigin: true,
      },
      '/admin': {
        target: 'http://localhost:9000',
        changeOrigin: true,
      },
      '/nyahome': {
        target: 'http://localhost:9000',
        changeOrigin: true,
      },
      '/download': {
        target: 'http://localhost:9000',
        changeOrigin: true,
      },
    },
  },
})
