import path from "path";
import { createRequire } from "module";
import tailwindcss from "@tailwindcss/vite";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { defineEnv } from "./vite.env";

const require = createRequire(import.meta.url);
const pkg = require("./package.json") as { version: string };

// Web build target for the browser. Aliases swap the platform adapter, the
// router shell, and Sentry over to web variants. Electron build is untouched
// — see vite.renderer.config.mts.
export default defineConfig({
  root: path.resolve(__dirname, "src/web"),
  plugins: [react(), tailwindcss()],
  resolve: {
    // Exact-match regexes for the file-level swaps — `@/lib/platform` points at
    // a file (`index.web.ts`), so a string-prefix alias would also swallow
    // deeper paths like `@/lib/platform/desktop-only`. The generic `@` alias
    // handles everything else.
    alias: [
      { find: /^@\/lib\/platform$/, replacement: path.resolve(__dirname, "./src/lib/platform/index.web.ts") },
      { find: /^@\/lib\/sentry$/, replacement: path.resolve(__dirname, "./src/lib/sentry.web.ts") },
      { find: /^@\/lib\/router-shell$/, replacement: path.resolve(__dirname, "./src/lib/router-shell.web.tsx") },
      { find: "@", replacement: path.resolve(__dirname, "./src") },
    ],
  },
  define: {
    ...defineEnv,
    __APP_VERSION__: JSON.stringify(pkg.version),
  },
  server: {
    port: 5174,
  },
  build: {
    outDir: path.resolve(__dirname, "dist-web"),
    emptyOutDir: true,
  },
});
