import {
  buildFinalizeLogResult,
  type FinalizeLogJob,
  type FinalizeLogResult,
} from "./logFinalizer";

// Child-process entry point for session finalization.
// Communicates with the parent via IPC (process.send / process.on("message")).
// Each job is { id: string; job: FinalizeLogJob }, each response is { id: string; result: FinalizeLogResult }.

process.on("message", (msg: { id: string; job: FinalizeLogJob }) => {
  let result: FinalizeLogResult;
  try {
    result = buildFinalizeLogResult(msg.job);
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    result = {
      log: msg.job.log,
      upstreamUrl: msg.job.upstreamUrl,
      error: message,
    };
  }
  if (process.send !== undefined) {
    process.send({ id: msg.id, result });
  }
});
