/*
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","../../geometry","../../core/Collection","../../core/Evented","../../core/JSONSupport","../../core/Loadable","../../core/maybe","../../core/reactiveUtils","../../core/string","../../core/accessorSupport/decorators/property","../../core/arrayUtils","../../core/has","../../core/accessorSupport/decorators/cast","../../core/accessorSupport/decorators/reader","../../core/accessorSupport/decorators/subclass","../../core/accessorSupport/ensureType","./kmlUtils","../../geometry/Extent"],(function(e,r,t,s,o,l,i,a,n,u,c,p,y,h,d,_,b,v,f){"use strict";var S;let k=S=function(r){function t(){var e;return(e=r.apply(this,arguments)||this)._sublayersHandles=null,e.description=null,e.id=null,e.networkLink=null,e.title=null,e.sourceJSON=null,e.fullExtent=null,e}e._inheritsLoose(t,r);var o=t.prototype;return o.initialize=function(){n.whenOnce((()=>this.networkLink)).then((()=>n.whenOnce((()=>!0===this.visible)))).then((()=>this.load()))},o.load=function(e){if(!this.networkLink)return;if(this.networkLink.viewFormat)return;const r=a.isSome(e)?e.signal:null,t=this._fetchService(this._get("networkLink").href,r).then((e=>{const r=v.computeExtent(e.sublayers);this.fullExtent=f.fromJSON(r),this.sourceJSON=e;const t=b.ensureType(s.ofType(S),v.sublayersFromJSON(S,e));this.sublayers?this.sublayers.addMany(t):this.sublayers=t,this.layer.emit("sublayer-update"),this.layer&&this.layer.notifyChange("visibleSublayers")}));return this.addResolvingPromise(t),Promise.resolve(this)},o.castSublayers=function(e){return b.ensureType(s.ofType(S),e)},o.readVisible=function(e,r){return!!r.visibility},o._fetchService=function(e,r){return v.fetchService(e,this.layer.outSpatialReference,this.layer.refreshInterval,r).then((e=>v.parseKML(e.data)))},e._createClass(t,[{key:"sublayers",set:function(e){const r=this._get("sublayers");r&&(r.forEach((e=>{e.parent=null,e.layer=null})),this._sublayersHandles.forEach((e=>e.remove())),this._sublayersHandles=null),e&&(e.forEach((e=>{e.parent=this,e.layer=this.layer})),this._sublayersHandles=[e.on("after-add",(({item:e})=>{e.parent=this,e.layer=this.layer})),e.on("after-remove",(({item:e})=>{e.parent=null,e.layer=null}))]),this._set("sublayers",e)}},{key:"visible",get:function(){return this._get("visible")},set:function(e){this._get("visible")!==e&&(this._set("visible",e),this.layer&&this.layer.notifyChange("visibleSublayers"))}},{key:"layer",set:function(e){this._set("layer",e),this.sublayers&&this.sublayers.forEach((r=>r.layer=e))}}]),t}(o.EventedMixin(l.JSONSupportMixin(i)));r.__decorate([c.property()],k.prototype,"description",void 0),r.__decorate([c.property()],k.prototype,"id",void 0),r.__decorate([c.property({readOnly:!0,value:null})],k.prototype,"networkLink",void 0),r.__decorate([c.property({json:{write:{allowNull:!0}}})],k.prototype,"parent",void 0),r.__decorate([c.property({value:null,json:{write:{allowNull:!0}}})],k.prototype,"sublayers",null),r.__decorate([h.cast("sublayers")],k.prototype,"castSublayers",null),r.__decorate([c.property({value:null,json:{read:{source:"name",reader:e=>u.stripHTML(e)}}})],k.prototype,"title",void 0),r.__decorate([c.property({value:!0})],k.prototype,"visible",null),r.__decorate([d.reader("visible",["visibility"])],k.prototype,"readVisible",null),r.__decorate([c.property()],k.prototype,"sourceJSON",void 0),r.__decorate([c.property({value:null})],k.prototype,"layer",null),r.__decorate([c.property({type:f})],k.prototype,"fullExtent",void 0),k=S=r.__decorate([_.subclass("esri.layers.support.KMLSublayer")],k);return k}));
