import{InvalidOperationError as t}from"../InvalidOperationError.js";import{checkArg as i}from"./checkArg.js";export class AsyncQueue{get isDone(){return this._isDone}_isDone=!1;_rejectReason;_items=[];_signalReady;_ready=new Promise((t=>{this._signalReady=t}));async*[Symbol.asyncIterator](){let t=0;for(;t<this._items.length||!this.isDone;){t===this._items.length&&await this._ready;const i=t;t=this._items.length,yield*from(this._items.slice(i))}if(this._rejectReason)throw this._rejectReason}done(){this._isDone=!0,this._signalReady()}reject(t){this._rejectReason||(this._rejectReason=t),this.done()}add(t){this.addMany([t])}addMany(i){if(this.isDone)throw new t("Cannot add more items to the queue once done() is called.");this._items.push(...i);const e=this._signalReady;this._ready=new Promise((t=>{this._signalReady=t})),e()}}export function from(t,s="default"){return i("iterable",t).isNotMissing(),{async*[Symbol.asyncIterator](){if("default"===s)for(const i of await t)e(i)?yield*i:yield await i;else{const i=Array.from(await t).map((t=>e(t)?[t,t[Symbol.asyncIterator]()]:[t,void 0])),s=i.map((t=>e(t[0])?t[1].next():t[0]));for(;i.length;){const[t]=await Promise.race(s.map((async t=>(await t,[t]))));if(null==t)continue;const o=await t,n=s.indexOf(t);if(e(i[n][0])){const t=o;t.done?(i.splice(n,1),s.splice(n,1)):s.splice(n,1,i[n][1].next()),void 0!==t.value&&(yield t.value)}else i.splice(n,1),s.splice(n,1),yield o}}}}}function e(t){return"function"==typeof t[Symbol.asyncIterator]}export async function*empty(){}export function fromSingleItem(t){return{async*[Symbol.asyncIterator](){yield await t}}}export async function toArray(t){const i=[];for await(const e of t)i.push(e);return i}export function map(t,e){return i("iterable",t).isNotMissing(),i("f",e).isNotMissing(),{async*[Symbol.asyncIterator](){let i=0;for await(const s of t)yield await e(s,i++)}}}export function concat(t,...e){return i("iterator",t).isNotMissing(),{async*[Symbol.asyncIterator](){yield*t;for(const t of e)"object"==typeof t&&t[Symbol.asyncIterator]||"object"==typeof t&&t[Symbol.iterator]?yield*t:yield t}}}export async function every(t,e){i("iterable",t).isNotMissing(),i("p",e).isNotMissing();let s=0;for await(const i of t)if(!await e(i,s++))return!1;return!0}export function filter(t,e){return{async*[Symbol.asyncIterator](){i("iterable",t).isNotMissing(),i("p",e).isNotMissing();let s=0;for await(const i of t)await e(i,s++)&&(yield i)}}}export async function find(t,e){i("iterable",t).isNotMissing(),i("p",e).isNotMissing();let s=0;for await(const i of t)if(await e(i,s++))return i}export async function first(t){return i("iterable",t).isNotMissing(),(await t[Symbol.asyncIterator]().next()).value}export function flatten(t){return{async*[Symbol.asyncIterator](){i("iterables",t).isNotMissing();for await(const i of t)yield*i}}}export async function forEach(t,e){i("iterable",t).isNotMissing(),i("f",e).isNotMissing();let s=0;for await(const i of t)await e(i,s++)}export async function some(t,e){i("it",t).isNotMissing(),i("p",e).isNotMissing();let s=0;for await(const i of t)if(await e(i,s++))return!0;return!1}export function take(t,e){return{async*[Symbol.asyncIterator](){i("iterable",t).isNotMissing(),i("n",e).isNotMissing();let s=0;for await(const i of t){if(s>=e)break;yield i,s++}}}}