import { execSync } from "node:child_process";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

const packageJson = JSON.parse(readFileSync(resolve(__dirname, "package.json"), "utf8")) as { version?: string };

function readGitCommit(): string {
  try {
    return execSync("git rev-parse --short HEAD", { cwd: __dirname, stdio: ["ignore", "pipe", "ignore"] })
      .toString()
      .trim();
  } catch {
    return "dev";
  }
}

const appBuildLabel = `v${packageJson.version || "0.0.0"}-${readGitCommit()}`;

// https://vite.dev/config/
export default defineConfig({
  plugins: [react()],
  define: {
    __APP_BUILD__: JSON.stringify(appBuildLabel),
  },
  build: {
    rollupOptions: {
      input: {
        main: resolve(__dirname, "index.html"),
        futureSharedContract: resolve(__dirname, "Future - Shared Contract.html"),
        worldshaperStudio: resolve(__dirname, "worldshaper-studio.html"),
        worldshaperHeightViewer: resolve(__dirname, "worldshaper-height-viewer.html"),
      },
      output: {
        manualChunks(id) {
          const normalizedId = id.replace(/\\/g, "/");
          if (normalizedId.includes("node_modules")) {
            if (normalizedId.includes("/pixi.js/")) {
              return "vendor-pixi";
            }
            if (normalizedId.includes("/react/") || normalizedId.includes("/react-dom/")) {
              return "vendor-react";
            }
            return "vendor-misc";
          }
          if (!normalizedId.includes("/src/worldshaperStudio/")) {
            return undefined;
          }
          if (
            normalizedId.includes("tileArtEditorWindowController")
            || normalizedId.includes("entityEditorWindowController")
            || normalizedId.includes("engineOverrideWindowController")
            || normalizedId.includes("statusLogWindowController")
            || normalizedId.includes("changelogSplashWindowController")
            || normalizedId.includes("toolWindowController")
            || normalizedId.includes("worldOverviewWindowController")
          ) {
            return "map-editor-windows";
          }
          if (normalizedId.includes("pixiTileStageController")) {
            return "map-editor-pixi";
          }
          if (
            normalizedId.includes("renderController")
            || normalizedId.includes("overlayRenderer")
          ) {
            return "map-editor-render";
          }
          if (
            normalizedId.includes("interactionController")
            || normalizedId.includes("sidebarController")
            || normalizedId.includes("npcController")
            || normalizedId.includes("historyController")
            || normalizedId.includes("historyStateStore")
            || normalizedId.includes("importController")
            || normalizedId.includes("persistenceController")
            || normalizedId.includes("graphicsDocumentHelpers")
          ) {
            return "map-editor-core";
          }
          return undefined;
        },
      },
    },
  },
  server: {
    host: true,
    port: 4170,
    proxy: {
      "/api": {
        target: "http://localhost:5180",
        changeOrigin: true,
      },
    },
  },
  preview: {
    host: true,
    port: 4170,
  },
});

