/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__decorate as t}from"tslib";import e from"../../../../core/Accessor.js";import s from"../../../../core/Collection.js";import i from"../../../../core/ReactiveSet.js";import{property as n,subclass as c}from"../../../../core/accessorSupport/decorators.js";let o=class extends e{constructor(t){super(t),this._requestQueue=[],this._isProcessing=!1,this._pendingRestarts=new i,this.pluginStack=new s}destroy(){this.cancelAll(),this._requestQueue.length=0,this._isProcessing=!1}get updating(){return this._isProcessing||this._requestQueue.length>0||this._pendingRestarts.size>0}get lastPlugin(){return this.pluginStack.at(-1)}get test(){return{pendingRestarts:this._pendingRestarts}}begin(t,e,s=!0){this._queueRequest({action:"begin",target:e,toAttach:t,cancelExisting:s})}replaceLeaf(t){const e=this.pluginStack.items.findLastIndex(t=>!t.configuration?.hidden),s=this.pluginStack.at(e-1)||null;this._queueRequest({action:"begin",target:s,toAttach:t,cancelExisting:!0})}cancel(t){this._queueRequest({action:"cancel",target:t})}complete(t){this._queueRequest({action:"complete",target:t})}cancelAll(){this._requestQueue.length=0,this.pluginStack.drain(t=>{t.destroy(),this.afterDetach(t)}),this._pendingRestarts.clear()}suspend(t){this._pendingRestarts.add(t),this.editSessionManager.cancelSession(t)}resume(t){const{pluginStack:e,editSessionManager:s,_pendingRestarts:i}=this;for(const n of t){const t=n.pluginInstance;if(i.has(t))try{const i=e.indexOf(t)-1,c=i>=0?e.at(i):null,{pluginState:o,currentInternalStep:a,sessionState:r}=n,l=s.startSession(t,r?.isConnected??!0,r?.geometryType??n.pluginConfiguration.geometryType,r);this.beforeResume?.(t,l);const u=this.getContext(t,l);t.start(u,a??o??void 0),c?.attachPlugin?.(t)}finally{i.delete(t)}}}_queueRequest(t){this._requestQueue.push(t),this._processQueue()}_processQueue(){if(!this._isProcessing){this._isProcessing=!0;try{for(;this._requestQueue.length>0;){const t=this._requestQueue.shift();this._processRequest(t)}}finally{this._isProcessing=!1}}}_processRequest(t){switch(t.action){case"begin":return this._processBegin(t);case"cancel":return this._processCancel(t);case"complete":return this._processComplete(t)}}_processBegin(t){const e=t.target,s=this.pluginStack;if(e){if(!s.includes(e))return;if(t.cancelExisting)for(;this.lastPlugin!==e&&s.includes(e);)this._processCancel({action:"cancel",target:this.lastPlugin});else if(this.lastPlugin!==e)return}const i=t.toAttach;for(const c of i){const t=c.create(),e=this.lastPlugin,s=e?.beforeAttachPlugin?.(c)?.useStandaloneSession??!1,i=this.editSessionManager.startSession(t,!s,c.geometryType,null);t.configuration=c;const n=this.getContext(t,i);t.start(n),this.pluginStack.push(t),e?.attachPlugin?.(t),this.afterAttach()}const n=this.lastPlugin;n?.configuration?.defaultDownstreamPlugin&&this._queueRequest({action:"begin",target:n,toAttach:[n.configuration.defaultDownstreamPlugin],cancelExisting:!1})}_processCancel({target:t}){const e=this.pluginStack;for(;e.includes(t);)this._cancelInternal(this.lastPlugin)}_processComplete({target:t}){const e=this.pluginStack.indexOf(t);if(-1===e)return;if(e!==this.pluginStack.length-1){const s=t?.beforeComplete?.()?.downstreamPluginCompletionMode??"cancel",i=this.pluginStack.at(e+1);"cancel"===s?this._processCancel({action:"cancel",target:i}):this._processComplete({action:"complete",target:i})}t.complete(),this.pluginStack.removeAt(e);const s=this.lastPlugin;s?.detachPlugin?.(t,"complete"),this.afterDetach?.(t),t?.destroy()}_cancelInternal(t){this.editSessionManager.cancelSession(t);const e=this.pluginStack.indexOf(t);this.pluginStack.remove(t);const s=this.pluginStack.at(e-1);s?.detachPlugin?.(t,"cancel"),this.afterDetach?.(t),t?.cancel?.(),t?.destroy()}};t([n()],o.prototype,"_isProcessing",void 0),t([n({constructOnly:!0})],o.prototype,"afterAttach",void 0),t([n({constructOnly:!0})],o.prototype,"afterDetach",void 0),t([n({constructOnly:!0})],o.prototype,"beforeResume",void 0),t([n({constructOnly:!0})],o.prototype,"editSessionManager",void 0),t([n({constructOnly:!0})],o.prototype,"getContext",void 0),t([n()],o.prototype,"updating",null),t([n()],o.prototype,"lastPlugin",null),o=t([c("esri.views.draw.support.managers.PluginLifecycleManager")],o);export{o as PluginLifecycleManager};
