/* 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{throwIfAborted as n,createAbortError as i}from"../../../../core/promiseUtils.js";import{QueueProcessor as c}from"../../../../core/QueueProcessor.js";import a from"../../../../core/ReactiveSet.js";import{property as r,subclass as o}from"../../../../core/accessorSupport/decorators.js";let l=class extends e{constructor(t){super(t),this._requestQueue=new c({concurrency:1,process:(t,e)=>this._processRequest(t,e)}),this._pendingRestarts=new a,this.pluginStack=new s}destroy(){this.cancelAll(),this._requestQueue.destroy()}get updating(){return this._requestQueue.running||this._requestQueue.full||this._pendingRestarts.size>0}get lastPlugin(){return this.pluginStack.at(-1)}begin(t,e,s=!0){return this._requestQueue.push({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;return this._requestQueue.push({action:"begin",target:s,toAttach:t,cancelExisting:!0})}cancel(t){return this._requestQueue.push({action:"cancel",target:t})}complete(t){return this._requestQueue.push({action:"complete",target:t})}cancelAll(){this._requestQueue.clear(),this.pluginStack.drain(t=>{t.destroy(),this.afterDetach(t)})}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,c=n>=0?e.at(n):null,{pluginState:a,currentInternalStep:r,sessionState:o}=i,l=s.startSession(t,o?.isConnected??!0,o?.geometryType??i.pluginConfiguration.geometryType,o),u=this.getContext(t,l);t.start(u,r??a??void 0),c?.attachPlugin?.(t)}finally{n.delete(t)}}}async _processRequest(t,e){switch(n(e),t.action){case"begin":return this._processBegin(t,e);case"cancel":return this._processCancel(t,e);case"complete":return this._processComplete(t,e)}}async _processBegin(t,e){const s=t.target,c=this.pluginStack;if(s){if(!c.includes(s))return;if(t.cancelExisting)for(;this.lastPlugin!==s&&c.includes(s);)this._processCancel({action:"cancel",target:this.lastPlugin},e),n(e);else if(this.lastPlugin!==s)return}const a=t.toAttach;for(const n of a){const t=await n.create();if(e.aborted)throw t.destroy(),i();const s=this.lastPlugin,c=s?.beforeAttachPlugin?.(n)?.useStandaloneSession??!1,a=this.editSessionManager.startSession(t,!c,n.geometryType,null);t.configuration=n;const r=this.getContext(t,a);t.start(r),this.pluginStack.push(t),s?.attachPlugin?.(t),this.afterAttach()}const r=this.lastPlugin;r?.configuration?.defaultDownstreamPlugin&&this._requestQueue.push({action:"begin",target:r,toAttach:[r.configuration.defaultDownstreamPlugin],cancelExisting:!1})}_processCancel({target:t},e){const s=this.pluginStack;for(;s.includes(t);)this._cancelInternal(this.lastPlugin)}async _processComplete({target:t},e){const s=this.pluginStack.indexOf(t);if(-1===s)return;if(s!==this.pluginStack.length-1){const n=t?.beforeComplete?.()?.downstreamPluginCompletionMode??"cancel",i=this.pluginStack.at(s+1);"cancel"===n?this._processCancel({action:"cancel",target:i},e):await this._processComplete({action:"complete",target:i},e)}n(e),await t.complete(e),this.pluginStack.removeAt(s);const i=this.lastPlugin;i?.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([r({constructOnly:!0})],l.prototype,"afterAttach",void 0),t([r({constructOnly:!0})],l.prototype,"afterDetach",void 0),t([r({constructOnly:!0})],l.prototype,"editSessionManager",void 0),t([r({constructOnly:!0})],l.prototype,"getContext",void 0),t([r()],l.prototype,"updating",null),t([r()],l.prototype,"lastPlugin",null),l=t([o("esri.views.draw.support.managers.PluginLifecycleManager")],l);export{l as PluginLifecycleManager};
