/*
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/Error","../../../core/Handles","../../../core/Loadable","../../../core/Logger","../../../core/Promise","../../../core/promiseUtils","../../../core/reactiveUtils","../../../core/accessorSupport/decorators/property","../../../core/arrayUtils","../../../core/has","../../../core/accessorSupport/ensureType","../../../core/accessorSupport/decorators/subclass","../../../portal/Portal","./LocalBasemapsSource"],(function(t,e,r,a,o,s,l,i,n,p,c,u,h,d,y,m,f,_){"use strict";const b=a.ofType(r),B="esri.widgets.BasemapGallery.support.PortalBasemapsSource",g=i.getLogger(B);let C=function(e){function r(t){var r;return(r=e.call(this,t)||this)._handles=new s,r.basemaps=new b,r.filterFunction=null,r.portal=f.getDefault(),r.query=null,r.updateBasemapsCallback=null,r}t._inheritsLoose(r,e);var a=r.prototype;return a.initialize=function(){this._handles.add([c.watch((()=>{var t,e;return[this.filterFunction,this.loadStatus,null==(t=this.portal)?void 0:t.basemapGalleryGroupQuery,null==(e=this.portal)?void 0:e.user,this.query,this.updateBasemapsCallback]}),(()=>this.refresh()),c.initial)])},a.destroy=function(){this._handles.destroy(),this._handles=null,this.filterFunction=null,this.portal=null},a.load=function(t){return this.addResolvingPromise(this.portal.load(t)),this.notifyChange("state"),Promise.resolve(this)},a.refresh=function(){var e=t._asyncToGenerator((function*(){if("ready"!==this.state)return;this._lastPortalBasemapFetchController&&(this._lastPortalBasemapFetchController.abort(),this._lastPortalBasemapFetchController=null);const t=this.portal,e=new AbortController;this._lastPortalBasemapFetchController=e,this.notifyChange("state");try{const r=yield t.fetchBasemaps(this._toQueryString(this.query),e);this._updateBasemaps(r)}catch(r){if(p.isAbortError(r))throw r;g.warn(new o("basemap-source:fetch-basemaps-error","Could not fetch basemaps from portal.",{error:r})),this._updateBasemaps()}this._lastPortalBasemapFetchController=null,this.notifyChange("state")}));function r(){return e.apply(this,arguments)}return r}(),a._toQueryString=function(t){return t&&"string"!=typeof t?Object.keys(t).map((e=>`${e}:${t[e]}`)).join(" AND "):t},a._updateBasemaps=function(t=[]){let e=this.filterFunction?t.filter(this.filterFunction):t;e=this.updateBasemapsCallback?this.updateBasemapsCallback(e):e,this.basemaps.removeAll(),this.basemaps.addMany(e)},t._createClass(r,[{key:"state",get:function(){return"not-loaded"===this.loadStatus?"not-loaded":"loading"===this.loadStatus||this._lastPortalBasemapFetchController?"loading":"ready"}}]),r}(l.LoadableMixin(n.EsriPromiseMixin(_)));e.__decorate([u.property({readOnly:!0,type:b})],C.prototype,"basemaps",void 0),e.__decorate([u.property()],C.prototype,"filterFunction",void 0),e.__decorate([u.property({type:f})],C.prototype,"portal",void 0),e.__decorate([u.property()],C.prototype,"query",void 0),e.__decorate([u.property({readOnly:!0})],C.prototype,"state",null),e.__decorate([u.property()],C.prototype,"updateBasemapsCallback",void 0),C=e.__decorate([m.subclass(B)],C);return C}));
