/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__decorate as t}from"tslib";import s from"./core/Accessor.js";import i from"./core/Collection.js";import{whenOnce as o}from"./core/reactiveUtils.js";import{property as a,subclass as n}from"./core/accessorSupport/decorators.js";import{UpdatingHandles as e}from"./core/support/UpdatingHandles.js";import{UndoInvalidError as r,UndoRedoUpdatingError as c,RedoInvalidError as d}from"./undoredo/UndoRedoError.js";let h=class extends s{constructor(){super(...arguments),this._stack=new i,this._stackPosition=-1,this._updatingHandles=new e}get updating(){return this._updatingHandles.updating}get canUndo(){return this.hasUndo&&!this.updating}get hasUndo(){return this._stackPosition>=0}get canRedo(){return this.hasRedo&&!this.updating}get hasRedo(){return this._stackPosition<this._stack.length-1}_truncateForwardStack(){this._stack.splice(this._stackPosition+1,this._stack.length-this._stackPosition).forEach(t=>t.destroy())}_drainStack(){this._stack.drain(t=>t.destroy()),this._stackPosition=-1}async undo(){if(!this.hasUndo)throw new r;if(this.updating)throw new c;const t=this._stack.getItemAt(this._stackPosition);t&&await this._updatingHandles.addPromise((async()=>{await t.executeUndoRedoOperation(1),--this._stackPosition,t.canRedo||this._truncateForwardStack()})())}async redo(){if(!this.hasRedo)throw new d;if(this.updating)throw new c;const t=this._stack.getItemAt(this._stackPosition+1);if(!t)throw new d;await this._updatingHandles.addPromise((async()=>{await t.executeUndoRedoOperation(2),++this._stackPosition})())}peekUndo(){if(this.canUndo)return this._stack.getItemAt(this._stackPosition)}peekRedo(){if(this.canRedo)return this._stack.getItemAt(this._stackPosition+1)}async inject(t){if(this.updating)throw new c;await this._updatingHandles.addPromise((async()=>{0===t.status&&await t.executeUndoRedoOperation(0),t.canUndo?(this._stack.splice(this._stackPosition+1,0,t),this._stackPosition++):this._stackPosition>-1&&(this._stack.splice(0,this._stackPosition+1).forEach(t=>t.destroy()),this._stackPosition=-1)})())}async add(t){if(this.updating)throw new c;await this._updatingHandles.addPromise((async()=>{0===t.status&&await t.executeUndoRedoOperation(0),this._stackPosition>=-1&&this._truncateForwardStack(),t.canUndo?(this._stack.push(t),this._stackPosition=this._stack.length-1):this._drainStack()})())}async removeTagged(t,s=!1){if(this.updating&&!s)return;await o(()=>!this.updating);const a=new i;for(let i=0;i<this._stack.length;i++){const s=this._stack.getItemAt(i);s&&(s.tag===t?(s.destroy(),i===this._stackPosition&&(this._stackPosition=a.length-1)):a.push(s))}this._stack=a,this._stackPosition>a.length-1&&(this._stackPosition=a.length-1)}async clear(t=!1){if(this.updating&&!t)throw new c;await o(()=>!this.updating),this._drainStack()}};t([a()],h.prototype,"_stack",void 0),t([a()],h.prototype,"_stackPosition",void 0),t([a()],h.prototype,"updating",null),t([a({readOnly:!0})],h.prototype,"canUndo",null),t([a({readOnly:!0})],h.prototype,"hasUndo",null),t([a({readOnly:!0})],h.prototype,"canRedo",null),t([a({readOnly:!0})],h.prototype,"hasRedo",null),h=t([n("esri.UndoRedo")],h);export{h as UndoRedo};
