import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react';

export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '');

  return defineConfig({
    server: {
      host: true,
      port: 3000,
      proxy: {
        // 지도 타일 서버
        '/MAPS': {
          target: env.VITE_MAP_TILE_URL || 'http://10.26.252.39:9090',
          changeOrigin: true,
          secure: false,
        },
        // GeoJSON 데이터
        '/geo': {
          target: env.VITE_MAP_TILE_URL || 'http://10.26.252.39:9090',
          changeOrigin: true,
          secure: false,
        },
        // 선박 신호 API (signal-api)
        // 참조: mda-react-front/vite.config.ts
        '/signal-api': {
          target: env.VITE_SIGNAL_API || 'http://10.26.252.39:9090/signal-api',
          changeOrigin: true,
          secure: false,
          rewrite: (path) => path.replace(/^\/signal-api/, ''),
        },
        // API 서버
        '/api': {
          target: env.VITE_API_URL || 'http://localhost:8080',
          changeOrigin: true,
          secure: false,
        },
      },
    },
    plugins: [react()],
    resolve: {
      alias: {
        '@': '/src',
      },
    },
    build: {
      outDir: 'dist',
      cssCodeSplit: true,
      rollupOptions: {
        output: {
          manualChunks: {
            vendor: ['react', 'react-dom', 'react-router-dom'],
            map: ['ol', 'ol-ext'],
            state: ['zustand'],
          },
        },
      },
    },
  });
};
