/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__decorate as e}from"tslib";import a from"../../core/Accessor.js";import{watch as t,initial as s,whenOnce as i}from"../../core/reactiveUtils.js";import{property as n,subclass as p}from"../../core/accessorSupport/decorators.js";import{isLoaded as r,canProjectWithoutEngine as o,load as l}from"../../geometry/projectionUtils.js";import{equals as c}from"../../geometry/support/spatialReferenceUtils.js";import{basemapEnsureType as m,destroyCache as h}from"../../support/basemapEnsureType.js";import{findSpatialReference as f,getBasemapThumbnailUrl as d}from"../../support/basemapUtils.js";let u=class extends a{constructor(e){super(e),this._basemapCache={},this._loadingProjectionEngine=!1,this.view=null,this.nextBasemap=m("hybrid",this._basemapCache)}initialize(){t(()=>this.nextBasemap,e=>{e&&!e.loaded&&e.load().catch(()=>{})},s)}destroy(){this.view=null,h(this._basemapCache),this._basemapCache=null}get _nextBasemapSpatialReferenceTask(){return f(this.view,this.nextBasemap)}get _viewSpatialReferenceLocked(){const{view:e}=this;return!e||!("spatialReferenceLocked"in e)||e.spatialReferenceLocked}get activeBasemap(){return m(this.view?.map?.basemap??"topo-vector",this._basemapCache)}get nextBasemap(){return this._get("nextBasemap")}set nextBasemap(e){this._set("nextBasemap",m(e,this._basemapCache))}get state(){const{view:e}=this;if(!e?.ready)return"disabled";if(this._nextBasemapSpatialReferenceTask.updating)return"disabled";const{spatialReference:a}=this._nextBasemapSpatialReferenceTask;return this._viewSpatialReferenceLocked&&null!=a&&!e.spatialReference.equals(a)?"incompatible-next-basemap":this._loadingProjectionEngine?"loading":"ready"}async toggle(){const{activeBasemap:e,nextBasemap:a,state:t,view:s}=this;if(!s||"disabled"===t||"incompatible-next-basemap"===t)return;const n=this._viewSpatialReferenceLocked;if(!n){if(await i(()=>!this._nextBasemapSpatialReferenceTask.updating),a!==this.nextBasemap||e!==this.activeBasemap)return;const{spatialReference:t}=this._nextBasemapSpatialReferenceTask;if(null==t||c(s.spatialReference,t)||r()||o(s.spatialReference,t)||(this._loadingProjectionEngine=!0,await l(),this._loadingProjectionEngine=!1),a!==this.nextBasemap||e!==this.activeBasemap)return}s.map.basemap=a,n||null==this._nextBasemapSpatialReferenceTask.spatialReference||c(s.spatialReference,this._nextBasemapSpatialReferenceTask.spatialReference)||(s.spatialReference=this._nextBasemapSpatialReferenceTask.spatialReference),this.nextBasemap=e}static getThumbnailUrl(e){return d(e)}};e([n()],u.prototype,"_loadingProjectionEngine",void 0),e([n({readOnly:!0})],u.prototype,"_nextBasemapSpatialReferenceTask",null),e([n({readOnly:!0})],u.prototype,"_viewSpatialReferenceLocked",null),e([n({readOnly:!0})],u.prototype,"activeBasemap",null),e([n()],u.prototype,"nextBasemap",null),e([n({readOnly:!0})],u.prototype,"state",null),e([n()],u.prototype,"view",void 0),e([n()],u.prototype,"toggle",null),u=e([p("esri.widgets.BasemapToggle.BasemapToggleViewModel")],u);const g=u;export{g as default};
