//#region ../chokidar/dist/esm/utils/pLimit.mjs
var Node = class {
	value;
	next;
	constructor(value) {
		this.value = value;
	}
};
var Queue = class {
	#head;
	#tail;
	#size;
	constructor() {
		this.clear();
	}
	enqueue(value) {
		const node = new Node(value);
		if (this.#head) {
			this.#tail.next = node;
			this.#tail = node;
		} else {
			this.#head = node;
			this.#tail = node;
		}
		this.#size++;
	}
	dequeue() {
		const current = this.#head;
		if (!current) return;
		this.#head = current.next;
		this.#size--;
		return current.value;
	}
	peek() {
		if (!this.#head) return;
		return this.#head.value;
	}
	clear() {
		this.#head = void 0;
		this.#tail = void 0;
		this.#size = 0;
	}
	get size() {
		return this.#size;
	}
	*[Symbol.iterator]() {
		let current = this.#head;
		while (current) {
			yield current.value;
			current = current.next;
		}
	}
	*drain() {
		while (this.#head) yield this.dequeue();
	}
};
const pLimit = (concurrency) => {
	validateConcurrency(concurrency);
	const queue = new Queue();
	let activeCount = 0;
	const idleWaiters = [];
	const notifyIdleIfNeeded = () => {
		if (activeCount === 0 && queue.size === 0) while (idleWaiters.length) idleWaiters.pop()?.();
	};
	const resumeNext = () => {
		if (activeCount < concurrency && queue.size > 0) {
			activeCount++;
			queue.dequeue()?.();
		}
	};
	const next = () => {
		activeCount--;
		resumeNext();
	};
	const run = async (fn, resolve, arguments_) => {
		const result = (async () => fn(...arguments_))();
		resolve(result);
		try {
			await result;
		} catch {}
		next();
	};
	const enqueue = (fn, resolve, arguments_) => {
		new Promise((internalResolve) => {
			queue.enqueue(internalResolve);
		}).then(run.bind(void 0, fn, resolve, arguments_));
		if (activeCount < concurrency) resumeNext();
	};
	const generator = (fn, ...arguments_) => new Promise((resolve) => {
		enqueue(fn, resolve, arguments_);
	});
	Object.defineProperties(generator, {
		activeCount: { get: () => activeCount },
		pendingCount: { get: () => queue.size },
		clearQueue: { value() {
			queue.clear();
			notifyIdleIfNeeded();
		} },
		concurrency: {
			get: () => concurrency,
			set(newConcurrency) {
				validateConcurrency(newConcurrency);
				concurrency = newConcurrency;
				queueMicrotask(() => {
					while (activeCount < concurrency && queue.size > 0) resumeNext();
				});
			}
		},
		map: { async value(array, fn) {
			const promises = array.map((value, index) => this(fn, value, index));
			return Promise.all(promises);
		} },
		onIdle: { value() {
			if (activeCount === 0 && queue.size === 0) return Promise.resolve();
			return new Promise((resolve) => idleWaiters.push(resolve));
		} }
	});
	return generator;
};
const validateConcurrency = (concurrency) => {
	if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) throw new TypeError("Expected `concurrency` to be a number from 1 and up");
};

//#endregion
export { pLimit };
//# sourceMappingURL=pLimit.mjs.map