import { getConfig } from "../config.js";
import { backfillEmbeddings } from "../cache/backfill-embeddings.js";
const HELP = `wigolo backfill \u2014 backfill embeddings for cached pages missing them

Usage:
  wigolo backfill [--dry-run] [--limit N] [--batch-size N]

Options:
  --dry-run           Compute embeddings but do not write to the vector store
  --limit N           Process at most N rows
  --batch-size N      Embed N pages per request (default 32)
  -h, --help          Print this help
`;
function parseNumberFlag(args, name) {
  const idx = args.indexOf(name);
  if (idx < 0) return void 0;
  const raw = args[idx + 1];
  if (raw === void 0) return void 0;
  const n = Number(raw);
  return Number.isFinite(n) && n > 0 ? n : void 0;
}
async function runBackfill(args) {
  if (args.includes("--help") || args.includes("-h")) {
    process.stdout.write(HELP);
    return 0;
  }
  const dryRun = args.includes("--dry-run");
  const limit = parseNumberFlag(args, "--limit");
  const batchSize = parseNumberFlag(args, "--batch-size");
  process.stderr.write("[wigolo backfill] scanning cache for rows without embeddings\u2026\n");
  const dataDir = getConfig().dataDir;
  const result = await backfillEmbeddings({
    dataDir,
    limit,
    batchSize,
    dryRun,
    onProgress: (done, total) => {
      if (total > 0 && (done === total || done % 100 === 0)) {
        process.stderr.write(`  ${done} / ${total}
`);
      }
    }
  });
  if (result.reason) {
    process.stderr.write(`[wigolo backfill] ${result.reason}
`);
    return 1;
  }
  process.stderr.write(
    `[wigolo backfill] done: scanned=${result.scanned} embedded=${result.embedded} skipped=${result.skipped} errors=${result.errors} model=${result.modelId}${dryRun ? " (dry-run)" : ""}
`
  );
  return result.errors > 0 && result.embedded === 0 ? 1 : 0;
}
export {
  runBackfill
};
//# sourceMappingURL=backfill.js.map