/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.24/esri/copyright.txt for details. */ import{_ as e}from"../../chunks/tslib.es6.js";import t from"../../Graphic.js";import"../../intl.js";import s from"../../core/Collection.js";import{HandleOwnerMixin as i}from"../../core/HandleOwner.js";import r from"../../core/Handles.js";import{isSome as o}from"../../core/maybe.js";import{debounce as l,ignoreAbortErrors as n}from"../../core/promiseUtils.js";import{on as a,watch as d}from"../../core/reactiveUtils.js";import{property as h}from"../../core/accessorSupport/decorators/property.js";import"../../core/arrayUtils.js";import"../../core/has.js";import"../../core/accessorSupport/ensureType.js";import{subclass as m}from"../../core/accessorSupport/decorators/subclass.js";import{highlightsSupported as c}from"../../views/support/layerViewUtils.js";import g from"./AttachmentsColumn.js";import u from"./FieldColumn.js";import p from"./Grid/Column.js";import f from"./Grid/Grid.js";import y from"./Grid/GridViewModel.js";import b from"./Grid/GroupColumn.js";import _ from"./support/FeatureStore.js";import{onLocaleChange as C}from"../../intl/locale.js";import{fetchMessageBundle as v}from"../../intl/messages.js";let w=class extends(i(y)){constructor(e){super(e),this._debounceRefresh=l((()=>this._refresh())),this._defaultHiddenFields=["CreationDate","Creator","EditDate","Editor","GlobalID"],this._highlights=new r,this.activeFilters=new s,this.autoRefreshEnabled=!0,this.cellClassNameGenerator=(e,t)=>e.path||null,this.columns=new s,this.dataProvider=async(e,t)=>{const{store:s}=this,{page:i,pageSize:r,sortOrders:o}=e,l=this._sortOrdersToLayerOrderByFields(o);t&&(s?(await s.set({orderByFields:l}),"loaded"!==s.state&&"loading"!==s.state&&await s.load(),t&&t(await s.fetchItems({page:i,pageSize:r}))):t&&t([]))},this.editingEnabled=!1,this.fieldConfigs=null,this.grid=null,this.hiddenFields=new s,this.highlightOnRowSelectEnabled=!0,this.itemIdPath="objectId",this.messagesCommon=null,this.relatedRecordsEnabled=!1,this.store=null,this.tableTemplate=null,this.view=null;const{hiddenFields:t,itemIdPath:i}=this;t.addMany(this._defaultHiddenFields),this._onLayerRefresh=this._onLayerRefresh.bind(this),this._set("store",new _),this._set("grid",new f({itemIdPath:i,viewModel:this}))}initialize(){const e=async()=>{this.messages=await v("esri/widgets/FeatureTable/t9n/FeatureTable"),this.messagesCommon=await v("esri/t9n/common")};e(),this.handles.add([C(e),a((()=>this.grid.selectedItems),"change",(e=>this._onSelectionChange(e))),d((()=>this.relatedRecordsEnabled),(e=>this.store.relatedRecordsEnabled=e)),d((()=>this.layer?.loaded),(e=>e&&this._onLayerLoad())),d((()=>this.store.state),(e=>{"loaded"===e&&this.grid?.scrollToTop()})),d((()=>[this.editingEnabled,this.messages,this.tableTemplate,this.fieldConfigs]),(()=>this.layer?.loaded&&this._generateColumns())),d((()=>this.layer?.definitionExpression),((e,t)=>(e||t)&&"loaded"===this.store.state&&this.reset())),a((()=>this.layer),"refresh",(e=>this._onLayerRefresh(e)))])}destroy(){this._resetColumns(),this.handles.removeAll(),this._highlights.removeAll(),this._highlights.destroy(),this.grid?.destroy(),this.columns?.destroy(),this.layer=null,this.view=null}get activeSortOrders(){return this.grid?.sortOrders||[]}set attachmentsEnabled(e){this.attachmentsEnabled!==e&&(this._set("attachmentsEnabled",e),this._generateColumns(),this.store.attachmentsEnabled=e)}set filterGeometry(e){if(!this.filterGeometry&&!e)return;const t=this.activeFilters.find((e=>"geometry"===e.type));t&&this.activeFilters.remove(t),this.clearSelection(),this._set("filterGeometry",e),this.store.filterGeometry=e,e&&this.activeFilters.add({type:"geometry",geometry:e})}set layer(e){this._set("layer",e),this.grid?.clearSort(),this._resetColumns(),this.store.layer=e,e&&(e.loaded?this._onLayerLoad():e.load()),this.notifyChange("state")}set messages(e){this.grid?.set("messages",e),this._set("messages",e)}clearHighlights(){this._highlights.removeAll()}clearSelection(){this.grid?.clearSelection()}deselectRows(e){const t=this._getStoreItemsFromSelectionParams(e);this.grid.deselectRows(t)}filterBySelection(){const e=this.grid.getSelectedRowIds();e.length&&(this.clearSelectionFilter(),this.store.objectIds=e,this.activeFilters.add({type:"selection",objectIds:e}))}getObjectIdIndex(e){return this.store?.getObjectIdIndex(e)}getValue(e,t){return this.store.getItemByObjectId(e)?.feature?.attributes[t]}async refresh(){return n(this._debounceRefresh())}reset(){this.grid.reset()}selectRows(e){const t=this._getStoreItemsFromSelectionParams(e);this.grid.selectRows(t)}scrollToIndex(e){this.grid?.scrollToIndex(e)}clearSelectionFilter(){this.store.objectIds=null;const e=this.activeFilters.find((e=>"selection"===e.type));e&&this.activeFilters.remove(e)}zoomToSelection(){const{layer:e,view:t}=this,s=this.grid.getSelectedRowIds();if(!e||!t||!s.length)return;const i=e.createQuery();i.objectIds=s,i.returnGeometry=!0,e.queryFeatures(i).then((e=>{t.goTo(e.features).catch((e=>{"AbortError"!==e.name&&console.error(e)}))}))}async _refresh(){await this.store.refreshLayerInfo();const e=this.grid.getSelectedRowIds();if(e.length){(await this.store.verifyFeaturesByObjectId(e)).forEach(((t,s)=>{t||this.deselectRows(e[s])}))}this.grid.refreshPageCache()}_onLayerLoad(){const e=this.layer.capabilities.query.maxRecordCount;e&&e(d||[]).find((t=>"fieldName"in e&&e.fieldName===t.name))||"group"===e.type&&"columnTemplates"in e)).map((e=>{if("fieldName"in e){const h=(d||[]).find((t=>e.fieldName===t.name)),m=!1===e.visible||!0!==e.visible&&i.includes(h.name),{direction:c,formatFunction:g,initialSortPriority:p,menuConfig:f,textAlign:y}=e,b=a.expressionInfos||null;return new u({direction:c,editingEnabled:t,expressionInfos:b,field:h,formatFunction:g,grid:s,hidden:m,initialSortPriority:p,layer:r,messages:o,messagesCommon:l,menuConfig:f,store:n,template:e,textAlign:y})}if("group"===e.type&&"columnTemplates"in e){const t=this._generateColumnsFromTemplates(e.columnTemplates),s=!1===e.visible||!0!==e.visible&&i.includes(e.label),{label:r}=e;return new b({header:r,columns:t,hidden:s})}return new p({...e})}))}_generateColumnsFromConfigs(){const{editingEnabled:e,fieldConfigs:t,grid:s,hiddenFields:i,layer:r,messages:l,messagesCommon:n,store:a}=this,d=this.layer?.fields??[];return t.filter((e=>(d||[]).find((t=>e.name===t.name)))).map((t=>{const h=t.direction||null,m=t.formatFunction||null,c=t.textAlign||null,g=o(t.initialSortPriority)?t.initialSortPriority:null,p=(d||[]).find((e=>t.name===e.name)),f=!1===t.visible||!0!==t.visible&&i.includes(p.name),y=t.menuConfig||null;return new u({config:t,direction:h,editingEnabled:e,field:p,formatFunction:m,grid:s,hidden:f,layer:r,store:a,messages:l,messagesCommon:n,menuConfig:y,textAlign:c,initialSortPriority:g})}))}_generateDefaultFieldColumns(){const{editingEnabled:e,grid:t,hiddenFields:s,layer:i,messages:r,messagesCommon:o,store:l}=this;return(this.layer?.fields??[]).map((n=>{const a=s.includes(n.name);return new u({editingEnabled:e,field:n,grid:t,hidden:a,layer:i,store:l,messages:r,messagesCommon:o})}))}_generateDefaultAttachmentColumn(){return new g({header:this.messages?.attachments})}_sortOrdersToLayerOrderByFields(e){return e&&e.length?e.filter(((e,t,s)=>s.map((e=>e.path)).indexOf(e.path)===t)).filter((({direction:e})=>!!e)).map((({direction:e,path:t})=>t+" "+e.toUpperCase())):[]}_highlight(e){const{view:t}=this,s=t&&e&&t.allLayerViews.items.find((({layer:t})=>t===e.layer));c(s)&&this._highlights.add(s.highlight(e),`feature-${e.getObjectId()}`)}_unhighlight(e){e&&this._highlights.remove(`feature-${e.getObjectId()}`)}_getStoreItemsFromSelectionParams(e){const s=[];return(e=e instanceof Array?e:[e]).forEach((e=>{let i=e instanceof t?e:null;const r=i?i.getObjectId():e;if("number"==typeof r||"string"==typeof r){if(!i){const e=this.store.getItemByObjectId(r);e&&(i=e.feature)}s.push({objectId:r,feature:i})}})),s}_onSelectionChange(e){const{highlightOnRowSelectEnabled:t}=this,{added:s,removed:i}=e;t&&s.forEach((e=>this._highlight(e.feature))),t&&i.forEach((e=>this._unhighlight(e.feature)))}_resetColumns(){this.columns.items.forEach((e=>e.destroy())),this.columns.removeAll()}};e([h({readOnly:!0})],w.prototype,"activeFilters",void 0),e([h({readOnly:!0})],w.prototype,"activeSortOrders",null),e([h()],w.prototype,"attachmentsEnabled",null),e([h()],w.prototype,"autoRefreshEnabled",void 0),e([h()],w.prototype,"cellClassNameGenerator",void 0),e([h({readOnly:!0})],w.prototype,"columns",void 0),e([h()],w.prototype,"dataProvider",void 0),e([h()],w.prototype,"editingEnabled",void 0),e([h()],w.prototype,"fieldConfigs",void 0),e([h()],w.prototype,"filterGeometry",null),e([h({readOnly:!0})],w.prototype,"grid",void 0),e([h()],w.prototype,"hiddenFields",void 0),e([h()],w.prototype,"highlightOnRowSelectEnabled",void 0),e([h({readOnly:!0})],w.prototype,"itemIdPath",void 0),e([h()],w.prototype,"layer",null),e([h()],w.prototype,"messages",null),e([h()],w.prototype,"messagesCommon",void 0),e([h()],w.prototype,"relatedRecordsEnabled",void 0),e([h({readOnly:!0,type:_})],w.prototype,"store",void 0),e([h()],w.prototype,"tableTemplate",void 0),e([h()],w.prototype,"view",void 0),w=e([m("esri.widgets.FeatureTable.FeatureTableViewModel")],w);const F=w;export{F as default};