import { fileURLToPath } from "node:url";
import tailwindcss from "@tailwindcss/vite";
import react from "@vitejs/plugin-react";
import { defineConfig, configDefaults } from "vitest/config";
import type { UserConfig } from "vite";

export function createDevServerConfig(
  env: NodeJS.ProcessEnv = process.env,
): UserConfig["server"] {
  const apiTarget = env.DASHBOARD_DEV_API_TARGET;
  if (!apiTarget) return undefined;

  return {
    proxy: {
      "/api": {
        target: apiTarget,
        changeOrigin: true,
      },
    },
  };
}

export default defineConfig({
  plugins: [react(), tailwindcss()],
  resolve: {
    alias: {
      $lib: fileURLToPath(new URL("./src/lib", import.meta.url)),
    },
  },
  server: createDevServerConfig(),
  test: {
    exclude: [...configDefaults.exclude, "tests/e2e/**"],
  },
});
