import path from 'path';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import jingrowui from 'jingrow-ui/vite';
import pluginRewriteAll from 'vite-plugin-rewrite-all';
import Components from 'unplugin-vue-components/vite';
import Icons from 'unplugin-icons/vite';
import IconsResolver from 'unplugin-icons/resolver';
import { sentryVitePlugin } from '@sentry/vite-plugin';
import vitePluginTranslate from './vite-plugin-translate.mjs';

// 语言配置：设置目标语言，默认为 'en'（英文），可设置为 'zh'（中文）等
const locale = process.env.DASHBOARD_LOCALE || 'zh';

export default defineConfig({
	plugins: [
		vue(),
		vueJsx(),
		pluginRewriteAll(),
		vitePluginTranslate({ locale }), // 传递语言配置
		jingrowui(),
		Components({
			dirs: [
				'src/components',
				'node_modules/jingrow-ui/src/components'
			],
			resolvers: [IconsResolver()]
		}),
		Icons(),
		sentryVitePlugin({
			url: process.env.SENTRY_URL,
			org: process.env.SENTRY_ORG,
			project: process.env.SENTRY_PROJECT,
			applicationKey: 'jcloud-dashboard',
			authToken: process.env.SENTRY_AUTH_TOKEN
		})
	],
	resolve: {
		alias: {
			'@': path.resolve(__dirname, 'src')
		}
	},
	optimizeDeps: {
		include: ['feather-icons', 'showdown']
	},
	server: {
		host: '0.0.0.0',
		port: 8080,
		proxy: {
			'/api/action': {
				target: 'http://localhost',
				changeOrigin: true,
				secure: false
			}
		}
	},
	build: {
		outDir: '../jcloud/public/dashboard',
		emptyOutDir: true,
		sourcemap: true,
		target: 'es2015',
		rollupOptions: {
			input: {
				main: path.resolve(__dirname, 'index.html')
			}
		}
	},
	define: {
		// 将语言配置注入到代码中，构建时使用
		'import.meta.env.DASHBOARD_LOCALE': JSON.stringify(locale)
	},
	// @ts-ignore
	test: {
		globals: true,
		environment: 'jsdom',
		setupFiles: 'src/tests/setup/msw.js',
		coverage: {
			extension: ['.vue', '.js'],
			all: true
		}
	}
});
