"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runChunkQueue = exports.runQueue = void 0;
function runQueue(items, processor, delay) {
    const leftItems = items.slice();
    return new Promise((resolve, reject) => {
        const run = () => {
            if (leftItems.length === 0) {
                resolve();
                return;
            }
            const item = leftItems.shift();
            processor(item, (i) => {
                leftItems.push(i);
            }).then(() => {
                setTimeout(run, delay);
            }).catch(reject);
        };
        run();
    });
}
exports.runQueue = runQueue;
function runChunkQueue(items, size, processor, delay) {
    const toProcessItems = items.slice();
    return new Promise((resolve, reject) => {
        const run = () => {
            if (toProcessItems.length === 0) {
                resolve();
                return;
            }
            const items = toProcessItems.splice(0, size);
            processor(items, (i) => {
                toProcessItems.push(...i);
            }).then(() => {
                setTimeout(run, delay);
            }).catch(reject);
        };
        run();
    });
}
exports.runChunkQueue = runChunkQueue;
