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

// https://vite.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    port: 5174,
    proxy: {
      // HLS 스트림 프록시 등 상대 경로 API 요청을 백엔드로 전달
      '/api': {
        target: 'http://localhost:3001',
        changeOrigin: true,
      },
      '/daily_ocean': {
        target: 'https://www.khoa.go.kr',
        changeOrigin: true,
      },
      '/scat/img': {
        target: process.env.VITE_SERVER_URL || 'https://wing-demo.gc-si.dev/',
        changeOrigin: true,
        configure: (proxy) => {
          proxy.on('proxyReq', (proxyReq, req) => {
            console.log(
              `[scat-img] ${req.url} → ${proxyReq.protocol}//${proxyReq.host}${proxyReq.path}`,
            );
          });
          proxy.on('proxyRes', (proxyRes, req) => {
            console.log(`[scat-img] ${req.url} ← ${proxyRes.statusCode}`);
          });
          proxy.on('error', (err, req) => {
            console.error(`[scat-img] ${req.url} ERROR:`, err.message);
          });
        },
      },
    },
  },
  resolve: {
    alias: {
      '@common': path.resolve(__dirname, 'src/common'),
      '@tabs': path.resolve(__dirname, 'src/tabs'),
      '@': path.resolve(__dirname, 'src'),
    },
  },
  build: {
    rollupOptions: {
      // deck.gl → @loaders.gl/worker-utils가 Node.js child_process를 참조하나
      // 브라우저에서는 해당 코드 경로를 타지 않으므로 undefined shim 허용
      shimMissingExports: true,
    },
  },
});
