/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{ignoreAbortErrors as e,createResolver as r}from"../../../../core/promiseUtils.js";import{QueueProcessor as s}from"../../../../core/QueueProcessor.js";import{UpdateTracking2D as t}from"./UpdateTracking2D.js";class i{constructor(){this.type="override-batch",this.messages=[],this._resovler=r()}get promise(){return this._resovler.promise}resolve(){this._resovler.resolve()}add(e){this.messages.push(e)}}class o{constructor(e){this.updateTracking=new t({debugName:"FeatureCommandQueue"}),this.process=e.process,this._queueProcessor=new s({concurrency:1,process:async e=>{if("override-batch"===e.type){const e=this._nextOverrideBatch;if(null==e)throw new Error("InternalError: Override should be defined");return this._nextOverrideBatch=null,await this.process(e),void e.resolve()}return this.process(e)}})}destroy(){this.updateTracking.destroy(),this._queueProcessor.destroy(),this.clear()}clear(){this._queueProcessor.clear()}async push(r){return e(this.updateTracking.addPromise(this._doPush(r)))}async _doPush(e){const r=this._queueProcessor,s=r.last();switch(e.type){case"update":case"highlight":if(s?.type===e.type)return;return r.push(e);case"override":case"edit":return this._pushOverride(e)}}_pushOverride(e){return null==this._nextOverrideBatch&&(this._nextOverrideBatch=new i,this._queueProcessor.push(this._nextOverrideBatch)),this._nextOverrideBatch.add(e),this._nextOverrideBatch.promise}}export{o as FeatureCommandQueue,i as OverrideBatch};
