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

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

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);
const streamBrowserifyPath = require.resolve("stream-browserify");

export default defineConfig(() => {
  return {
    plugins: [react(), tailwindcss()],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
        "node:buffer": "buffer",
        "node:stream": streamBrowserifyPath,
        // Resolve from TS source so Vite gets ESM named exports (dist is CommonJS).
        "@edr/types": path.resolve(
          __dirname,
          "../../../packages/types/src/index.ts",
        ),
      },
      // Force a single copy of these singletons so MantineProvider context is
      // shared between the backoffice app and @edr/ui-common (which ships its
      // own node_modules copy). Without this, two separate @mantine/core
      // instances are bundled and the context lookup fails at runtime.
      dedupe: ["react", "react-dom", "@mantine/core", "@mantine/hooks"],
    },
    server: {
      port: 5183,
      host: "0.0.0.0",
    },
    test: {
      environment: "node",
    },
  };
});
