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

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const rootDir = path.resolve(__dirname, "..");

const contentFiles = [
  "content/shared.js",
  "content/collectors/page-meta.js",
  "content/collectors/text-elements.js",
  "content/collectors/structured-data.js",
  "content/collectors/activity-hints.js",
  "content.js"
];

export async function buildContentBundle() {
  const chunks = [];

  for (const relativePath of contentFiles) {
    const absolutePath = path.join(rootDir, relativePath);
    const source = await fs.readFile(absolutePath, "utf8");
    chunks.push(`// ${relativePath}\n${source.trim()}\n`);
  }

  const bundle = `${chunks.join("\n")}\n`;
  await fs.writeFile(path.join(rootDir, "content.bundle.js"), bundle, "utf8");
}

if (process.argv[1] === fileURLToPath(import.meta.url)) {
  await buildContentBundle();
}
