import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import PrerenderPlugin from '@prerenderer/rollup-plugin'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { fileURLToPath, URL } from 'node:url'

export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    PrerenderPlugin({
      staticDir: 'dist',
      routes: ['/'],
      renderer: '@prerenderer/renderer-puppeteer',
      rendererOptions: {
        renderAfterTime: 5000,
        renderAfterDocumentEvent: 'prerender-trigger'
      }
    } as any)
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  build: {
    outDir: 'dist'
  },
  server: {
    host: '0.0.0.0',
    proxy: {
      '/api': {
        target: 'http://127.0.0.1:8080',
        changeOrigin: true,
      },
      '/ai-api': {
          // target: 'http://192.168.31.133:8000',
          target: 'http://127.0.0.1:8000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/ai-api/, ''),
      },
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        // 各 scss 文件按需 @use '../variables' as * 引入全局变量
      }
    }
  }
})