/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{__decorate as e}from"tslib";import t from"../../../core/Accessor.js";import{IdentifiableMixin as s}from"../../../core/Identifiable.js";import{isAbortError as r}from"../../../core/promiseUtils.js";import{watch as i,whenOnce as a}from"../../../core/reactiveUtils.js";import{property as o,subclass as l}from"../../../core/accessorSupport/decorators.js";import{findSpatialReference as n}from"../../../support/basemapUtils.js";let p=class extends(s(t)){constructor(e){super(e),this.compatibilityFunction=null,this.error=null,this.state="loading",this.view=null}initialize(){const e=()=>this.refresh();this.addHandles([i(()=>this.basemap?.loadStatus,e),i(()=>this.compatibilityFunction,e),i(()=>this.view&&"basemapTerrain"in this.view&&this.view.basemapTerrain?.tilingScheme,e),i(()=>this.view?.ready,e),i(()=>this.view?.spatialReference,e)]),this.refresh()}destroy(){this._cancelRefresh(),this.basemap=null,this.compatibilityFunction=null,this.view=null}get _spatialReferenceTask(){return n(this.view,this.basemap)}set basemap(e){e&&e.load().catch(()=>{}),this._set("basemap",e)}get spatialReference(){return this._spatialReferenceTask.spatialReference}refresh(){this._cancelRefresh(),this._set("state","loading");const e=this.basemap?.loadStatus;if("loaded"!==e&&"failed"!==e)return;if(!this.compatibilityFunction)return void("loaded"===e?(this._set("state","ready"),this._set("error",null)):(this._set("state","error"),this._set("error",this.basemap.loadError)));const t=new AbortController,{signal:s}=t;this.compatibilityFunction(this,{signal:s}).then(()=>a(()=>!this._spatialReferenceTask.updating,s)).then(()=>{this._set("state","ready"),this._set("error",null)}).catch(e=>{r(e)||(this._set("state","error"),this._set("error",e))}),this._refreshController=t}_cancelRefresh(){this._refreshController&&(this._refreshController.abort(),this._refreshController=null)}};e([o({readOnly:!0})],p.prototype,"_spatialReferenceTask",null),e([o()],p.prototype,"basemap",null),e([o()],p.prototype,"compatibilityFunction",void 0),e([o({readOnly:!0})],p.prototype,"error",void 0),e([o({readOnly:!0})],p.prototype,"spatialReference",null),e([o({readOnly:!0})],p.prototype,"state",void 0),e([o()],p.prototype,"view",void 0),p=e([l("esri.widgets.BasemapGallery.support.BasemapGalleryItem")],p);const h=p;export{h as default};
