import { loadEnv, type ServerOptions } from 'vite';
import { root, wrapperEnv } from './utils';

export const serverOptions = (mode: string) => {
	const { VITE_APP_URL, VITE_APP_MOCK_URL, VITE_PORT } = wrapperEnv(loadEnv(mode, root));

	const options: ServerOptions = {
		port: VITE_PORT,
		host: '0.0.0.0',
		open: true,
		cors: true,
		proxy: {
			'/api': {
				target: VITE_APP_URL,
				changeOrigin: true,
				rewrite: (path: string) => path.replace(/^\/api/, '/api'),
			},
			'/mock': {
				target: VITE_APP_MOCK_URL,
				changeOrigin: true,
				rewrite: (path: string) => path.replace(/^\/mock/, '/mock'),
			},
		},
	};

	return options;
};
