/*
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/CollectionFlattener","../core/maybe","../core/MultiOriginJSONSupport","../core/promiseUtils","../core/reactiveUtils","../core/urlUtils","../core/accessorSupport/decorators/property","../core/arrayUtils","../core/has","../core/accessorSupport/ensureType","../core/accessorSupport/decorators/reader","../core/accessorSupport/decorators/subclass","../core/accessorSupport/decorators/writer","../geometry/SpatialReference","./Layer","./mixins/BlendLayer","./mixins/OperationalLayer","./mixins/PortalLayer","./mixins/RefreshableLayer","./mixins/ScaleRangeLayer","./support/commonProperties","./support/KMLSublayer","./support/kmlUtils","../geometry/Extent"],(function(e,r,t,o,s,i,l,a,n,p,u,c,y,d,h,b,f,_,m,v,S,g,L,x,w,E,O,F){"use strict";const M=["kml","xml"];let k=function(r){function t(...e){var t;return(t=r.call(this,...e)||this)._visibleFolders=[],t.allSublayers=new s({getCollections:()=>[t.sublayers],getChildrenFunction:e=>e.sublayers}),t.outSpatialReference=_.WGS84,t.path=null,t.legendEnabled=!1,t.operationalLayerType="KML",t.sublayers=null,t.type="kml",t.url=null,t}e._inheritsLoose(t,r);var o=t.prototype;return o.initialize=function(){this.own([n.watch((()=>this.sublayers),((e,r)=>{r&&r.forEach((e=>{e.parent=null,e.layer=null})),e&&e.forEach((e=>{e.parent=this,e.layer=this}))}),n.sync),this.on("sublayer-update",(()=>this.notifyChange("fullExtent")))])},o.normalizeCtorArgs=function(e,r){return"string"==typeof e?{url:e,...r}:e},o.readSublayersFromItemOrWebMap=function(e,r){this._visibleFolders=r.visibleFolders},o.readSublayers=function(e,r,t){return O.sublayersFromJSON(E,r,t,this._visibleFolders)},o.writeSublayers=function(e,r){const t=[],o=e.toArray();for(;o.length;){const e=o[0];e.networkLink||(e.visible&&t.push(e.id),e.sublayers&&o.push(...e.sublayers.toArray())),o.shift()}r.visibleFolders=t},o.load=function(e){const r=i.isSome(e)?e.signal:null;return this.addResolvingPromise(this.loadFromPortal({supportedTypes:["KML"]},e).catch(a.throwIfAbortError).then((()=>this._fetchService(r)))),Promise.resolve(this)},o.destroy=function(){r.prototype.destroy.call(this),this.allSublayers.destroy()},o._fetchService=function(){var r=e._asyncToGenerator((function*(e){const r=yield Promise.resolve().then((()=>this.resourceInfo?{ssl:!1,data:this.resourceInfo}:O.fetchService(this.url,this.outSpatialReference,this.refreshInterval,e))),t=O.parseKML(r.data);t&&this.read(t,{origin:"service"})}));function t(e){return r.apply(this,arguments)}return t}(),o._recomputeFullExtent=function(){let e=null;i.isSome(this.extent)&&(e=this.extent.clone());const r=t=>{if(t.sublayers)for(const o of t.sublayers.items)r(o),o.visible&&o.fullExtent&&(i.isSome(e)?e.union(o.fullExtent):e=o.fullExtent.clone())};return r(this),e},e._createClass(t,[{key:"title",get:function(){const e=this._get("title");return e&&"defaults"!==this.originOf("title")?e:this.url?p.getFilename(this.url,M)||"KML":e||""},set:function(e){this._set("title",e)}},{key:"visibleSublayers",get:function(){const e=this.sublayers,r=[],t=e=>{e.visible&&(r.push(e),e.sublayers&&e.sublayers.forEach(t))};return e&&e.forEach(t),r}},{key:"fullExtent",get:function(){return this._recomputeFullExtent()}}]),t}(v.BlendLayer(L.RefreshableLayer(x.ScaleRangeLayer(S.OperationalLayer(g.PortalLayer(l.MultiOriginJSONMixin(m)))))));r.__decorate([u.property({readOnly:!0})],k.prototype,"allSublayers",void 0),r.__decorate([u.property({type:_})],k.prototype,"outSpatialReference",void 0),r.__decorate([u.property({type:String,json:{origins:{"web-scene":{read:!0,write:!0}},read:!1}})],k.prototype,"path",void 0),r.__decorate([u.property({readOnly:!0,json:{read:!1,write:!1}})],k.prototype,"legendEnabled",void 0),r.__decorate([u.property({type:["show","hide","hide-children"]})],k.prototype,"listMode",void 0),r.__decorate([u.property({type:["KML"]})],k.prototype,"operationalLayerType",void 0),r.__decorate([u.property({})],k.prototype,"resourceInfo",void 0),r.__decorate([u.property({type:o.ofType(E),json:{write:{ignoreOrigin:!0}}})],k.prototype,"sublayers",void 0),r.__decorate([h.reader(["web-map","portal-item"],"sublayers",["visibleFolders"])],k.prototype,"readSublayersFromItemOrWebMap",null),r.__decorate([h.reader("service","sublayers",["sublayers"])],k.prototype,"readSublayers",null),r.__decorate([f.writer("sublayers")],k.prototype,"writeSublayers",null),r.__decorate([u.property({readOnly:!0,json:{read:!1}})],k.prototype,"type",void 0),r.__decorate([u.property({json:{origins:{"web-map":{read:{source:"title"}}},write:{ignoreOrigin:!0}}})],k.prototype,"title",null),r.__decorate([u.property(w.url)],k.prototype,"url",void 0),r.__decorate([u.property({readOnly:!0})],k.prototype,"visibleSublayers",null),r.__decorate([u.property({type:F})],k.prototype,"extent",void 0),r.__decorate([u.property()],k.prototype,"fullExtent",null),k=r.__decorate([b.subclass("esri.layers.KMLLayer")],k);return k}));
