/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
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 o from"../../layers/support/MapImage.js";import{parseUrl as n,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 p from"../support/LinearUnit.js";import c from"../support/ParameterValue.js";import{dataTypeJsonMap as f,multiValuePrefix as l}from"../support/parameterValueUtils.js";import S from"../support/RasterData.js";import y from"../support/TimeUnit.js";async function d(r,a,o,s,i){const u={},m={},p=[];return N(s,p,u),t(p).then(t=>{const{outSpatialReference:p,processExtent:c,processSpatialReference:f,returnColumnName:l,returnFeatureCollection:S,returnM:y,returnZ:d}=o,{path:N}=n(r);for(const e in u){const[r,a]=u[e];m[e]=t.slice(r,a)}const j=p?p.wkid||p:null,J=f?f.wkid||f:null,O="execute"===a?{returnColumnName:l||void 0,returnFeatureCollection:S||void 0,returnM:y||void 0,returnZ:d||void 0}:null,g=v({...c?{context:{extent:c,outSR:j,processSR:J}}:{"env:outSR":j,"env:processSR":J},...s,...O,f:"json"},null,m),R={...i,query:g};return e(`${N}/${a}`,R)})}function N(e,r,t){for(const a in e){const o=e[a];if(o&&"object"==typeof o&&o instanceof m){const{features:e}=o;t[a]=[r.length,r.length+e.length],e.forEach(e=>{r.push(e.geometry)})}}}async function j(e,t){switch(e){case"time-unit":return y.fromJSON(t);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 p.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:o}=t;return e.read({layerDefinition:a,featureSet:o},{origin:"portal-item"}),e.spatialReference=r.fromJSON(o.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?o.fromJSON(t.mapImage):S.fromJSON(t);case"field":return a.fromJSON(t)}}function J(e){return e.startsWith(l)}function O(e){return e.replace(l,"")}async function g(e,r){const t=O(e),a="composite"===t?r.map(e=>j(f.fromJSON(e.dataType),e.value)):r.map(e=>j(t,e));return Promise.all(a)}async function R(e){const r=f.fromJSON(e.dataType),{paramName:t}=e,a=J(r)?await g(r,e.value):await j(r,e.value);return new c({dataType:r,paramName:t,value:a})}function v(e,r,t){for(const a in e){const r=e[a];Array.isArray(r)?e[a]=JSON.stringify(r.map(e=>v({item:e},!0).item)):r instanceof Date&&(e[a]=r.getTime())}return s(e,r,t)}export{N as collectGeometries,d as constructRequest,R as decode,v as gpEncode};
