/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../chunks/_rollupPluginBabelHelpers","../../chunks/tslib.es6","../../core/Collection","../../core/CollectionFlattener","../../core/Error","../../core/Logger","../../core/reactiveUtils","../../core/accessorSupport/decorators/property","../../core/arrayUtils","../../core/has","../../core/accessorSupport/utils","../../core/accessorSupport/ensureType","../../core/accessorSupport/decorators/subclass","../../core/accessorSupport/PropertyOrigin","../support/Sublayer","../support/sublayerUtils"],(function(e,r,s,t,i,a,o,n,l,u,c,y,d,b,p,S,h){"use strict";const f=o.getLogger("esri.layers.TileLayer");function g(e,r){const s=[],t={};return e?(e.forEach((e=>{const i=new S;if(i.read(e,r),t[i.id]=i,null!=e.parentLayerId&&-1!==e.parentLayerId){const r=t[e.parentLayerId];r.sublayers||(r.sublayers=[]),r.sublayers.unshift(i)}else s.unshift(i)})),s):s}const O=t.ofType(S);function E(e,r){e&&e.forEach((e=>{r(e),e.sublayers&&e.sublayers.length&&E(e.sublayers,r)}))}const I=e=>{let o=function(e){function s(...r){var s;return(s=e.call(this,...r)||this).allSublayers=new i({getCollections:()=>[s.sublayers],getChildrenFunction:e=>e.sublayers}),s.sublayersSourceJSON={[p.OriginId.SERVICE]:{},[p.OriginId.PORTAL_ITEM]:{},[p.OriginId.WEB_SCENE]:{},[p.OriginId.WEB_MAP]:{}},s.own(n.watch((()=>s.sublayers),((e,r)=>s._handleSublayersChange(e,r)),n.sync)),s}r._inheritsLoose(s,e);var t=s.prototype;return t.readSublayers=function(e,r){if(!r||!e)return;const{sublayersSourceJSON:s}=this,t=p.nameToId(r.origin);if(t<p.OriginId.SERVICE)return;if(s[t]={context:r,visibleLayers:e.visibleLayers||s[t].visibleLayers,layers:e.layers||s[t].layers},t>p.OriginId.SERVICE)return;this._set("serviceSublayers",this.createSublayersForOrigin("service").sublayers);const{sublayers:i,origin:a}=this.createSublayersForOrigin("web-document"),o=y.getProperties(this);o.setDefaultOrigin(a),this._set("sublayers",new O(i)),o.setDefaultOrigin("user")},t.findSublayerById=function(e){return this.allSublayers.find((r=>r.id===e))},t.createServiceSublayers=function(){return this.createSublayersForOrigin("service").sublayers},t.createSublayersForOrigin=function(e){const r="web-document"===e?p.nameToId("web-map"):p.nameToId(e);let s=p.OriginId.SERVICE,t=this.sublayersSourceJSON[p.OriginId.SERVICE].layers,i=this.sublayersSourceJSON[p.OriginId.SERVICE].context,a=null;const o=[p.OriginId.PORTAL_ITEM,p.OriginId.WEB_SCENE,p.OriginId.WEB_MAP].filter((e=>e<=r));for(const d of o){const e=this.sublayersSourceJSON[d];h.isSublayerOverhaul(e.layers)&&(s=d,t=e.layers,i=e.context,e.visibleLayers&&(a={visibleLayers:e.visibleLayers,context:e.context}))}const n=[p.OriginId.PORTAL_ITEM,p.OriginId.WEB_SCENE,p.OriginId.WEB_MAP].filter((e=>e>s&&e<=r));let l=null;for(const d of n){const{layers:e,visibleLayers:r,context:s}=this.sublayersSourceJSON[d];e&&(l={layers:e,context:s}),r&&(a={visibleLayers:r,context:s})}const u=g(t,i),c=new Map,y=new Set;if(l)for(const d of l.layers)c.set(d.id,d);if(a)for(const d of a.visibleLayers)y.add(d);return E(u,(e=>{l&&e.read(c.get(e.id),l.context),a&&e.read({defaultVisibility:y.has(e.id)},a.context)})),{origin:p.idToName(s),sublayers:new O({items:u})}},t.read=function(r,s){e.prototype.read.call(this,r,s),this.readSublayers(r,s)},t._handleSublayersChange=function(e,r){r&&(r.forEach((e=>{e.parent=null,e.layer=null})),this.handles.remove("sublayers-owner")),e&&(e.forEach((e=>{e.parent=this,e.layer=this})),this.handles.add([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.handles.add(e.on("before-changes",(e=>{f.error(new a("tilelayer:sublayers-non-modifiable","ISublayer can't be added, moved, or removed from the layer's sublayers",{layer:this})),e.preventDefault()})),"sublayers-owner"))},s}(e);return s.__decorate([l.property({readOnly:!0})],o.prototype,"allSublayers",void 0),s.__decorate([l.property({readOnly:!0,type:t.ofType(S)})],o.prototype,"serviceSublayers",void 0),s.__decorate([l.property({value:null,type:O,json:{read:!1,write:{allowNull:!0,ignoreOrigin:!0}}})],o.prototype,"sublayers",void 0),s.__decorate([l.property({readOnly:!0})],o.prototype,"sublayersSourceJSON",void 0),o=s.__decorate([b.subclass("esri.layers.mixins.SublayersOwner")],o),o};e.SublayersOwner=I,e.forEachSublayer=E,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
