/*
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","../../../core/Accessor","../../../core/HandleOwner","../../../core/promiseUtils","../../../core/reactiveUtils","../../../core/accessorSupport/decorators/property","../../../core/arrayUtils","../../../core/has","../../../core/accessorSupport/ensureType","../../../core/accessorSupport/decorators/subclass","../../../support/basemapUtils"],(function(e,t,r,s,i,a,o,n,l,c,p,h){"use strict";let u=function(t){function r(e){var r;return(r=t.call(this,e)||this).compatibilityFunction=null,r.error=null,r.state="loading",r.view=null,r}e._inheritsLoose(r,t);var s=r.prototype;return s.initialize=function(){const e=()=>this.refresh();this.handles.add([a.watch((()=>{var e;return null==(e=this.basemap)?void 0:e.loadStatus}),e),a.watch((()=>this.compatibilityFunction),e),a.watch((()=>{var e;return this.view&&"basemapTerrain"in this.view&&(null==(e=this.view.basemapTerrain)?void 0:e.tilingScheme)}),e),a.watch((()=>{var e;return null==(e=this.view)?void 0:e.ready}),e),a.watch((()=>{var e;return null==(e=this.view)?void 0:e.spatialReference}),e)]),this.refresh()},s.destroy=function(){this._cancelRefresh(),this.basemap=null,this.compatibilityFunction=null,this.view=null},s.refresh=function(){var e;this._cancelRefresh(),this._set("state","loading");const t=null==(e=this.basemap)?void 0:e.loadStatus;if("loaded"!==t&&"failed"!==t)return;if(!this.compatibilityFunction)return void("loaded"===t?(this._set("state","ready"),this._set("error",null)):(this._set("state","error"),this._set("error",this.basemap.loadError)));const r=new AbortController,{signal:s}=r;this.compatibilityFunction(this,{signal:s}).then((()=>a.whenOnce((()=>!this._spatialReferenceTask.updating),s))).then((()=>{this._set("state","ready"),this._set("error",null)})).catch((e=>{i.isAbortError(e)||(this._set("state","error"),this._set("error",e))})),this._refreshController=r},s._cancelRefresh=function(){this._refreshController&&(this._refreshController.abort(),this._refreshController=null)},e._createClass(r,[{key:"_spatialReferenceTask",get:function(){return h.findSpatialReference(this.view,this.basemap)}},{key:"basemap",set:function(e){const t=this._get("basemap");t&&t.cancelLoad(),e&&e.load().catch((()=>{})),this._set("basemap",e)}},{key:"spatialReference",get:function(){return this._spatialReferenceTask.spatialReference}}]),r}(s.HandleOwnerMixin(r));t.__decorate([o.property({readOnly:!0})],u.prototype,"_spatialReferenceTask",null),t.__decorate([o.property()],u.prototype,"basemap",null),t.__decorate([o.property()],u.prototype,"compatibilityFunction",void 0),t.__decorate([o.property({readOnly:!0})],u.prototype,"error",void 0),t.__decorate([o.property({readOnly:!0})],u.prototype,"spatialReference",null),t.__decorate([o.property({readOnly:!0})],u.prototype,"state",void 0),t.__decorate([o.property()],u.prototype,"view",void 0),u=t.__decorate([p.subclass("esri.widgets.BasemapGallery.support.BasemapGalleryItem")],u);return u}));
