import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("data_sync").child({ component: "sync-import" });
const metadata = {
  queue: "data-sync-import",
  id: "data-sync:import",
  concurrency: 5
};
async function handle(job, ctx) {
  try {
    const engine = ctx.resolve("dataSyncEngine");
    await engine.runImport(job.payload.runId, job.payload.batchSize, job.payload.scope);
  } catch (error) {
    const message = error instanceof Error ? error.message : "Data sync import worker failed";
    const errorStack = error instanceof Error ? error.stack : void 0;
    try {
      const syncRunService = ctx.resolve("dataSyncRunService");
      const progressService = ctx.resolve("progressService");
      const run = await syncRunService.getRun(job.payload.runId, job.payload.scope);
      if (run && run.status !== "completed" && run.status !== "failed" && run.status !== "cancelled") {
        await syncRunService.markStatus(run.id, "failed", job.payload.scope, message);
        if (run.progressJobId) {
          await progressService.failJob(
            run.progressJobId,
            {
              errorMessage: message,
              errorStack
            },
            job.payload.scope
          );
        }
      }
    } catch (finalizeError) {
      logger.error("Failed to finalize crashed import worker job", { err: finalizeError });
    }
    throw error;
  }
}
export {
  handle as default,
  metadata
};
//# sourceMappingURL=sync-import.js.map
