// All material copyright Esri, All Rights Reserved, unless otherwise specified. // See https://js.arcgis.com/4.30/esri/copyright.txt for details. //>>built define("../../chunks/tslib.es6 ../../core/Evented ../../core/ObjectPool ../../core/reactiveUtils ../../core/accessorSupport/decorators/property ../../core/has ../../core/Logger ../../core/RandomLCG ../../core/accessorSupport/decorators/subclass ../../layers/support/layerUtils ./TemplateItem ./TemplateItemGroup ../support/templateUtils".split(" "),function(f,d,q,u,g,B,C,D,v,w,r,x,t){var n;const y=({layer:a})=>({key:a,label:a.title??""}),z=({layer:a})=>({key:a.geometryType,label:a.geometryType??""}); d=n=class extends d.EventedAccessor{constructor(a){super(a);this._groupPool=new q(x);this._itemPool=new q(r);this.disabled=!1;this.selectedItem=this.filterFunction=this.disabledItemFunction=null}initialize(){this._get("groupBy")||(this.groupBy="layer")}get _featureTemplatesByLayer(){if(!this.layers)return new Map;const a=new Map;for(const b of this.layers)if("subtype-group"===b.type)for(const c of b.sublayers)a.set(c,t.getAllTemplatesForLayer(c));else a.set(b,t.getAllTemplatesForLayer(b));return a}set groupBy(a){this._set("groupBy", a);if("function"===typeof a)this._groupByFunction=b=>this._ensureGroupByObject(a(b));else switch(a){case "layer":this._groupByFunction=y;break;case "geometry":this._groupByFunction=z;break;default:this._groupByFunction=null}}set layers(a){this.removeHandles("layers");if(a){const b=()=>this.notifyChange("state");this.addHandles(a.map(c=>u.when(()=>c.loadStatus,b)),"layers")}this._set("layers",a)}get layers(){return this._get("layers")}get state(){const {layers:a}=this;return a&&0!==a.length?a.some(b=> "loading"===b.loadStatus||"not-loaded"===b.loadStatus)?"loading":"ready":"disabled"}get numberOfFeatureTemplates(){return Array.from(this._featureTemplatesByLayer.values()).reduce((a,b)=>a+b.length,0)}get items(){if(0===this.numberOfFeatureTemplates)return this._releasePreviousItems(),[];var a=this._featureTemplatesByLayer,b=[],c=null!=this.filterFunction?this.filterFunction:n._nullFilterFunction;for(const [e,k]of a)if(e.loaded||"subtype-sublayer"===e.type&&e.parent?.loaded)if(a=w.getEffectiveLayerCapabilities(e)?.operations, a?.supportsEditing&&a?.supportsAdd)for(var h of k)b.push({layer:e,template:h,matchesFilter:c({label:h.name})});if(null==this._groupByFunction)return c=b.filter(({matchesFilter:e})=>e).map(({template:e,layer:k})=>this._createItem(e,k)),this._releasePreviousItems(),c;h=new Map;for(var l of b){const {template:e,layer:k}=l;b=this._groupByFunction({template:e,layer:k});const {key:m,label:p}=null!=b.key?b:n.nullGroupBy;h.has(m)||h.set(m,{label:p,templateItemInfos:[]});h.get(m)?.templateItemInfos.push(l)}l= [];for(const e of h.values()){const {label:k,templateItemInfos:m}=e;b=m.filter(({matchesFilter:p})=>p);b=c({label:k})?m:0this._createItem(p,A)),l.push(this._createGroup(k,b)))}if(1===l.length&&l[0].label===n.nullGroupBy.label)return this._releasePreviousItems(),l[0].items;this._releasePreviousItems();return l}refresh(){this.notifyChange("items")}select(a,{emit:b=!0}={}){const c=this.selectedItem;a=a?.clone()||null;this._set("selectedItem", a);b&&this.emit("select",{item:a,oldItem:c,template:a?.template??null})}_createItem(a,b){const c=this._itemPool.acquire(),h=this.disabledItemFunction?.({template:a,layer:b})??!1;c.set({disabled:h,layer:b,template:a});return c}_createGroup(a,b){const c=this._groupPool.acquire();c.set("label",a);c.items=b;return c}_releasePreviousItems(){this._destroyItems(this._get("items"))}_destroyItems(a){a&&(a[0]instanceof r?a.forEach(b=>this._destroyItem(b)):a.forEach(b=>this._destroyGroup(b)))}_destroyGroup(a){a.items.forEach(b=> this._destroyItem(b));a.items.length=0;this._groupPool.release(a)}_destroyItem(a){a.layer=null;a.template=null;this._itemPool.release(a)}_ensureGroupByObject(a){return"string"===typeof a?{key:a,label:a}:a}};d.nullGroupBy={key:Symbol(),label:"Other\u200b"};d._nullFilterFunction=a=>!0;f.__decorate([g.property()],d.prototype,"_featureTemplatesByLayer",null);f.__decorate([g.property()],d.prototype,"_groupByFunction",void 0);f.__decorate([g.property()],d.prototype,"disabled",void 0);f.__decorate([g.property()], d.prototype,"disabledItemFunction",void 0);f.__decorate([g.property()],d.prototype,"filterFunction",void 0);f.__decorate([g.property()],d.prototype,"groupBy",null);f.__decorate([g.property()],d.prototype,"layers",null);f.__decorate([g.property()],d.prototype,"state",null);f.__decorate([g.property({readOnly:!0})],d.prototype,"numberOfFeatureTemplates",null);f.__decorate([g.property({readOnly:!0})],d.prototype,"items",null);f.__decorate([g.property({readOnly:!0})],d.prototype,"selectedItem",void 0); return d=n=f.__decorate([v.subclass("esri.widgets.FeatureTemplates.FeatureTemplatesViewModel")],d)});