/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as e}from"tslib";import{countIf as t,addMany as r,isSome as s}from"../../../core/arrayUtils.js";import i from"../../../core/ReactiveMap.js";import{property as o,subclass as a}from"../../../core/accessorSupport/decorators.js";import{getDisplayFieldNameFromFields as l}from"../../../layers/support/fieldUtils.js";import{isSelectableLayerWithObjectIds as n,isSelectableLayerViewWithObjectIds as c}from"../../../views/support/selectionUtils.js";import u from"./FeatureItem.js";import p from"./ItemBase.js";import{getSuggestedQueryOutFields as f}from"../Selector2D/selectorUtils.js";let m=class extends p{constructor(e){super(e),this._onChangeController=null,this.featureItems=[],this.featureTitleMap=new i,this.groupLayerItem=null,this.layer=null}get effectiveObjectIds(){return this.objectIds.slice(0,this.maxVisibleFeatureCountPerLayer)}get effectiveSelection(){return this.selection.slice(0,this.maxVisibleFeatureCountPerLayer)}get maxVisibleFeatureCountExceeded(){const{viewModel:e}=this;return this.selection.length>e.maxVisibleFeatureCountPerLayer&&this.visibleTotal===e.maxVisibleFeatureCountPerLayer}get maxVisibleFeatureCountPerLayer(){return this.viewModel.maxVisibleFeatureCountPerLayer}get objectIds(){const e=this.layer;return n(e)?this.selection:[]}get selection(){return this.viewModel.effectiveSelectionManager?.getSelection(this.layer)??[]}get total(){return this.selection.length}get visible(){return this.featureItems.some(e=>e.visible)}get visibleTotal(){return t(this.featureItems,e=>e.visible)}cancel(){this._onChangeController?.abort()}reset(){this.featureItems.forEach(e=>e.destroy()),this.featureItems=[]}async sync({controller:e,promises:t}){const{effectiveSelection:i,layer:o}=this;this.cancel();const a=new Map(this.featureItems.map(e=>[e.identifier,e])),l=new Map,c=[],u=[];for(const r of i){const e=a.get(r);e?l.set(r,e):(c.push(r),l.set(r,null))}if(this._onChangeController=e,c.length>0){n(o)?r(u,await this._createFeatureItemsFromIds(o,c,e)):r(u,this._createFeatureItemsFromGraphics(c));for(const e of u)null!=e.identifier&&l.set(e.identifier,e)}e.signal.aborted||(this.featureItems=Array.from(l.values()).filter(s),t.push(this.syncTitles(u)))}async syncTitles(e=this.featureItems){const{layer:t}=this,r=e.map(e=>e.graphic);if("getFeatureTitles"in t&&t.getFeatureTitles){(await t.getFeatureTitles(r,{fetchMissingFields:!0,removeHTML:!0})).forEach((e,t)=>this.featureTitleMap.set(t,e))}else if(n(t)){const e=t.displayField||l(t.fields);e&&r.forEach(t=>{const r=t.getObjectId(),s=t.attributes[e];null!=r&&s&&this.featureTitleMap.set(r,s)})}}_createFeatureItemsFromGraphics(e){const{layer:t,viewModel:r}=this;return e.map(e=>new u({graphic:e,layer:t,layerItem:this,viewModel:r}))}async _createFeatureItemsFromIds(e,t,r){const{layerView:s,viewModel:i}=this,o=new Map;if(c(s)){const e=s.createQuery();e.objectIds=t,e.outFields=["*"],e.returnGeometry=!0;const i=await s.queryFeatures(e,r);for(const t of i.features){const e=t.getObjectId();null!=e&&o.set(e,t)}}if(r.signal.aborted)return[];const a=t.filter(e=>!o.has(e));if(a.length){const{view:t}=this,s=e.createQuery();s.objectIds=a,s.outSpatialReference=t?.spatialReference,s.outFields=t?f(e,t,!0):["*"],s.returnGeometry=!0;const i=await e.queryFeatures(s,r);for(const e of i.features){const t=e.getObjectId();null!=t&&o.set(t,e)}}return Array.from(o.values()).map(t=>new u({graphic:t,layer:e,layerItem:this,viewModel:i}))}};e([o()],m.prototype,"_onChangeController",void 0),e([o()],m.prototype,"effectiveObjectIds",null),e([o()],m.prototype,"effectiveSelection",null),e([o()],m.prototype,"featureItems",void 0),e([o()],m.prototype,"featureTitleMap",void 0),e([o()],m.prototype,"groupLayerItem",void 0),e([o()],m.prototype,"layer",void 0),e([o()],m.prototype,"maxVisibleFeatureCountExceeded",null),e([o()],m.prototype,"maxVisibleFeatureCountPerLayer",null),e([o()],m.prototype,"objectIds",null),e([o()],m.prototype,"selection",null),e([o()],m.prototype,"total",null),e([o()],m.prototype,"visible",null),e([o()],m.prototype,"visibleTotal",null),m=e([a("esri.widgets.support.SelectionList.LayerItem")],m);const h=m;export{h as default};
