/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../chunks/_rollupPluginBabelHelpers","../../chunks/tslib.es6","../../Basemap","../../core/Collection","../../core/HandleOwner","../../core/Loadable","../../core/maybe","../../core/reactiveUtils","../../core/accessorSupport/decorators/property","../../core/arrayUtils","../../core/has","../../core/accessorSupport/decorators/cast","../../core/accessorSupport/decorators/subclass","../../geometry/projection","../../geometry/support/spatialReferenceUtils","../../support/basemapUtils","./support/basemapCompatibilityUtils","./support/BasemapGalleryItem","./support/LocalBasemapsSource","./support/PortalBasemapsSource"],(function(e,t,i,a,r,s,o,n,c,l,p,u,d,m,h,v,y,f,_,b){"use strict";const w=a.ofType(f);function g(e){return e&&"esri.portal.Portal"===e.declaredClass}function B(e){return e&&!(e instanceof b)&&(!!e.portal||!!e.query)}function P(e){return e&&"basemaps"in e&&"state"in e&&"refresh"in e}let R=function(t){function r(e){var i;return(i=t.call(this,e)||this)._loadingProjectionEngine=!1,i.items=new w,i.source=new b,i.view=null,i}e._inheritsLoose(r,t);var c=r.prototype;return c.initialize=function(){const e=()=>this._recreateItems();this.handles.add([n.watch((()=>"ready"===this.state?this.compatibilityFunction:null),(()=>this._updateItems())),n.on((()=>{var e;return null==(e=this.source)?void 0:e.basemaps}),"change",e,{onListenerAdd:e})])},c.castSource=function(e){return Array.isArray(e)||a.isCollection(e)?new _({basemaps:e}):g(e)?new b({portal:e}):B(e)?new b(e):P(e)?e:null},c.basemapEquals=function(e,t){return v.contentEquals(e,t)},c.refresh=function(){this._recreateItems()},c.load=function(e){return this.addResolvingPromise(s.isLoadable(this.source)?this.source.load(e):null),Promise.resolve(this)},c._recreateItems=function(){var e;const t=null==(e=this.source)?void 0:e.basemaps,{view:i,compatibilityFunction:a}=this;this.items.removeAll().forEach((e=>e.destroy())),t&&this.items.addMany(t.map((e=>new f({basemap:e,compatibilityFunction:a,view:i}))))},c._updateItems=function(){for(const e of this.items)e.compatibilityFunction=this.compatibilityFunction,e.view=this.view},e._createClass(r,[{key:"activeBasemap",get:function(){var e,t,i;return null!=(e=null==(t=this.view)||null==(i=t.map)?void 0:i.basemap)?e:null},set:function(e){var t,a;if(!this.view.map)return;const r="string"==typeof e?i.fromId(e):e;if(!r||!this.view.ready)return this.view.map.basemap=r,void this._clearOverride("activeBasemap");const s=r.spatialReference||(null==(t=this.items)||null==(a=t.find((e=>this.basemapEquals(r,e.basemap))))?void 0:a.spatialReference);if(s&&"spatialReferenceLocked"in this.view&&!this.view.spatialReferenceLocked){const t=this.view.spatialReference;if(o.isSome(s)&&!h.equals(t,s)&&!m.canProjectWithoutEngine(this.view.spatialReference,s)&&!m.isLoaded())return this._override("activeBasemap",r),this._loadingProjectionEngine=!0,void m.load().then((()=>{this._get("activeBasemap")===e&&(this.view.map.basemap=e,this.view.spatialReference=s,this._clearOverride("activeBasemap"))}),(()=>{})).then((()=>{this._loadingProjectionEngine=!1}));this.view.map.basemap=r,this._clearOverride("activeBasemap"),o.isSome(s)&&!h.equals(this.view.spatialReference,s)&&(this.view.spatialReference=s)}else this.view.map.basemap=r,this._clearOverride("activeBasemap")}},{key:"compatibilityFunction",get:function(){var e;return"3d"===(null==(e=this.view)?void 0:e.type)?y.default3DCompatibility:y.default2DCompatibility},set:function(e){o.isSome(e)?this._override("compatibilityFunction",e):this._clearOverride("compatibilityFunction")}},{key:"state",get:function(){var e;return null!=(e=this.view)&&e.ready&&this.source?this._loadingProjectionEngine?"loading":"ready":"disabled"}}]),r}(r.HandleOwnerMixin(s));t.__decorate([c.property()],R.prototype,"_loadingProjectionEngine",void 0),t.__decorate([c.property()],R.prototype,"activeBasemap",null),t.__decorate([c.property()],R.prototype,"compatibilityFunction",null),t.__decorate([c.property({readOnly:!0,type:w})],R.prototype,"items",void 0),t.__decorate([c.property()],R.prototype,"source",void 0),t.__decorate([u.cast("source")],R.prototype,"castSource",null),t.__decorate([c.property({readOnly:!0})],R.prototype,"state",null),t.__decorate([c.property()],R.prototype,"view",void 0),R=t.__decorate([d.subclass("esri.widgets.BasemapGallery.BasemapGalleryViewModel")],R);return R}));
