/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"../../request.js";import r from"../../geometry/SpatialReference.js";import{normalizeCentralMeridian as t}from"../../geometry/support/normalizeUtils.js";import a from"../../layers/support/Field.js";import n from"../../layers/support/MapImage.js";import{parseUrl as o,encode as s}from"../utils.js";import i from"../support/ArealUnit.js";import u from"../support/DataFile.js";import m from"../support/FeatureSet.js";import c from"../support/LinearUnit.js";import p from"../support/ParameterValue.js";import{dataTypeJsonMap as f,multiValuePrefix as l}from"../support/parameterValueUtils.js";import y from"../support/RasterData.js";async function S(r,a,n,s,i){const u={},m={},c=[];return d(s,c,u),t(c).then(t=>{const{outSpatialReference:c,processExtent:p,processSpatialReference:f,returnColumnName:l,returnFeatureCollection:y,returnM:S,returnZ:d}=n,{path:N}=o(r);for(const e in u){const[r,a]=u[e];m[e]=t.slice(r,a)}const j=c?c.wkid||c:null,g=f?f.wkid||f:null,J="execute"===a?{returnColumnName:l||void 0,returnFeatureCollection:y||void 0,returnM:S||void 0,returnZ:d||void 0}:null,O=R({...p?{context:{extent:p,outSR:j,processSR:g}}:{"env:outSR":j,"env:processSR":g},...s,...J,f:"json"},null,m),v={...i,query:O};return e(`${N}/${a}`,v)})}function d(e,r,t){for(const a in e){const n=e[a];if(n&&"object"==typeof n&&n instanceof m){const{features:e}=n;t[a]=[r.length,r.length+e.length],e.forEach(e=>{r.push(e.geometry)})}}}async function N(e,t){switch(e){case"areal-unit":return i.fromJSON(t);case"boolean":case"double":case"long":case"string":case"value-table":return t;case"date":return new Date(t);case"data-file":return u.fromJSON(t);case"linear-unit":return c.fromJSON(t);case"feature-record-set-layer":if("url"in t)return u.fromJSON(t);if("layerDefinition"in t){const e=new(0,(await import("../../layers/FeatureLayer.js")).default),{layerDefinition:a,featureSet:n}=t;return e.read({layerDefinition:a,featureSet:n},{origin:"portal-item"}),e.spatialReference=r.fromJSON(n.spatialReference??a.spatialReference??a.extent.spatialReference),e}return m.fromJSON(t);case"record-set":return"url"in t?u.fromJSON(t):m.fromJSON(t);case"raster-data":case"raster-data-layer":return"mapImage"in t?n.fromJSON(t.mapImage):y.fromJSON(t);case"field":return a.fromJSON(t)}}function j(e){return e.startsWith(l)}function g(e){return e.replace(l,"")}async function J(e,r){const t=g(e),a="composite"===t?r.map(e=>N(f.fromJSON(e.dataType),e.value)):r.map(e=>N(t,e));return Promise.all(a)}async function O(e){const r=f.fromJSON(e.dataType),{paramName:t}=e,a=j(r)?await J(r,e.value):await N(r,e.value);return new p({dataType:r,paramName:t,value:a})}function R(e,r,t){for(const a in e){const r=e[a];Array.isArray(r)?e[a]=JSON.stringify(r.map(e=>R({item:e},!0).item)):r instanceof Date&&(e[a]=r.getTime())}return s(e,r,t)}export{d as collectGeometries,S as constructRequest,O as decode,R as gpEncode};
