var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
var Semaphore = class {
  constructor(e) {
    __publicField(this, "waiters", []);
    if (!Number.isInteger(e) || e < 1) throw Error(`Semaphore permits must be a positive integer, got ${e}`);
    this.maxPermits = e, this.permits = e;
  }
  get available() {
    return this.permits;
  }
  get pending() {
    return this.waiters.length;
  }
  acquire() {
    return this.permits > 0 ? (this.permits--, Promise.resolve()) : new Promise((e) => {
      this.waiters.push(e);
    });
  }
  release() {
    let e = this.waiters.shift();
    if (e) {
      e();
      return;
    }
    if (this.permits >= this.maxPermits) throw Error("Semaphore.release() called more times than acquire() \u2014 refusing to exceed initial permit count");
    this.permits++;
  }
  async run(e) {
    await this.acquire();
    try {
      return await e();
    } finally {
      this.release();
    }
  }
};
function mapWithConcurrency(t, n, r) {
  let i = new Semaphore(n);
  return t.length === 0 ? Promise.resolve([]) : Promise.all(t.map((e, t2) => i.run(() => r(e, t2))));
}
export {
  mapWithConcurrency as n,
  Semaphore as t
};
