/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../chunks/_rollupPluginBabelHelpers","../../chunks/tslib.es6","../../core/Collection","../../core/Evented","../../core/Handles","../../core/reactiveUtils","../../core/accessorSupport/decorators/property","../../core/arrayUtils","../../core/has","../../core/accessorSupport/ensureType","../../core/accessorSupport/decorators/subclass","../LayerList/support/layerListUtils","./ListItem"],(function(e,t,s,i,o,a,r,l,n,c,d,h,m){"use strict";const u={map:"map",tables:"tables",layerListMode:"layer-list-mode"},p="hide",_=s.ofType(m);let y=function(t){function s(e){var s;return(s=t.call(this,e)||this)._handles=new o,s.listItemCreatedFunction=null,s.tableItems=new _,s.map=null,s}e._inheritsLoose(s,t);var i=s.prototype;return i.initialize=function(){this._handles.add([a.watch((()=>{var e;return[this.map,null==(e=this.map)?void 0:e.loaded]}),(()=>this._mapHandles()),a.initial),a.watch((()=>this.listItemCreatedFunction),(()=>this._listItemCreatedFunctionChanged()))],u.map)},i.destroy=function(){this._handles.destroy(),this._handles=null,this.map=null,this.tableItems.removeAll()},i.triggerAction=function(e,t){e&&!e.disabled&&this.emit("trigger-action",{action:e,item:t})},i._mapHandles=function(){const{_handles:e,map:t}=this;e.remove(u.tables),this._compileList(),t&&e.add(a.watch((()=>{var e,t;return null==(e=this.map)||null==(t=e.allTables)?void 0:t.toArray()}),(()=>this._compileList()),a.initial),u.tables)},i._removeAllItems=function(){this.tableItems.removeAll()},i._getViewableTables=function(e){if(e)return e.filter((e=>h.findLayerListMode(e)!==p))},i._watchTablesListMode=function(e){const{_handles:t}=this;t.remove(u.layerListMode),e&&e.forEach((e=>{t.add(a.watch((()=>e.listMode),(()=>this._compileList())),u.layerListMode)}))},i._compileList=function(){var e;const t=null==(e=this.map)?void 0:e.tables;this._watchTablesListMode(t);const s=this._getViewableTables(t);s&&s.length?(this._createNewItems(s),this._removeItems(s),this._sortItems(s)):this._removeAllItems()},i._createNewItems=function(e){const{tableItems:t,listItemCreatedFunction:s}=this;e.forEach((e=>{t.find((t=>t.layer===e))||t.add(new m({layer:e,listItemCreatedFunction:s}))}))},i._removeItems=function(e){const{tableItems:t}=this;t.forEach((s=>{if(!s)return;e&&e.find((e=>s.layer===e))||t.remove(s)}))},i._sortItems=function(e){const{tableItems:t}=this;t.sort(((t,s)=>{const i=e.indexOf(t.layer),o=e.indexOf(s.layer);return i>o?-1:i<o?1:0}))},i._listItemCreatedFunctionChanged=function(){this._removeAllItems(),this._compileList()},e._createClass(s,[{key:"state",get:function(){var e;const t=null==(e=this.map)?void 0:e.loadStatus;return"string"==typeof t?"loaded"===t?"ready":"loading"===t?"loading":"disabled":"disabled"}}]),s}(i.EventedAccessor);t.__decorate([r.property()],y.prototype,"listItemCreatedFunction",void 0),t.__decorate([r.property({type:_})],y.prototype,"tableItems",void 0),t.__decorate([r.property()],y.prototype,"map",void 0),t.__decorate([r.property({readOnly:!0})],y.prototype,"state",null),y=t.__decorate([d.subclass("esri.widgets.TableList.TableListViewModel")],y);return y}));
