import { parentPort, workerData } from "node:worker_threads";
import { FactoryRunDetailTooLargeError, loadFactoryRunDetail } from "./adapters/factory";

const { adwId, dbPath } = workerData as { adwId: string; dbPath: string };
try {
  const run = loadFactoryRunDetail(adwId, { dbPath });
  parentPort?.postMessage(
    run === undefined
      ? { status: "missing" }
      : { status: "ok", body: JSON.stringify(run) },
  );
} catch (error) {
  if (error instanceof FactoryRunDetailTooLargeError) {
    parentPort?.postMessage({ status: "too-large" });
  } else {
    throw error;
  }
}
