/*
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","./ListItem","./support/layerListUtils"],(function(e,t,i,s,r,a,n,o,l,c,d,h,u){"use strict";const m={view:"view",viewLayers:"view-layers",mapLayers:"map-layers",layerViews:"layer-views",layerListMode:"layer-list-mode",children:"children"},y="hide",p=i.ofType(h);let v=function(t){function i(e){var i;return(i=t.call(this,e)||this)._handles=new r,i.listItemCreatedFunction=null,i.operationalItems=new p,i.view=null,i}e._inheritsLoose(i,t);var s=i.prototype;return s.initialize=function(){this._handles.add([a.watch((()=>{var e;return[this.view,null==(e=this.view)?void 0:e.ready]}),(()=>this._viewHandles()),a.initial),a.watch((()=>this.listItemCreatedFunction),(()=>this._listItemCreatedFunctionChanged()))],m.view)},s.destroy=function(){this._handles.destroy(),this._handles=null,this.view=null,this.operationalItems.removeAll()},s.triggerAction=function(e,t){e&&!e.disabled&&this.emit("trigger-action",{action:e,item:t})},s.moveListItem=function(e,t,i,s){var r,a;const n=null==e?void 0:e.layer;if(!n)return;const o=null==(r=this.view)||null==(a=r.map)?void 0:a.layers,l=t?u.getItemLayers(t):o,c=i?u.getItemLayers(i):o;if(!l||!c)return;const{operationalItems:d}=this,h=(null==t?void 0:t.children)||d,m=(null==i?void 0:i.children)||d,y=c.length-s;e.parent=i||null,h.includes(e)&&h.remove(e),l.includes(n)&&l.remove(n),m.includes(e)||m.add(e,y),c.includes(n)||c.add(n,y)},s._createLayerViewHandles=function(e){const{_handles:t}=this;t.remove(m.layerViews),this._compileList(),e&&t.add(e.on("change",(()=>this._compileList())),m.layerViews)},s._createMapLayerHandles=function(e){const{_handles:t}=this;t.remove(m.mapLayers),this._compileList(),e&&t.add(e.on("change",(()=>this._compileList())),m.mapLayers)},s._createListItem=function(e){const{view:t,listItemCreatedFunction:i}=this;return new h({layer:e,listItemCreatedFunction:i,view:t})},s._removeAllItems=function(){const{_handles:e,operationalItems:t}=this;t.forEach((t=>{e.remove(`${m.children}-${t.uid}`)})),t.removeAll()},s._getViewableLayers=function(e){if(e)return e.filter((e=>u.findLayerListMode(e)!==y))},s._watchLayersListMode=function(e){const{_handles:t}=this;t.remove(m.layerListMode),e&&e.forEach((e=>{"listMode"in e&&t.add(a.watch((()=>e.listMode),(()=>this._compileList())),m.layerListMode)}))},s._compileList=function(){const e=this.get("view.map.layers");this._watchLayersListMode(e);const t=this._getViewableLayers(e);t&&t.length?(this._createNewItems(t),this._removeItems(t),this._sortItems(t)):this._removeAllItems()},s._createNewItems=function(e){const{operationalItems:t}=this;e.forEach((e=>{t.find((t=>t.layer===e))||t.add(this._createListItem(e))}))},s._removeItems=function(e){const{_handles:t,operationalItems:i}=this,s=[];i.forEach((i=>{i&&e&&e.includes(i.layer)||(t.remove(`${m.children}-${i.uid}`),s.push(i))})),i.removeMany(s)},s._sortItems=function(e){const{operationalItems:t}=this;t.sort(((t,i)=>{const s=e.indexOf(t.layer),r=e.indexOf(i.layer);return s>r?-1:s<r?1:0}))},s._listItemCreatedFunctionChanged=function(){this._removeAllItems(),this._compileList()},s._viewHandles=function(){const{_handles:e,view:t}=this;e.remove([m.mapLayers,m.layerViews,m.viewLayers]),this._compileList(),t&&t.ready&&e.add([a.watch((()=>{var e,t;return null==(e=this.view)||null==(t=e.map)?void 0:t.allLayers}),(e=>this._createMapLayerHandles(e)),a.initial),a.watch((()=>{var e;return null==(e=this.view)?void 0:e.allLayerViews}),(e=>this._createLayerViewHandles(e)),a.initial)],m.viewLayers)},e._createClass(i,[{key:"state",get:function(){const e=this.get("view");return this.get("view.ready")?"ready":e?"loading":"disabled"}}]),i}(s.EventedAccessor);t.__decorate([n.property()],v.prototype,"listItemCreatedFunction",void 0),t.__decorate([n.property({type:p})],v.prototype,"operationalItems",void 0),t.__decorate([n.property({readOnly:!0})],v.prototype,"state",null),t.__decorate([n.property()],v.prototype,"view",void 0),v=t.__decorate([d.subclass("esri.widgets.LayerList.LayerListViewModel")],v);return v}));
