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

export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), '');
  const base = env.VITE_BASE_PATH || '/resto/build/';
  const apiBase = env.VITE_API_BASE || '/resto/api';

  return {
    plugins: [react()],
    base,
    build: {
      outDir: '../build',
      emptyOutDir: true,
      manifest: 'manifest.json',
    },
    server: {
      host: '0.0.0.0',
      port: 5173,
      strictPort: true,
      hmr:
        command === 'serve'
          ? {
              clientPort: 8071,
              path: base,
            }
          : undefined,
      proxy: {
        [apiBase]: {
          target: 'http://localhost:8071',
          changeOrigin: true,
        },
      },
    },
  };
});
