function c(i){return"data"in i}function a(i){return"execute"in i}class o{#t=[];#e=null;#s;#i;constructor(t){this.#c(t),this.#s=t.batchSize,this.#i=t.batchProcessor}async addItem(t){return new Promise((s,h)=>{if(a(t)){const r={execute:async(...n)=>{try{const e=await t.execute(...n);return s(e),e}catch(e){throw console.error("Failed to execute item:",e),h(e),e}}};this.#t.push(r)}else if(c(t))this.#t.push(t),s(t.data);else{h(new Error("Invalid item type"));return}this.#h()})}#h(){this.#e||this.#t.length===0||this.#s&&this.#t.length<this.#s||(this.#e=this.#n().catch(t=>{console.error("Batch queue processing failed:",t)}).finally(()=>{this.#e=null,this.#t.length>0&&this.#h()}))}async#n(){for(;this.#t.length>=(this.#s??1);)await this.#r()}async processRemainingItems(){for(this.#e&&await this.#e;this.#t.length>0;)await this.#r()}async#r(){const t=this.#s??this.#t.length,s=this.#t.splice(0,t);s.length&&await this.#i(s)}#c(t){if(t.batchSize&&t.batchSize<=0)throw new Error("Batch size must be greater than 0")}get isProcessing(){return this.#e!==null}}export{o as BatchQueue,c as isDataItem,a as isExecutableItem};
