/* 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 o,on as a}from"../../core/reactiveUtils.js";import{property as l,subclass as r}from"../../core/accessorSupport/decorators.js";import d from"../LayerList/ListItem.js";import{findLayerListMode as c}from"../LayerList/support/layerListUtils.js";const n={layerListMode:"layer-list-mode"},m="hide",h=e.ofType(d);let y=class extends s{constructor(t){super(t),this.catalogItems=new h,this.checkPublishStatusEnabled=!1,this.catalogLayer=null,this.listItemCreatedFunction=null,this.listModeDisabled=!1,this.view=null}initialize(){this.addHandles([i(()=>[this.catalogLayer?.loaded,this.view?.ready],()=>this._compileList(),o),a(()=>this.catalogLayer?.dynamicGroupLayer.layers,"change",()=>this._compileList()),i(()=>[this.listItemCreatedFunction,this.checkPublishStatusEnabled,this.listModeDisabled],()=>this._recompileList())])}destroy(){this.view=null,this._removeAllItems()}get state(){const{view:t,catalogLayer:e}=this;return t?.ready&&e?.loaded?"ready":t&&e?"loading":"disabled"}get totalItems(){return this.catalogItems.flatten(t=>t.children).length}triggerAction(t,e){t&&!t.disabled&&this.emit("trigger-action",{action:t,item:e})}_createListItem(t){const{view:e,listItemCreatedFunction:s,checkPublishStatusEnabled:i,listModeDisabled:o}=this;return new d({checkPublishStatusEnabled:i,listModeDisabled:o,layer:t,listItemCreatedFunction:s,view:e})}_removeAllItems(){this.catalogItems.destroyAll()}_getViewableLayers(t){return t?this.listModeDisabled?t:t.filter(t=>c(t)!==m):void 0}_watchLayersListMode(t){this.removeHandles(n.layerListMode),t&&!this.listModeDisabled&&this.addHandles(i(()=>t.filter(t=>"listMode"in t).map(t=>t.listMode).toArray(),()=>this._compileList()),n.layerListMode)}_compileList(){const{catalogLayer:t}=this;if(!t?.loaded)return;const e=t?.dynamicGroupLayer.layers;this._watchLayersListMode(e);const s=this._getViewableLayers(e);s?.length?(this._createNewItems(s),this._removeItems(s),this._sortItems(s)):this._removeAllItems()}_createNewItems(t){const{catalogItems:e}=this;t.forEach(t=>{e.some(e=>e.layer===t)||e.add(this._createListItem(t))})}_removeItems(t){const{catalogItems:e}=this,s=[];e.forEach(e=>{e&&t?.includes(e.layer)||s.push(e)}),e.destroyMany(s)}_sortItems(t){const{catalogItems:e}=this;e.sort((e,s)=>{const i=t.indexOf(e.layer),o=t.indexOf(s.layer);return i>o?-1:i<o?1:0})}_recompileList(){this._removeAllItems(),this._compileList()}};t([l({type:h})],y.prototype,"catalogItems",void 0),t([l()],y.prototype,"checkPublishStatusEnabled",void 0),t([l()],y.prototype,"catalogLayer",void 0),t([l()],y.prototype,"listItemCreatedFunction",void 0),t([l({nonNullable:!0})],y.prototype,"listModeDisabled",void 0),t([l({readOnly:!0})],y.prototype,"state",null),t([l()],y.prototype,"totalItems",null),t([l()],y.prototype,"view",void 0),y=t([r("esri.widgets.CatalogLayerList.CatalogLayerListViewModel")],y);export{y as default};
