var f=class{value;next;constructor(t){this.value=t}},a=class{#e;#t;#i;constructor(){this.clear()}enqueue(t){let i=new f(t);this.#e?(this.#t.next=i,this.#t=i):(this.#e=i,this.#t=i),this.#i++}dequeue(){let t=this.#e;if(t)return this.#e=this.#e.next,this.#i--,this.#e||(this.#t=void 0),t.value}peek(){if(this.#e)return this.#e.value}clear(){this.#e=void 0,this.#t=void 0,this.#i=0}get size(){return this.#i}*[Symbol.iterator](){let t=this.#e;for(;t;)yield t.value,t=t.next}*drain(){for(;this.#e;)yield this.dequeue()}};function x(e){let t=!1;if(typeof e=="object"&&({concurrency:e,rejectOnClear:t=!1}=e),c(e),typeof t!="boolean")throw new TypeError("Expected `rejectOnClear` to be a boolean");let i=new a,o=0,h=()=>{o<e&&i.size>0&&(o++,i.dequeue().run())},m=()=>{o--,h()},p=async(s,r,u)=>{let n=(async()=>s(...u))();r(n);try{await n}catch{}m()},b=(s,r,u,n)=>{let l={reject:u};new Promise(w=>{l.run=w,i.enqueue(l)}).then(p.bind(void 0,s,r,n)),o<e&&h()},d=(s,...r)=>new Promise((u,n)=>{b(s,u,n,r)});return Object.defineProperties(d,{activeCount:{get:()=>o},pendingCount:{get:()=>i.size},clearQueue:{value(){if(!t){i.clear();return}let s=AbortSignal.abort().reason;for(;i.size>0;)i.dequeue().reject(s)}},concurrency:{get:()=>e,set(s){c(s),e=s,queueMicrotask(()=>{for(;o<e&&i.size>0;)h()})}},map:{async value(s,r){let u=Array.from(s,(n,l)=>this(r,n,l));return Promise.all(u)}}}),d}function c(e){if(!((Number.isInteger(e)||e===Number.POSITIVE_INFINITY)&&e>0))throw new TypeError("Expected `concurrency` to be a number from 1 and up")}export{x as a};
