import path from "node:path";
import { fileURLToPath } from "node:url";

import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

// Pin Mantine to this app's copy. pnpm can install a second @mantine/core under
// @edr/ui-common (linked to react@18) while the portal uses react@19 — dedupe
// alone does not merge those into one module in production builds.
const mantineCore = path.resolve(__dirname, "node_modules/@mantine/core");
const mantineHooks = path.resolve(__dirname, "node_modules/@mantine/hooks");

export default defineConfig({
  plugins: [react(), tailwindcss()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
      // Resolve from TS source so Vite gets ESM named exports (dist is CommonJS).
      "@edr/types": path.resolve(__dirname, "../../../packages/types/src/index.ts"),
      "@mantine/core": mantineCore,
      "@mantine/hooks": mantineHooks,
    },
    dedupe: ["react", "react-dom", "@mantine/core", "@mantine/hooks"],
  },
  optimizeDeps: {
    include: ["@mantine/core", "@mantine/hooks", "@edr/ui-common"],
  },
  server: {
    port: 5173,
    host: "0.0.0.0",
  },
  test: {
    environment: "node",
  },
});
