/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as t}from"tslib";import e from"../../core/Collection.js";import{EventedAccessor as s}from"../../core/Evented.js";import{watch as i,initial as l}from"../../core/reactiveUtils.js";import{property as o,subclass as a}from"../../core/accessorSupport/decorators.js";import{findLayerListMode as r}from"../LayerList/support/layerListUtils.js";import d from"./ListItem.js";const m={map:"map",layerListMode:"layer-list-mode"},n="hide",h=e.ofType(d);let c=class extends s{constructor(t){super(t),this.checkPublishStatusEnabled=!1,this.listItemCreatedFunction=null,this.listModeDisabled=!1,this.tableItems=new h,this.map=null}initialize(){this.addHandles([i(()=>[this.tables?.toArray(),this.map?.allTables.toArray()],()=>this._compileList(),l),i(()=>[this.map?.loaded,this.listItemCreatedFunction,this.checkPublishStatusEnabled,this.listModeDisabled],()=>this._recompileList())],m.map)}destroy(){this._removeAllItems(),this.map=null,this.tables=null}get tables(){return this.map?.allTables}set tables(t){this._overrideIfSome("tables",t)}get state(){const{map:t,tables:e}=this;if(!t)return e?"ready":"disabled";const s=t?.loadStatus;return"string"==typeof s?"loaded"===s?"ready":"loading"===s?"loading":"disabled":"ready"}get totalItems(){return this.tableItems.length}triggerAction(t,e){t&&!t.disabled&&this.emit("trigger-action",{action:t,item:e})}_removeAllItems(){this.tableItems.destroyAll()}_getViewableTables(t){return t?this.listModeDisabled?t:t.filter(t=>r(t)!==n):void 0}_watchTablesListMode(t){this.removeHandles(m.layerListMode),t&&!this.listModeDisabled&&this.addHandles(i(()=>t.filter(t=>"listMode"in t).map(t=>t.listMode).toArray(),()=>this._compileList()),m.layerListMode)}_compileList(){const{tables:t}=this;this._watchTablesListMode(t);const e=this._getViewableTables(t);e?.length?(this._createNewItems(e),this._removeItems(e),this._sortItems(e)):this._removeAllItems()}_createNewItems(t){const{tableItems:e,listItemCreatedFunction:s,checkPublishStatusEnabled:i}=this;t.forEach(t=>{e.some(e=>e.layer===t)||e.add(new d({checkPublishStatusEnabled:i,layer:t,listItemCreatedFunction:s}))})}_removeItems(t){const{tableItems:e}=this;e.forEach(s=>{if(!s)return;const i=t?.find(t=>s.layer===t);i||(e.remove(s),s.destroy())})}_sortItems(t){const{tableItems:e}=this;e.sort((e,s)=>{const i=t.indexOf(e.layer),l=t.indexOf(s.layer);return i>l?-1:i<l?1:0})}_recompileList(){this._removeAllItems(),this._compileList()}};t([o()],c.prototype,"checkPublishStatusEnabled",void 0),t([o()],c.prototype,"listItemCreatedFunction",void 0),t([o({nonNullable:!0})],c.prototype,"listModeDisabled",void 0),t([o({type:h,readOnly:!0})],c.prototype,"tableItems",void 0),t([o()],c.prototype,"tables",null),t([o()],c.prototype,"map",void 0),t([o({readOnly:!0})],c.prototype,"state",null),t([o()],c.prototype,"totalItems",null),c=t([a("esri.widgets.TableList.TableListViewModel")],c);export{c as default};
