/** Run async work with a fixed concurrency cap while preserving input order. */
export async function mapWithConcurrency(items, limit, worker) {
    const concurrency = Math.max(1, Math.floor(limit));
    const results = new Array(items.length);
    let nextIndex = 0;
    async function runWorker() {
        for (;;) {
            const index = nextIndex++;
            if (index >= items.length)
                return;
            results[index] = await worker(items[index], index);
        }
    }
    const workers = Array.from({ length: Math.min(concurrency, items.length) }, () => runWorker());
    await Promise.all(workers);
    return results;
}
