/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import e from"../../core/Error.js";import{removeMaybe as r}from"../../core/maybe.js";import{createResolver as t,onAbort as i,throwIfAborted as l}from"../../core/promiseUtils.js";import a from"../../layers/Layer.js";function o(e){return"tryRecycleWith"in e}class s{constructor(r,l,a){this.layer=r,this.view=l,this.layerViewImporter=a,this._controller=new AbortController,this._started=!1,this.done=!1,this._deferred=t(),this.promise=this._deferred.promise,i(this._controller.signal,()=>{this._recycleController?.abort();const t=new e("cancelled:layerview-create","layerview creation cancelled",{layer:r});this._deferred?.reject(t)})}tryRecycle(e){if(!this.done||!this.layerView||!o(this.layerView))return null;this._recycleController?.abort(),this._recycleController=new AbortController;const r=this.layer.type,t=this._recycleController.signal;for(let i=0;i<e.length;i++){const a=e[i];if(a.type!==r)continue;const o=this.layerView.tryRecycleWith(a,{signal:t});if(o){e.splice(i,1),this.layer=a;const r=this.layerView,s=r.view;return this.promise=o.then(()=>(l(t),a.emit("layerview-destroy",{view:s,layerView:r}),s.emit("layerview-destroy",{view:s,layerView:r,layer:void 0}),a.emit("layerview-create",{view:s,layerView:r}),s.emit("layerview-create",{view:s,layerView:r,layer:void 0}),r)),this.promise}}return null}destroy(){this._controller.abort();const{layerView:e}=this;if(e){const{layer:r,view:t}=this;r.emit("layerview-destroy",{view:t,layerView:e}),t.emit("layerview-destroy",{layer:r,layerView:e})}this.done=!0,this.layer=null,this.layerView=null,this.view=null,this.layerViewImporter=null,this._map=null,this.promise=null,this._deferred=null}async start(){const{view:t}=this;if(this._started||!t.map)return;this._started=!0;const{_controller:{signal:a},layer:o}=this;this._map=t.map;try{let n,w;if(await o.load({signal:a}),o.prefetchResources&&await o.prefetchResources({signal:a}),y(o))n=await o.createLayerView(t,{signal:a});else{if(!this.layerViewImporter.hasLayerViewModule(o))throw new e("layer:view-not-supported","No layerview implementation was found");const r=await this.layerViewImporter.importLayerView(o);l(a),n="default"in r?new r.default({layer:o,view:t}):new r({layer:o,view:t})}const c=()=>{w=r(w),n.destroyed||n.destroy(),this.done=!0};w=i(a,c),l(a);try{await n.when()}catch(s){throw c(),s}const h=this._map?.allLayers?.includes(o);if(!h)return c(),void this._deferred?.reject(new e("view:no-layerview-for-layer","The layer has been removed from the map",{layer:o}));this.layerView=n,o.emit("layerview-create",{view:t,layerView:n}),t.emit("layerview-create",{layer:o,layerView:n}),this.done=!0,this._deferred?.resolve(n)}catch(s){o.emit("layerview-create-error",{view:t,error:s}),t.emit("layerview-create-error",{layer:o,error:s}),this.done=!0,this._deferred?.reject(new e("layerview:create-error","layerview creation failed",{layer:o,error:s}))}}}function y(e){return e.createLayerView!==a.prototype.createLayerView}export{s as LayerLayerViewInfo};
