"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncFifoMutex = void 0;
/**
 * Async FIFO mutex (fair queue).
 *
 * Used for per-dialog and per-dialog-tree critical sections, keyed by dialog ID.
 */
class AsyncFifoMutex {
    locked = false;
    waiters = [];
    async acquire() {
        if (!this.locked) {
            this.locked = true;
            return () => this.release();
        }
        await new Promise((resolve) => {
            this.waiters.push(resolve);
        });
        return () => this.release();
    }
    isLocked() {
        return this.locked;
    }
    release() {
        const next = this.waiters.shift();
        if (next) {
            next();
            return;
        }
        this.locked = false;
    }
}
exports.AsyncFifoMutex = AsyncFifoMutex;
