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