// Simple queue that processes items sequentially
export class AsyncQueue {
    constructor(processFn) {
        Object.defineProperty(this, "queue", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: []
        });
        Object.defineProperty(this, "processing", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: false
        });
        Object.defineProperty(this, "processFn", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.processFn = processFn;
    }
    enqueue(item) {
        return new Promise((resolve, reject) => {
            this.queue.push({ item, resolve, reject });
            this.processQueue();
        });
    }
    async processQueue() {
        if (this.processing) {
            return;
        }
        this.processing = true;
        try {
            while (this.queue.length > 0) {
                const queueItem = this.queue.shift();
                if (!queueItem)
                    continue;
                try {
                    const result = await this.processFn(queueItem.item);
                    queueItem.resolve(result);
                }
                catch (error) {
                    queueItem.reject(error);
                }
            }
        }
        finally {
            this.processing = false;
            if (this.queue.length > 0) {
                this.processQueue();
            }
        }
    }
}
