import { defineConfig } from "vite";
import solidPlugin from "vite-plugin-solid";
import solidSvg from "vite-plugin-solid-svg";
import { patchCssModules } from "vite-css-modules";
import path from "node:path";
import { exec } from "child_process";

// watch also clan-cli to catch api changes
const clanCliDir = path.resolve(__dirname, "../../clan-cli");

function regenPythonApiOnFileChange() {
  return {
    name: "run-python-script-on-change",
    handleHotUpdate({}) {
      exec("reload-python-api.sh", (err, stdout, stderr) => {
        if (err) {
          console.error(`reload-python-api.sh error:\n${stderr}`);
        }
      });
    },
    configureServer(server: import("vite").ViteDevServer) {
      server.watcher.add([clanCliDir]);
    },
  };
}

export default defineConfig({
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./"), // Adjust the path as needed
    },
  },
  base: "./",
  optimizeDeps: {
    include: ["debug", "extend"],
  },
  plugins: [
    /*
    Uncomment the following line to enable solid-devtools.
    For more info see https://github.com/thetarnav/solid-devtools/tree/main/packages/extension#readme
    */
    solidPlugin(),
    solidSvg(),
    regenPythonApiOnFileChange(),
    patchCssModules({ generateSourceTypes: true }),
  ],
  server: {
    port: 3000,
  },
  build: {
    target: "safari11",
    modulePreload: false,
    //  assetsDi
    manifest: true,
    // Inline everything: TODO
    // Detect file:///assets requests and point to the correct directory in webview
    rollupOptions: {
      output: {
        format: "iife",
        // entryFileName: ""
        // inlineDynamicImports: true,
      },
    },
    // assetsInlineLimit: 0,
  },
});
