"use strict";

exports.__esModule = true;
exports.JOB_POLL_INTERVAL_MS = void 0;
exports.isTerminalJobStatus = isTerminalJobStatus;
exports.pollImportJob = pollImportJob;
var _types = require("@wix/bex-utils/@wix/ambassador-hub-v1-job/types");
const JOB_POLL_INTERVAL_MS = exports.JOB_POLL_INTERVAL_MS = 2000;

/** A job is terminal once it has succeeded, partially succeeded, or failed. */
function isTerminalJobStatus(status) {
  return status === _types.JobStatus.COMPLETED || status === _types.JobStatus.PARTIALLY_SUCCESSFUL || status === _types.JobStatus.FAILED;
}
/**
 * Polls a data-movement job until it reaches a terminal status, then resolves
 * with the terminal job. Resolves `null` if cancelled mid-poll. Pure async unit
 * (no MobX) so `ImportState` stays thin and this is testable on its own.
 */
async function pollImportJob(getJob, jobId, options = {}) {
  const {
    intervalMs = JOB_POLL_INTERVAL_MS,
    onProgress,
    isCancelled,
    wait = defaultWait
  } = options;
  for (;;) {
    if (isCancelled != null && isCancelled()) {
      return null;
    }
    const job = await getJob(jobId);
    onProgress == null || onProgress(job);
    if (isTerminalJobStatus(job.status)) {
      return job;
    }
    if (isCancelled != null && isCancelled()) {
      return null;
    }
    await wait(intervalMs);
  }
}
function defaultWait(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
//# sourceMappingURL=importJobPoller.js.map