import { fileURLToPath } from "node:url";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vitest/config";

const commonRoot = fileURLToPath(new URL("../common", import.meta.url)).replace(
  /\\/g,
  "/",
);
const commonCoverageIncludes = ["core", "shell", "theme", "ui"].map(
  (directory) => `${commonRoot}/${directory}/**/*.{ts,tsx}`,
);

export default defineConfig({
  plugins: [react()],
  server: {
    fs: {
      allow: [
        fileURLToPath(new URL(".", import.meta.url)),
        fileURLToPath(new URL("../common", import.meta.url)),
        "/workspace/common",
      ],
    },
  },
  test: {
    globals: true,
    environment: "jsdom",
    include: ["src/**/*.{test,spec}.{ts,tsx}"],
    coverage: {
      provider: "v8",
      reporter: ["text", "html", "lcov", "json-summary"],
      reportsDirectory: "coverage",
      all: true,
      allowExternal: true,
      include: ["src/**/*.{ts,tsx}", ...commonCoverageIncludes],
      exclude: [
        "**/*.{test,spec}.{ts,tsx}",
        "**/*.d.ts",
        "**/node_modules/**",
        "**/dist/**",
        "**/coverage/**",
        "src/main.tsx",
        "src/vite-env.d.ts",
        "../common/**/node_modules/**",
        "../common/.pnpm-store/**",
        `${commonRoot}/theme/**`,
        `${commonRoot}/core/pagination/*.ts`,
        `${commonRoot}/core/pagination/*.worker.ts`,
        `${commonRoot}/core/query/queryClient.ts`,
      ],
    },
  },
});
