import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import UnoCss from 'unocss/vite'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import IconsResolver from 'unplugin-icons/resolver'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import monacoEditorPlugin from 'vite-plugin-monaco-editor'
import vueJsx from '@vitejs/plugin-vue-jsx'

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

	return {
		base: './',
		build: {
			outDir: 'app-agent'
			// rollupOptions: {
			// 	output: {
			// 		manualChunks: {
			// 			framework: ['vue', 'vue-router', 'pinia'],
			// 			element: ['element-plus', '@element-plus/icons-vue'],
			// 			monaco: ['monaco-editor'],
			// 			lexical: ['lexical', 'lexical-vue'],
			// 			echarts: ['echarts']
			// 		}
			// 	}
			// }
		},
		plugins: [
			vue(),
			vueJsx(),
			UnoCss(),
			createSvgIconsPlugin({
				// 指定存放 SVG 的文件夹
				iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
				symbolId: 'svg-icon-[dir]-[name]'
			}),
			// 代码编辑器
			(monacoEditorPlugin as any).default({
				languageWorkers: ['editorWorkerService', 'typescript', 'json', 'html', 'css']
			}),
			// 按需求加载(模板)
			AutoImport({
				imports: ['vue'],
				resolvers: [
					IconsResolver({
						prefix: 'Icon'
					}),
					ElementPlusResolver()
				],
				dts: 'auto-imports.d.ts'
			}),
			Components({
				resolvers: [
					// 自动注册图标组件
					IconsResolver({
						enabledCollections: ['ep']
					}),
					ElementPlusResolver()
				],
				dts: 'components.d.ts'
			})
		],
		resolve: {
			alias: {
				'@': path.resolve(__dirname, 'src')
			}
		},
		server: {
			host: true,
			port: 5174,
			proxy: {
				'/api': {
					target: `http://${env.VITE_BASE_URL}`,
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/api/, '/api')
				},
				'/Content': {
					target: `http://shalu-componenttesting-admin-dev.shalu.com`,
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/Content/, '/Content')
				},
				'/Views': {
					target: `http://shalu-componenttesting-admin-dev.shalu.com`,
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/Views/, '/Views')
				},
				'/File': {
					target: `http://${env.VITE_BASE_URL}`,
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/File/, '/File')
				},
				'/App': {
					target: `http://${env.VITE_BASE_URL}`,
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/App/, '/App')
				},
				'/_runtime': {
					target: `http://${env.VITE_BASE_URL}`,
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/_runtime/, '/_runtime')
				}
			}
		}
	}
})
