#!/usr/bin/env node
// Reads the compiled worker output (dist/worker.js) and emits
// dist/worker-source.generated.js exporting it as a string constant. Runs
// after `tsc -b` (see package.json "build"). The companion .d.ts shim lives
// at src/worker-source.generated.d.ts so tsc can resolve the import from
// src/index.ts at compile time.
import { readFileSync, writeFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';

const here = dirname(fileURLToPath(import.meta.url));
const distDir = join(here, '..', 'dist');
const workerJsPath = join(distDir, 'worker.js');
const outPath = join(distDir, 'worker-source.generated.js');

const workerSource = readFileSync(workerJsPath, 'utf8');

// JSON.stringify produces a valid JS string literal — handles backticks,
// backslashes, and Unicode safely with no template-literal interpolation
// surprises.
const body = `// Generated by scripts/emit-source.mjs — do not edit by hand.\n` +
  `export const WORKER_SOURCE = ${JSON.stringify(workerSource)};\n`;

writeFileSync(outPath, body);
console.log(`[emit-source] wrote ${outPath} (${workerSource.length} chars)`);
