// SPDX-FileCopyrightText: 2026 Stan Grams <sjg@haxx.space>
//
// SPDX-License-Identifier: GPL-2.0-or-later

import { build } from "esbuild";
import { rm } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import path from "node:path";

const frontendDir = path.dirname(fileURLToPath(import.meta.url));
const sourceDir = path.join(frontendDir, "src");
const outputDir = path.join(frontendDir, "..", "assets", "web", "generated");

await rm(outputDir, { recursive: true, force: true });

await build({
  entryPoints: {
    app: path.join(sourceDir, "bootstrap.ts"),
    ft2: path.join(sourceDir, "plugins", "ft2.ts"),
    ft4: path.join(sourceDir, "plugins", "ft4.ts"),
    wspr: path.join(sourceDir, "plugins", "wspr.ts"),
    cw: path.join(sourceDir, "plugins", "cw.ts"),
    ft8: path.join(sourceDir, "plugins", "ft8.ts"),
    vdes: path.join(sourceDir, "plugins", "vdes.ts"),
    wefax: path.join(sourceDir, "plugins", "wefax.ts"),
    "background-decode": path.join(sourceDir, "plugins", "background-decode.ts"),
    ais: path.join(sourceDir, "plugins", "ais.ts"),
    aprs: path.join(sourceDir, "plugins", "aprs.ts"),
    "hf-aprs": path.join(sourceDir, "plugins", "hf-aprs.ts"),
    sat: path.join(sourceDir, "plugins", "sat.ts"),
    "sat-scheduler": path.join(sourceDir, "plugins", "sat-scheduler.ts"),
    vchan: path.join(sourceDir, "plugins", "vchan.ts"),
    bookmarks: path.join(sourceDir, "plugins", "bookmarks.ts"),
    scheduler: path.join(sourceDir, "plugins", "scheduler.ts"),
    "map-core": path.join(sourceDir, "map-core.ts"),
    screenshot: path.join(sourceDir, "screenshot.ts"),
  },
  outdir: outputDir,
  bundle: true,
  format: "esm",
  splitting: true,
  entryNames: "[name]",
  chunkNames: "chunk-[hash]",
  platform: "browser",
  target: "es2022",
  sourcemap: false,
  legalComments: "inline",
  charset: "utf8",
  logLevel: "info",
});

await build({
  entryPoints: [path.join(sourceDir, "decode-history-worker.ts")],
  outfile: path.join(outputDir, "decode-history-worker.js"),
  bundle: true,
  format: "iife",
  platform: "browser",
  target: "es2022",
  sourcemap: false,
  legalComments: "inline",
  charset: "utf8",
  logLevel: "info",
});
