import { JobStatus, } from '@wix/bex-utils/@wix/ambassador-hub-v1-job/types';
export const JOB_POLL_INTERVAL_MS = 2000;
/** A job is terminal once it has succeeded, partially succeeded, or failed. */
export function isTerminalJobStatus(status) {
    return (status === JobStatus.COMPLETED ||
        status === JobStatus.PARTIALLY_SUCCESSFUL ||
        status === 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.
 */
export async function pollImportJob(getJob, jobId, options = {}) {
    const { intervalMs = JOB_POLL_INTERVAL_MS, onProgress, isCancelled, wait = defaultWait, } = options;
    for (;;) {
        if (isCancelled?.()) {
            return null;
        }
        const job = await getJob(jobId);
        onProgress?.(job);
        if (isTerminalJobStatus(job.status)) {
            return job;
        }
        if (isCancelled?.()) {
            return null;
        }
        await wait(intervalMs);
    }
}
function defaultWait(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}
//# sourceMappingURL=importJobPoller.js.map