/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as t}from"tslib";import e from"../../../../core/Accessor.js";import s from"../../../../core/Collection.js";import n from"../../../../core/ReactiveSet.js";import{property as i,subclass as o}from"../../../../core/accessorSupport/decorators.js";let c=class extends e{constructor(t){super(t),this._requestQueue=[],this._isProcessing=!1,this._pendingRestarts=new n,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:n}=this;for(const i of t){const t=i.pluginInstance;if(n.has(t))try{const n=e.indexOf(t)-1,o=n>=0?e.at(n):null,{pluginState:c,currentInternalStep:a,sessionState:r}=i,l=s.startSession(t,r?.isConnected??!0,r?.geometryType??i.pluginConfiguration.geometryType,r);this.beforeResume?.(t,l);const u=this.getContext(t,l);t.start(u,a??c??void 0),o?.attachPlugin?.(t)}finally{n.delete(t)}}}gatherCompletablePlugins(){const t=this.pluginStack;if(!t.length)return[];const e=[t.at(0)];for(let s=1;s<this.pluginStack.length;s++){const n=t.at(s-1),i=t.at(s);if(i){if("complete"!==(n?.beforeComplete?.()?.downstreamPluginCompletionMode??"cancel"))break;e.push(i)}}return e}_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 n=t.toAttach;for(const o of n){const t=o.create(),e=this.lastPlugin,s=e?.beforeAttachPlugin?.(o)?.useStandaloneSession??!1,n=this.editSessionManager.startSession(t,!s,o.geometryType,null);t.configuration=o;const i=this.getContext(t,n);t.start(i),this.pluginStack.push(t),e?.attachPlugin?.(t),this.afterAttach()}const i=this.lastPlugin;i?.configuration?.defaultDownstreamPlugin&&this._queueRequest({action:"begin",target:i,toAttach:[i.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",n=this.pluginStack.at(e+1);"cancel"===s?this._processCancel({action:"cancel",target:n}):this._processComplete({action:"complete",target:n})}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([i()],c.prototype,"_isProcessing",void 0),t([i({constructOnly:!0})],c.prototype,"afterAttach",void 0),t([i({constructOnly:!0})],c.prototype,"afterDetach",void 0),t([i({constructOnly:!0})],c.prototype,"beforeResume",void 0),t([i({constructOnly:!0})],c.prototype,"editSessionManager",void 0),t([i({constructOnly:!0})],c.prototype,"getContext",void 0),t([i()],c.prototype,"updating",null),t([i()],c.prototype,"lastPlugin",null),c=t([o("esri.views.draw.support.managers.PluginLifecycleManager")],c);export{c as PluginLifecycleManager};
