/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../../core/Collection.js";import{Loadable as i,isLoadable as s}from"../../core/Loadable.js";import{watch as a,on as r,when as o}from"../../core/reactiveUtils.js";import{property as n,subclass as p}from"../../core/accessorSupport/decorators.js";import{canProjectWithoutEngine as c,isLoaded as m,load as l}from"../../geometry/projectionUtils.js";import{equals as u}from"../../geometry/support/spatialReferenceUtils.js";import{isLinkChartView as d}from"../../linkChart/utils.js";import{basemapEnsureType as h}from"../../support/basemapEnsureType.js";import{contentEquals as f}from"../../support/basemapUtils.js";import{default3DCompatibility as y,default2DCompatibility as v}from"./support/basemapCompatibilityUtils.js";import b from"./support/BasemapGalleryItem.js";import g from"./support/LocalBasemapsSource.js";import w from"./support/PortalBasemapsSource.js";const _=t.ofType(b);function B(e){return e&&"esri.portal.Portal"===e.declaredClass}function j(e){return e&&!(e instanceof w)&&(!!e.portal||!!e.query)}function I(e){return e&&"basemaps"in e&&"state"in e&&"refresh"in e}let R=class extends i{constructor(e){super(e),this._loadingProjectionEngine=!1,this.items=new _,this.view=null,this.source=new w}initialize(){const e=()=>this._recreateItems();this.addHandles([a(()=>"ready"===this.state?this.compatibilityFunction:null,()=>this._updateItems()),r(()=>this.source?.basemaps,"change",e,{onListenerAdd:e}),o(()=>this.view,()=>{this.source instanceof w&&(this.source.viewType=this.view?.type)},{once:!0})])}destroy(){const e=this.source.basemaps.find(e=>e===this.activeBasemap);e&&this.source.basemaps.remove(e),this.source?.destroy()}get activeBasemap(){return this.view?.map?.basemap??null}set activeBasemap(e){const t=this.view;if(!t?.map)return;if(!(e=h(e))||!t.ready)return t.map.basemap=e,void this._clearOverride("activeBasemap");const i=e.spatialReference||this.items?.find(t=>this.basemapEquals(e,t.basemap))?.spatialReference;if(i&&"spatialReferenceLocked"in t&&!t.spatialReferenceLocked){const s=t.spatialReference;if(null!=i&&!u(s,i)&&!c(t.spatialReference,i)&&!m())return this._override("activeBasemap",e),this._loadingProjectionEngine=!0,void l().then(()=>{this._get("activeBasemap")===e&&(t.map.basemap=e,t.spatialReference=i,this._clearOverride("activeBasemap"))},()=>{}).then(()=>{this._loadingProjectionEngine=!1});t.map.basemap=e,this._clearOverride("activeBasemap"),null==i||u(t.spatialReference,i)||(t.spatialReference=i)}else t.map.basemap=e,this._clearOverride("activeBasemap")}get activeBasemapIndex(){const{state:e,activeBasemap:t}=this;return"ready"!==e?-1:this._findBasemapIndex(t)}get compatibilityFunction(){return"3d"===this.view?.type?y:v}set compatibilityFunction(e){this._overrideIfSome("compatibilityFunction",e)}get source(){return this._get("source")}set source(e){(Array.isArray(e)||t.isCollection(e))&&(e=new g({basemaps:Array.isArray(e)?new t(e):e})),B(e)&&(e=new w({portal:e})),j(e)&&(e=new w(e)),I(e)||(e=null),this._set("source",e)}get state(){return this.view?.ready&&this.source?d(this.view)&&!this.view.inGeographicLayout?"unsupported":this._loadingProjectionEngine?"loading":"ready":"disabled"}basemapEquals(e,t){return f(e,t)}refresh(){this._recreateItems()}load(){return this.loadSource()}loadSource(e){return this.addResolvingPromise(s(this.source)?this.source.load(e):null),Promise.resolve(this)}_findBasemapIndex(e){const{items:t}=this,i=t.findIndex(t=>t.basemap===e);return-1===i?t.findIndex(t=>this.basemapEquals(t.basemap,e)):i}_recreateItems(){const e=this.source?.basemaps??[],{view:t,compatibilityFunction:i}=this,s=new Map(this.items.map(e=>[e.basemap,e])),a=e.map(e=>{const a=s.get(e);return a?(s.delete(e),a):new b({basemap:e,compatibilityFunction:i,view:t})});this.items.removeAll(),this.items.addMany(a),s.forEach(e=>e.destroy())}_updateItems(){for(const e of this.items)e.compatibilityFunction=this.compatibilityFunction,e.view=this.view}};e([n()],R.prototype,"_loadingProjectionEngine",void 0),e([n()],R.prototype,"activeBasemap",null),e([n({readOnly:!0})],R.prototype,"activeBasemapIndex",null),e([n()],R.prototype,"compatibilityFunction",null),e([n({readOnly:!0,type:_})],R.prototype,"items",void 0),e([n()],R.prototype,"source",null),e([n({readOnly:!0})],R.prototype,"state",null),e([n()],R.prototype,"view",void 0),R=e([p("esri.widgets.BasemapGallery.BasemapGalleryViewModel")],R);const E=R;export{E as default};
