/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{__decorate as e}from"tslib";import r from"../../core/Collection.js";import s from"../../core/CollectionFlattener.js";import t from"../../core/Error.js";import a from"../../core/Logger.js";import{watch as l,sync as o}from"../../core/reactiveUtils.js";import{property as i,subclass as n}from"../../core/accessorSupport/decorators.js";import{nameToId as u,idToName as y}from"../../core/accessorSupport/PropertyOrigin.js";import{getProperties as c}from"../../core/accessorSupport/utils.js";import b from"../support/Sublayer.js";import{isSublayerOverhaul as d}from"../support/sublayerUtils.js";function p(e,r){const s=[],t={};return e?(e.forEach(e=>{const a=new b;if(a.read(e,r),t[a.id]=a,null!=e.parentLayerId&&-1!==e.parentLayerId){const r=t[e.parentLayerId];r.sublayers||(r.sublayers=[]),r.sublayers.unshift(a)}else s.unshift(a)}),s):s}const h=r.ofType(b);function f(e,r){e&&e.forEach(e=>{r(e),e.sublayers&&e.sublayers.length&&f(e.sublayers,r)})}const S=S=>{const m=S;let v=class extends m{constructor(...e){super(...e),this.allSublayers=new s({getCollections:()=>[this.sublayers],getChildrenFunction:e=>e.sublayers}),this.sublayersSourceJSON={2:{},3:{},4:{},5:{},6:{}},this.subtables=null,this.addHandles([l(()=>this.sublayers,(e,r)=>this._handleSublayersChange(e,r),o),l(()=>this.subtables,(e,r)=>this._handleSublayersChange(e,r),o)])}destroy(){this.allSublayers.destroy()}readSublayers(e,r){if(!r||!e)return;const{sublayersSourceJSON:s}=this,t=u(r.origin);if(t<2)return;if(s[t]={context:r,visibleLayers:e.visibleLayers||s[t].visibleLayers,layers:e.layers||s[t].layers},t>2)return;this._set("serviceSublayers",this.createSublayersForOrigin("service").sublayers);const{sublayers:a,origin:l}=this.createSublayersForOrigin("web-document"),o=c(this);o.setDefaultOrigin(l),this._set("sublayers",new h(a)),o.setDefaultOrigin("user")}findSublayerById(e){return this.allSublayers.find(r=>r.id===e)}createServiceSublayers(){return this.createSublayersForOrigin("service").sublayers}createSublayersForOrigin(e){let r;const s=u("web-document"===e?"web-map":e);let t=2,a=this.sublayersSourceJSON[2].layers,l=this.sublayersSourceJSON[2].context,o=null;const i=[3,4,5].filter(e=>e<=s);for(const u of i){const e=this.sublayersSourceJSON[u];d(e.layers)&&(t=u,a=e.layers,l=e.context,e.visibleLayers&&(o={visibleLayers:e.visibleLayers,context:e.context}))}const n=[3,4,5].filter(e=>e>t&&e<=s);let c=null;for(const u of n){const{layers:e,visibleLayers:s,context:t}=this.sublayersSourceJSON[u];e&&(c={layers:e,context:t},r??=u),s&&(o={visibleLayers:s,context:t})}const b=p(a,l),S=new Map,m=new Set;if(c)for(const u of c.layers)S.set(u.id,u);if(o?.visibleLayers)for(const u of o.visibleLayers)m.add(u);return f(b,e=>{c&&e.read(S.get(e.id),c.context),o&&e.read({defaultVisibility:m.has(e.id)},o.context)}),{origin:y(t),originWithPartialOverrides:r?y(r):null,sublayers:new h({items:b})}}read(e,r){super.read(e,r),this.readSublayers(e,r)}_handleSublayersChange(e,r){r&&(r.forEach(e=>{e.parent=null,e.layer=null}),this.removeHandles("sublayers-owner")),e&&(e.forEach(e=>{e.parent=this,e.layer=this}),this.addHandles([e.on("after-add",({item:e})=>{e.parent=this,e.layer=this}),e.on("after-remove",({item:e})=>{e.parent=null,e.layer=null})],"sublayers-owner"),"tile"===this.type&&this.addHandles(e.on("before-changes",e=>{a.getLogger("esri.layers.TileLayer").error(new t("tilelayer:sublayers-non-modifiable","Sublayer can't be added, moved, or removed from the layer's sublayers",{layer:this})),e.preventDefault()}),"sublayers-owner"))}};return e([i({readOnly:!0})],v.prototype,"allSublayers",void 0),e([i({readOnly:!0,type:r.ofType(b)})],v.prototype,"serviceSublayers",void 0),e([i({value:null,type:h,json:{read:!1,write:{allowNull:!0,ignoreOrigin:!0}}})],v.prototype,"sublayers",void 0),e([i({readOnly:!0})],v.prototype,"sublayersSourceJSON",void 0),e([i({type:h,json:{read:{source:"tables"}}})],v.prototype,"subtables",void 0),v=e([n("esri.layers.mixins.SublayersOwner")],v),v};export{S as SublayersOwner,f as forEachSublayer};
