import { readJsonlRaw } from "./jsonl.ts";

/**
 * Build a Set of completed paragraph IDs from an annotation JSONL file.
 * Used for crash-safe resume: skip paragraphs already in the output.
 */
export async function loadCompletedIds(
  outputPath: string,
): Promise<{ completedIds: Set<string>; skippedLines: number }> {
  const { records, skipped } = await readJsonlRaw(outputPath);
  const completedIds = new Set<string>();

  for (const record of records) {
    if (
      record &&
      typeof record === "object" &&
      "paragraphId" in record &&
      typeof (record as { paragraphId: unknown }).paragraphId === "string"
    ) {
      completedIds.add((record as { paragraphId: string }).paragraphId);
    }
  }

  return { completedIds, skippedLines: skipped };
}
