/*
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/CollectionFlattener","../core/loadAll","../core/maybe","../core/MultiOriginJSONSupport","../core/reactiveUtils","../core/accessorSupport/decorators/property","../core/arrayUtils","../core/has","../core/accessorSupport/utils","../core/accessorSupport/ensureType","../core/accessorSupport/decorators/subclass","../core/accessorSupport/decorators/writer","./Layer","./mixins/BlendLayer","./mixins/OperationalLayer","./mixins/PortalLayer","./mixins/ScaleRangeLayer","../support/collectionUtils","../support/LayersMixin","../support/TablesMixin","../webdoc/support/writeUtils"],(function(e,i,t,r,s,o,l,a,n,y,c,p,d,u,h,b,v,_,f,L,g,m,O){"use strict";let w=function(i){function o(r){var s;return(s=i.call(this,r)||this)._visibilityHandles={},s.allLayers=new t({getCollections:()=>[s.layers],getChildrenFunction:e=>"layers"in e?e.layers:null}),s.allTables=L.createFlattenedTablesCollection(e._assertThisInitialized(s)),s.fullExtent=void 0,s.operationalLayerType="GroupLayer",s.spatialReference=void 0,s.type="group",s}e._inheritsLoose(o,i);var a=o.prototype;return a.initialize=function(){this._enforceVisibility(this.visibilityMode,this.visible),this.own(l.watch((()=>this.visible),this._onVisibilityChange.bind(this),l.sync))},a._writeLayers=function(e,i,t,r){const o=[];if(!e)return o;e.forEach((e=>{const i=O.getLayerJSON(e,r.webmap?r.webmap.getLayerJSONFromResourceInfo(e):null,r);s.isSome(i)&&i.layerType&&o.push(i)})),i.layers=o},a.load=function(e){return this.addResolvingPromise(this.loadFromPortal({supportedTypes:["Feature Service","Feature Collection","Scene Service"]},e)),Promise.resolve(this)},a.loadAll=function(){return r.loadAll(this,(e=>{e(this.layers,this.tables)}))},a.layerAdded=function(e){e.visible&&"exclusive"===this.visibilityMode?this._turnOffOtherLayers(e):"inherited"===this.visibilityMode&&(e.visible=this.visible),this._visibilityHandles[e.uid]=l.watch((()=>e.visible),(i=>this._onChildVisibilityChange(e,i)),l.sync)},a.layerRemoved=function(e){const i=this._visibilityHandles[e.uid];i&&(i.remove(),delete this._visibilityHandles[e.uid]),this._enforceVisibility(this.visibilityMode,this.visible)},a._turnOffOtherLayers=function(e){this.layers.forEach((i=>{i!==e&&(i.visible=!1)}))},a._enforceVisibility=function(e,i){if(!c.getProperties(this).initialized)return;const t=this.layers;let r=t.find((e=>e.visible));switch(e){case"exclusive":t.length&&!r&&(r=t.getItemAt(0),r.visible=!0),this._turnOffOtherLayers(r);break;case"inherited":t.forEach((e=>{e.visible=i}))}},a._onVisibilityChange=function(e){"inherited"===this.visibilityMode&&this.layers.forEach((i=>{i.visible=e}))},a._onChildVisibilityChange=function(e,i){switch(this.visibilityMode){case"exclusive":i?this._turnOffOtherLayers(e):this._isAnyLayerVisible()||(e.visible=!0);break;case"inherited":e.visible=this.visible}},a._isAnyLayerVisible=function(){return this.layers.some((e=>e.visible))},e._createClass(o,[{key:"portalItem",set:function(e){this._set("portalItem",e)}},{key:"visibilityMode",set:function(e){const i=this._get("visibilityMode")!==e;this._set("visibilityMode",e),i&&this._enforceVisibility(e,this.visible)}}]),o}(b.BlendLayer(f.ScaleRangeLayer(v.OperationalLayer(_.PortalLayer(m.TablesMixin(g.LayersMixin(o.MultiOriginJSONMixin(h))))))));i.__decorate([a.property({readOnly:!0,dependsOn:[]})],w.prototype,"allLayers",void 0),i.__decorate([a.property({readOnly:!0})],w.prototype,"allTables",void 0),i.__decorate([a.property()],w.prototype,"fullExtent",void 0),i.__decorate([a.property({json:{read:!1,write:{ignoreOrigin:!0}}})],w.prototype,"layers",void 0),i.__decorate([u.writer("layers")],w.prototype,"_writeLayers",null),i.__decorate([a.property({type:["GroupLayer"]})],w.prototype,"operationalLayerType",void 0),i.__decorate([a.property({json:{origins:{"web-document":{read:!1,write:!1}}}})],w.prototype,"portalItem",null),i.__decorate([a.property()],w.prototype,"spatialReference",void 0),i.__decorate([a.property({json:{read:!1},readOnly:!0,value:"group"})],w.prototype,"type",void 0),i.__decorate([a.property({type:["independent","inherited","exclusive"],value:"independent",json:{write:!0,origins:{"web-map":{read:!1,write:!1}}}})],w.prototype,"visibilityMode",null),w=i.__decorate([d.subclass("esri.layers.GroupLayer")],w);return w}));
