/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../../core/Collection.js";import{EventedAccessor as s}from"../../core/Evented.js";import{watch as i,initial as a}from"../../core/reactiveUtils.js";import{property as r,subclass as l}from"../../core/accessorSupport/decorators.js";import{isLinkChartView as o}from"../../linkChart/utils.js";import n from"./ListItem.js";import{getItemLayers as d,findLayerListMode as h}from"./support/layerListUtils.js";const m={view:"view",viewLayers:"view-layers",mapLayers:"map-layers",layerViews:"layer-views",layerListMode:"layer-list-mode"},c="hide",p=t.ofType(n);let y=class extends s{constructor(e){super(e),this.checkPublishStatusEnabled=!1,this.listItemCreatedFunction=null,this.listModeDisabled=!1,this.operationalItems=new p,this.view=null}initialize(){this.addHandles([i(()=>!0===this.view?.ready,()=>this._viewHandles(),a),i(()=>[this.listItemCreatedFunction,this.checkPublishStatusEnabled,this.listModeDisabled],()=>this._recompileList()),i(()=>o(this.view)?this.view.inGeographicLayout:null,()=>this._compileList())],m.view)}destroy(){this._removeAllItems(),this.view=null}get state(){const{view:e}=this;return e?.ready?"ready":e?"loading":"disabled"}get totalItems(){return this.operationalItems.flatten(e=>e.children).length}triggerAction(e,t){e&&!e.disabled&&this.emit("trigger-action",{action:e,item:t})}moveListItem(e,t,s,i){const a=e?.layer;if(!a||"subtype-sublayer"===a.type||"sublayer"===a.type)return;const r=this.view?.map?.layers,l=t?d(t):r,o=s?d(s):r;if(!l||!o)return;const{operationalItems:n}=this,h=t?.children||n,m=s?.children||n,c=o.length-i;e.parent=s||null,h.remove(e),l.remove(a),m.includes(e)||m.add(e,c),o.includes(a)||o.add(a,c),this._compileList()}_createLayerViewHandles(e){this.removeHandles(m.layerViews),this._compileList(),e&&this.addHandles(e.on("change",()=>this._compileList()),m.layerViews)}_createMapLayerHandles(e){this.removeHandles(m.mapLayers),this._compileList(),e&&this.addHandles(e.on("change",()=>this._compileList()),m.mapLayers)}_createListItem(e){const{view:t,listItemCreatedFunction:s,checkPublishStatusEnabled:i,listModeDisabled:a}=this;return new n({checkPublishStatusEnabled:i,listModeDisabled:a,layer:e,listItemCreatedFunction:s,view:t})}_removeAllItems(){this.operationalItems.destroyAll()}_getViewableLayers(e){return e?this.listModeDisabled?e:e.filter(e=>h(e)!==c):void 0}_watchLayersListMode(e){this.removeHandles(m.layerListMode),e&&!this.listModeDisabled&&this.addHandles(i(()=>e.filter(e=>"listMode"in e).map(e=>e.listMode).toArray(),()=>this._compileList()),m.layerListMode)}_compileList(){const e=this.view?.map?.layers,t=o(this.view)&&!this.view.inGeographicLayout?e?.filter(({type:e})=>"link-chart"===e):e;this._watchLayersListMode(t);const s=this._getViewableLayers(t);s?.length?(this._createNewItems(s),this._removeItems(s),this._sortItems(s)):this._removeAllItems()}_createNewItems(e){const{operationalItems:t}=this;e.forEach(e=>{t.some(t=>t.layer===e)||t.add(this._createListItem(e))})}_removeItems(e){const{operationalItems:t}=this,s=[];t.forEach(t=>{t&&e&&e.includes(t.layer)||s.push(t)}),t.destroyMany(s)}_sortItems(e){const{operationalItems:t}=this;t.sort((t,s)=>{const i=e.indexOf(t.layer),a=e.indexOf(s.layer);return i>a?-1:i<a?1:0})}_recompileList(){this._removeAllItems(),this._compileList()}_viewHandles(){const{view:e}=this;this.removeHandles([m.mapLayers,m.layerViews,m.viewLayers]),e?.ready?this.addHandles([i(()=>this.view?.map?.allLayers,e=>this._createMapLayerHandles(e),a),i(()=>this.view?.allLayerViews,e=>this._createLayerViewHandles(e),a)],m.viewLayers):this._removeAllItems()}};e([r()],y.prototype,"checkPublishStatusEnabled",void 0),e([r()],y.prototype,"listItemCreatedFunction",void 0),e([r({nonNullable:!0})],y.prototype,"listModeDisabled",void 0),e([r({type:p})],y.prototype,"operationalItems",void 0),e([r({readOnly:!0})],y.prototype,"state",null),e([r()],y.prototype,"totalItems",null),e([r()],y.prototype,"view",void 0),y=e([l("esri.widgets.LayerList.LayerListViewModel")],y);const v=y;export{v as default};
