import { readFile, writeFile } from "node:fs/promises";
import { buildPagesConfig } from "./cloudflare-pages-config.mjs";

const production = JSON.parse(await readFile(
  new URL("../wrangler.production.jsonc", import.meta.url),
  "utf8",
));
const databaseId = production.d1_databases?.find(({ binding }) => binding === "DB")?.database_id;
const config = buildPagesConfig({ databaseId });
await writeFile(
  new URL("../pages/wrangler.jsonc", import.meta.url),
  `${JSON.stringify(config, null, 2)}\n`,
  "utf8",
);
console.log("Wrote isolated HearBooks Pages configuration.");
