/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../../core/Collection.js";import{makeHandle as r}from"../../core/handleUtils.js";import{LRUCache as o}from"../../core/LRUCache.js";import{someMap as i,getOrCreateMapValue as s}from"../../core/MapUtils.js";import{MultiOriginJSONSupportMixin as a}from"../../core/MultiOriginJSONSupport.js";import n from"../../core/ReactiveMap.js";import{watch as l}from"../../core/reactiveUtils.js";import{signal as d}from"../../core/signal.js";import{property as p,subclass as y}from"../../core/accessorSupport/decorators.js";import{Integer as c}from"../../core/accessorSupport/ensureType.js";import h from"../Layer.js";import{BlendLayer as u}from"../mixins/BlendLayer.js";import{ScaleRangeLayer as m}from"../mixins/ScaleRangeLayer.js";import{legendEnabled as f,opacity as g}from"../support/commonProperties.js";import _ from"../support/OrderByInfo.js";import{getAttributeComparator as j}from"../../statistics/utils.js";let v=class extends(m(u(a(h)))){constructor(e){super(e),this._layerCache=new o(20,e=>e.destroy()),this._oidToReference=new n,this._layerToReference=new Map,this.legendEnabled=!0,this.layers=new t,this.maximumVisibleSublayers=10,this.opacity=1,this.parent=null,this.persistenceEnabled=!0,this.title="Layers in view",this.type="catalog-dynamic-group",this.visible=!0}initialize(){this.addHandles([this.layers.on("after-add",({item:e})=>{e.parent=this}),this.layers.on("after-remove",({item:e})=>{e.parent=null}),l(()=>this._orderBy,()=>{this._updateLayerSortValues(),this._sortAllLayers()})])}load(e){return this.addResolvingPromise(this.parent.load()),Promise.resolve(this)}destroy(){this._layerCache.destroy(),this._oidToReference.clear(),this._layerToReference.clear()}get _orderBy(){return this.parent?this.parent.orderBy?.find(e=>!e.valueExpression&&e.field)??new _({field:this.parent.objectIdField}):null}get _referenceComparator(){const e=this._orderBy;if(!this.parent||!e)return()=>0;const t=this.parent.fieldsIndex.get(e.field),r=j(t?.toJSON().type,"descending"===e.order,"case-sensitive"),o=j("esriFieldTypeOID","descending"===e.order);return(e,t)=>r(t.sortValue,e.sortValue)||o(t.objectId,e.objectId)}get fullExtent(){return this.parent?.fullExtent??null}get updating(){return i(this._oidToReference,({pending:e})=>null!=e)}acquireLayer(e){if(this.destroyed)return r();const t=this._getLayerReference(e);return t.count++,r(()=>{t.count--,t.count||this._destroyLayerReference(t)})}_getLayerReference(e){const t=e.getObjectId();return s(this._oidToReference,t,()=>{const t=e.getObjectId(),r=`${t}`,o=e.getAttribute(this.parent.itemSourceField),i=new b(e,t,o),s=this._layerCache.pop(r);return s?(this._addLayer(i,s),i):(i.pending=this.parent.createLayerFromFootprint(e).then(e=>{i.count?this._addLayer(i,e):(this.destroyed||this._layerCache.get(r)||this._layerCache.put(r,e),i.layer=null)}).catch(()=>{}).finally(()=>{i.pending=null}),i)})}_destroyLayerReference(e){e.layer&&(this._layerToReference.delete(e.layer),this.layers.remove(e.layer),this.destroyed?e.layer.destroy():this._layerCache.put(`${e.objectId}`,e.layer),e.layer=null),this._oidToReference.delete(e.objectId)}_addLayer(e,t){e.layer=t,t.persistenceEnabled=!1,this._layerToReference.set(t,e),this._updateLayerSortValue(e),this.layers.add(t),this._sortAllLayers()}_updateLayerSortValues(){for(const e of this._layerToReference.values())this._updateLayerSortValue(e)}_updateLayerSortValue(e){this._orderBy&&(e.sortValue=e.footprint.getAttribute(this._orderBy.field))}_sortAllLayers(){this.layers.sort((e,t)=>this._referenceComparator(this._layerToReference.get(e),this._layerToReference.get(t)))}};e([p()],v.prototype,"_orderBy",null),e([p({readOnly:!0})],v.prototype,"_referenceComparator",null),e([p(f)],v.prototype,"legendEnabled",void 0),e([p({type:["show","hide","hide-children"],json:{write:!0}})],v.prototype,"listMode",void 0),e([p({readOnly:!0})],v.prototype,"fullExtent",null),e([p({type:String,json:{origins:{service:{read:!1},"portal-item":{read:!1}},write:{ignoreOrigin:!0,isRequired:!0}}})],v.prototype,"id",void 0),e([p({readOnly:!0})],v.prototype,"layers",void 0),e([p({type:c,range:{min:0,max:50},json:{write:!0,default:10}})],v.prototype,"maximumVisibleSublayers",void 0),e([p(g)],v.prototype,"opacity",void 0),e([p({clonable:!1})],v.prototype,"parent",void 0),e([p({type:String,nonNullable:!0,json:{write:{ignoreOrigin:!0,isRequired:!0}}})],v.prototype,"title",void 0),e([p({json:{read:!1}})],v.prototype,"type",void 0),e([p({readOnly:!0})],v.prototype,"updating",null),e([p({type:Boolean,json:{name:"visibility",write:!0}})],v.prototype,"visible",void 0),v=e([y("esri.layers.catalog.CatalogDynamicGroupLayer")],v);class b{constructor(e,t,r){this.footprint=e,this.objectId=t,this.itemSource=r,this.count=0,this.layer=null,this.sortValue=void 0,this._pending=d(null)}get pending(){return this._pending.value}set pending(e){this._pending.value=e}}export{v as default};
