"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeCorkableQueue = void 0;
const promises = __importStar(require("./promises"));
const types = __importStar(require("./types"));
const pause = Symbol('queue pause');
function makeCorkableQueue(options) {
    const handles = new Map();
    let corked = false;
    return {
        get corked() {
            return corked;
        },
        pause,
        run,
        cancel,
        cork,
        uncork,
    };
    async function run(task) {
        const handle = {
            running: false,
            async start() {
                if (handle.running)
                    return;
                handle.running = true;
                handle.controller = options.makeAbortController();
                try {
                    const result = await task(handle.controller.signal);
                    if (handle.running && result !== pause) {
                        handles.delete(task);
                        handle.promise.resolve(result);
                    }
                }
                catch (error) {
                    if (handle.running || !types.instanceOf(error, 'AbortError'))
                        handle.promise.reject(error);
                }
                finally {
                    return handle.promise;
                }
            },
            abort() {
                if (!handle.running)
                    return;
                handle.running = false;
                handle.controller.abort();
            },
            promise: promises.makeControlledPromise(),
        };
        handles.set(task, handle);
        if (!corked)
            handle.start();
        return handle.promise;
    }
    function cancel(task) {
        const handle = handles.get(task);
        if (!(handle === null || handle === void 0 ? void 0 : handle.running))
            return;
        handle.abort();
        handles.delete(task);
    }
    function cork() {
        if (corked)
            return;
        corked = true;
        Array.from(handles.values())
            .slice(1)
            .forEach(handle => handle.abort());
    }
    function uncork() {
        if (!corked)
            return;
        corked = false;
        handles.forEach(handle => handle.start());
    }
}
exports.makeCorkableQueue = makeCorkableQueue;
