import {defineConfig} from 'vite'
import react from '@vitejs/plugin-react'
import {resolve} from "path";
import AppPackage from './package.json'
import dayjs from "dayjs";

// https://vitejs.dev/config/
export default defineConfig(({mode}) => {
	const devServerHost = mode == 'test' ? 'https://fm-admin.starbitech.com' : 'http://192.168.0.231:9999'
	const AUTH_TOKEN_KEY = mode == 'production' ? 'digital-person-token' : `digital-person-token_${mode}`

	if (mode !== 'production') {
		console.log('dev server is', devServerHost, mode)
	}
	return {
		plugins: [react()],
		base: process.env.PUBLIC_PATH || (mode == 'relative' ? './' : '/'),
		define: {
			AppConfig: JSON.stringify({
				SITE_URL: process.env.APP_SITE_URL || null,
				API_PREFIX: process.env.APP_API_PREFIX || '/mgmt/v1/metahuman',
				AUTH_TOKEN_KEY: process.env.AUTH_TOKEN_KEY || AUTH_TOKEN_KEY,
				AUTHED_PERSON_DATA_KEY: process.env.AUTHED_PERSON_DATA_KEY || 'digital-person-user-info',
				ONLY_LIVE: process.env.ONLY_LIVE || 'no',
				APP_LANG: process.env.APP_LANGUAGE || 'zh-CN'
			}),
			AppMode: JSON.stringify(mode),
			AppBuildVersion: JSON.stringify(AppPackage.name + '-' + AppPackage.version + '-' + dayjs().format('YYYYMMDDHH_mmss'))
		},
		resolve: {
			alias: {
				'@': resolve(__dirname, './src')
			}
		},
		css: {
			preprocessorOptions: {
				scss: {
					api: 'modern'
				}
			}
		},
		server: {
			port: 10021,
			proxy: {
				'/mgmt': {
					target: `${devServerHost}`, // http://124.220.14.192/ 192.168.0.231:9999
					changeOrigin: true,
					// rewrite: (path) => path.replace(/^\/api/, '')
				},
				'/api': {
					target: `${devServerHost}`,
					changeOrigin: true,
					// rewrite: (path) => path.replace(/^\/api/, '')
				}
			}
		}
	}
})
