import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import path from "node:path";

// E2E/dev mode: when VITE_E2E_API is set, swap Wails bindings for HTTP shims
// that hit the matrix_client_pc E2E HTTP server. The production `wails build`
// run does NOT set VITE_E2E_API -> bindings resolve to the real generated files.
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), "");
  const useShim = !!env.VITE_E2E_API;

  const aliases = useShim
    ? {
        "../wailsjs/go/main/MatrixService": path.resolve(
          __dirname,
          "src/shims/MatrixServiceShim.ts",
        ),
        "../../wailsjs/go/main/MatrixService": path.resolve(
          __dirname,
          "src/shims/MatrixServiceShim.ts",
        ),
        "../wailsjs/runtime/runtime": path.resolve(
          __dirname,
          "src/shims/RuntimeShim.ts",
        ),
        "../../wailsjs/runtime/runtime": path.resolve(
          __dirname,
          "src/shims/RuntimeShim.ts",
        ),
      }
    : {};

  return {
    plugins: [react()],
    resolve: { alias: aliases },
    server: {
      host: "0.0.0.0",
      port: 5173,
      strictPort: false,
    },
  };
});
