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

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
  server: {
    host: "::",
    port: 3000,
    // Served behind a proxy/tunnel on an arbitrary domain; allow any Host.
    allowedHosts: true,
    // Proxy API requests to backend - enables same-origin cookies in dev mode
    proxy: {
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
      },
    },
  },
  preview: {
    host: true,
    allowedHosts: true,
  },
  plugins: [
    react()
  ].filter(Boolean),
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
  build: {
    sourcemap: true,
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (!id.includes("node_modules")) {
            return undefined;
          }

          if (id.includes("/react/") || id.includes("/react-dom/")) {
            return "react";
          }

          if (id.includes("/react-router") || id.includes("/react-router-dom")) {
            return "router";
          }

          if (id.includes("/@radix-ui/")) {
            return "radix";
          }

          if (id.includes("/recharts/")) {
            return "charts";
          }

          if (
            id.includes("/react-hook-form/")
            || id.includes("/@hookform/")
            || id.includes("/zod/")
          ) {
            return "forms";
          }

          if (
            id.includes("/lucide-react/")
            || id.includes("/sonner/")
            || id.includes("/vaul/")
            || id.includes("/cmdk/")
          ) {
            return "ui";
          }

          return "vendor";
        },
      },
    },
  },
}));
