/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as e}from"tslib";import r from"../core/Collection.js";import t from"../core/CollectionFlattener.js";import{MultiOriginJSONSupportMixin as o}from"../core/MultiOriginJSONSupport.js";import{throwIfAbortError as s}from"../core/promiseUtils.js";import{watch as i,sync as l}from"../core/reactiveUtils.js";import{getFilename as a}from"../core/urlUtils.js";import{property as n,subclass as p}from"../core/accessorSupport/decorators.js";import u from"../geometry/Extent.js";import y from"../geometry/SpatialReference.js";import m from"./Layer.js";import{BlendLayer as c}from"./mixins/BlendLayer.js";import{OperationalLayer as d}from"./mixins/OperationalLayer.js";import{PortalLayer as h}from"./mixins/PortalLayer.js";import{RefreshableLayer as f}from"./mixins/RefreshableLayer.js";import{ScaleRangeLayer as b}from"./mixins/ScaleRangeLayer.js";import{url as v}from"./support/commonProperties.js";import g from"./support/KMLSublayer.js";import{sublayersFromJSON as S,fetchService as j,parseKML as x}from"./support/kmlUtils.js";import{reader as L}from"../core/accessorSupport/decorators/reader.js";import{writer as w}from"../core/accessorSupport/decorators/writer.js";const E=["kml","xml"];let F=class extends(c(f(b(d(h(o(m))))))){constructor(...e){super(...e),this._visibleFolders=[],this.allSublayers=new t({getCollections:()=>[this.sublayers],getChildrenFunction:e=>e.sublayers}),this.outSpatialReference=y.WGS84,this.path=null,this.legendEnabled=!1,this.operationalLayerType="KML",this.sublayers=null,this.type="kml",this.url=null}initialize(){this.addHandles([i(()=>this.sublayers,(e,r)=>{r&&r.forEach(e=>{e.parent=null,e.layer=null}),e&&e.forEach(e=>{e.parent=this,e.layer=this})},l),this.on("sublayer-update",()=>this.notifyChange("fullExtent"))])}normalizeCtorArgs(e,r){return"string"==typeof e?{url:e,...r}:e}get sublayerById(){const e=new Map;for(const r of this.allSublayers)e.set(r.id,r);return e}readSublayersFromItemOrWebMap(e,r){this._visibleFolders=r.visibleFolders}readSublayers(e,r,t){return S(g,r,t,this._visibleFolders)}writeSublayers(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}get title(){const e=this._get("title");return e&&"defaults"!==this.originOf("title")?e:this.url?a(this.url,E)||"KML":e}set title(e){this._set("title",e)}get visibleSublayers(){const e=this.sublayers,r=[],t=e=>{e.visible&&(r.push(e),e.sublayers&&e.sublayers.forEach(t))};return e?.forEach(t),r}get fullExtent(){return this._recomputeFullExtent()}load(e){const r=null!=e?e.signal:null;return this.addResolvingPromise(this.loadFromPortal({supportedTypes:["KML"],supportsData:!1},e).catch(s).then(()=>this._fetchService(r))),Promise.resolve(this)}destroy(){super.destroy(),this.allSublayers.destroy()}async _fetchService(e){const r=await Promise.resolve().then(()=>this.resourceInfo?{ssl:!1,data:this.resourceInfo}:j(this.url??"",this.outSpatialReference,this.refreshInterval,e)),t=x(r.data);t&&this.read(t,{origin:"service"})}_recomputeFullExtent(){let e=null;null!=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&&(null!=e?e.union(o.fullExtent):e=o.fullExtent.clone())};return r(this),e}};e([n({readOnly:!0})],F.prototype,"allSublayers",void 0),e([n({readOnly:!0})],F.prototype,"sublayerById",null),e([n({type:y})],F.prototype,"outSpatialReference",void 0),e([n({type:String,json:{origins:{"web-scene":{read:!0,write:!0}},read:!1}})],F.prototype,"path",void 0),e([n({readOnly:!0,json:{read:!1,write:!1}})],F.prototype,"legendEnabled",void 0),e([n({type:["show","hide","hide-children"]})],F.prototype,"listMode",void 0),e([n({type:["KML"]})],F.prototype,"operationalLayerType",void 0),e([n({})],F.prototype,"resourceInfo",void 0),e([n({type:r.ofType(g),json:{write:{ignoreOrigin:!0}}})],F.prototype,"sublayers",void 0),e([L(["web-map","portal-item"],"sublayers",["visibleFolders"])],F.prototype,"readSublayersFromItemOrWebMap",null),e([L("service","sublayers",["sublayers"])],F.prototype,"readSublayers",null),e([w("sublayers")],F.prototype,"writeSublayers",null),e([n({readOnly:!0,json:{read:!1}})],F.prototype,"type",void 0),e([n({json:{origins:{"web-map":{read:{source:"title"}}},write:{ignoreOrigin:!0}}})],F.prototype,"title",null),e([n(v)],F.prototype,"url",void 0),e([n({readOnly:!0})],F.prototype,"visibleSublayers",null),e([n({type:u})],F.prototype,"extent",void 0),e([n()],F.prototype,"fullExtent",null),F=e([p("esri.layers.KMLLayer")],F);const O=F;export{O as default};
