/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import e from"../../config.js";import{id as o}from"../../kernel.js";import t from"../../PopupTemplate.js";import r from"../../request.js";import{clone as s}from"../../core/lang.js";import{addQueryParameters as n}from"../../core/urlUtils.js";import i from"../../geometry/SpatialReference.js";import{create as f,negativeInfinity as a,expandWithAABB as l,equals as u}from"../../geometry/support/aaBoundingBox.js";import{getBoundsXYZ as m}from"../../geometry/support/boundsUtils.js";import{fromJSON as p}from"../../renderers/support/jsonUtils.js";import y from"../../rest/support/FeatureSet.js";const c={esriGeometryPoint:"points",esriGeometryPolyline:"polylines",esriGeometryPolygon:"polygons"};function d(e){const o=e.folders||[],t=o.slice(),r=new Map,n=new Map,i=new Map,f=new Map,a=new Map,l={esriGeometryPoint:n,esriGeometryPolyline:i,esriGeometryPolygon:f};(e.featureCollection?.layers||[]).forEach(e=>{const o=s(e);o.featureSet.features=[];const t=e.featureSet.geometryType;r.set(t,o);const a=e.layerDefinition.objectIdField;"esriGeometryPoint"===t?h(n,a,e.featureSet.features):"esriGeometryPolyline"===t?h(i,a,e.featureSet.features):"esriGeometryPolygon"===t&&h(f,a,e.featureSet.features)}),e.groundOverlays&&e.groundOverlays.forEach(e=>{a.set(e.id,e)}),o.forEach(o=>{o.networkLinkIds.forEach(r=>{const s=P(r,o.id,e.networkLinks);s&&t.push(s)})}),t.forEach(e=>{if(e.featureInfos){e.points=s(r.get("esriGeometryPoint")),e.polylines=s(r.get("esriGeometryPolyline")),e.polygons=s(r.get("esriGeometryPolygon")),e.mapImages=[];for(const o of e.featureInfos)switch(o.type){case"esriGeometryPoint":case"esriGeometryPolyline":case"esriGeometryPolygon":{const t=l[o.type].get(o.id);t&&e[c[o.type]]?.featureSet.features.push(t);break}case"GroundOverlay":{const t=a.get(o.id);t&&e.mapImages.push(t);break}}e.fullExtent=I([e])}});const u=I(t);return{folders:o,sublayers:t,extent:u}}function g(t,s,i,f){const a=o?.findCredential(t);t=n(t,{token:a?.token});const l=e.kmlServiceUrl;return r(l,{query:{url:t,model:"simple",folders:"",refresh:0!==i||void 0,outSR:JSON.stringify(s)},responseType:"json",signal:f})}function S(e,o,t=null,r=[]){const s=[],n={},i=o.sublayers,f=new Set(o.folders.map(e=>e.id));return i.forEach(o=>{const i=new e;if(t?i.read(o,t):i.read(o),r.length&&f.has(i.id)&&(i.visible=r.includes(i.id)),n[o.id]=i,null!=o.parentFolderId&&-1!==o.parentFolderId){const e=n[o.parentFolderId];e.sublayers||(e.sublayers=[]),e.sublayers?.unshift(i)}else s.unshift(i)}),s}function h(e,o,t){t.forEach(t=>{e.set(t.attributes[o],t)})}function G(e,o){let t;return o.some(o=>o.id===e&&(t=o,!0)),t}function P(e,o,t){const r=G(e,t);return r&&(r.parentFolderId=o,r.networkLink=r),r}async function b(e,o,r,s){const n=e[o];if(!n)return[];const i=y.fromJSON(n.featureSet).features,f=n.layerDefinition,a=p(f.drawingInfo.renderer),l=t.fromJSON(n.popupInfo),u=[];for(const t of i){const e=await a.getSymbolAsync(t);t.symbol=e,t.popupTemplate=l,t.visible=!0;const o=r.sublayerById.get(s);t.origin=o.origin,u.push(t)}return u}function I(e){const o=f(a),t=f(a);for(const r of e){if(r.polygons?.featureSet?.features)for(const e of r.polygons.featureSet.features)m(o,e.geometry),l(t,o);if(r.polylines?.featureSet?.features)for(const e of r.polylines.featureSet.features)m(o,e.geometry),l(t,o);if(r.points?.featureSet?.features)for(const e of r.points.featureSet.features)m(o,e.geometry),l(t,o);if(r.mapImages)for(const e of r.mapImages)m(o,e.extent),l(t,o)}return u(t,a)?void 0:{xmin:t[0],ymin:t[1],zmin:t[2],xmax:t[3],ymax:t[4],zmax:t[5],spatialReference:i.WGS84}}export{I as computeExtent,g as fetchService,b as getGraphics,d as parseKML,S as sublayersFromJSON};
