import{logger as a}from"../tools/notifiers/logger.js";function c(i){return"data"in i}function o(i){return"execute"in i}class l{#t=[];#e=null;#s;#i;constructor(t){this.#a(t),this.#s=t.batchSize,this.#i=t.batchProcessor}async addItem(t){return new Promise((e,h)=>{if(o(t)){const r={execute:async(...n)=>{try{const s=await t.execute(...n);return e(s),s}catch(s){throw h(s),s}}};this.#t.push(r)}else if(c(t))this.#t.push(t),e(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=>{a.error(`Batch queue processing failed: ${t.message}`)}).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,e=this.#t.splice(0,t);e.length&&await this.#i(e)}#a(t){if(t.batchSize&&t.batchSize<=0)throw new Error("Batch size must be greater than 0")}get isProcessing(){return this.#e!==null}}export{l as BatchQueue,c as isDataItem,o as isExecutableItem};
