import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import path from "node:path";

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, path.resolve(__dirname, "../.."), "");
  const allowedHosts = (env.WEB_ALLOWED_HOSTS || "")
    .split(",")
    .map((host) => host.trim().replace(/^"|"$/g, ""))
    .filter(Boolean);

  return {
    plugins: [react()],
    build: {
      outDir: path.resolve(__dirname, "../server/public"),
      emptyOutDir: true,
    },
    server: {
      host: "0.0.0.0",
      port: Number(env.WEB_PORT) || 5173,
      allowedHosts: allowedHosts.length ? allowedHosts : true,
    },
  };
});
