/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import t from"../../../request.js";import{urlToObject as e,join as r}from"../../../core/urlUtils.js";import{getJsonType as n}from"../../../geometry/support/jsonTypeUtils.js";import{normalizeCentralMeridian as o}from"../../../geometry/support/normalizeUtils.js";import{srToRESTValue as i}from"../../../geometry/support/spatialReferenceUtils.js";import{mapParameters as s}from"../../operations/urlUtils.js";import{applyFeatureSetZUnitScaling as l}from"./queryZScale.js";const u="Layer does not support extent calculation.";function y(t,e){const r=t.geometry,o=t.toJSON(),s=o;if(null!=r&&(s.geometry=JSON.stringify(r),s.geometryType=n(r),s.inSR=i(r.spatialReference)),o.topFilter?.groupByFields&&(s.topFilter.groupByFields=o.topFilter.groupByFields.join(",")),o.topFilter?.orderByFields&&(s.topFilter.orderByFields=o.topFilter.orderByFields.join(",")),o.topFilter&&(s.topFilter=JSON.stringify(s.topFilter)),o.objectIds&&(s.objectIds=o.objectIds.join(",")),o.orderByFields&&(s.orderByFields=o.orderByFields.join(",")),o.outFields&&!(e?.returnCountOnly||e?.returnExtentOnly||e?.returnIdsOnly)?o.outFields.includes("*")?s.outFields="*":s.outFields=o.outFields.join(","):delete s.outFields,o.outSR?s.outSR=i(o.outSR):r&&o.returnGeometry&&(s.outSR=s.inSR),o.returnGeometry&&delete o.returnGeometry,o.timeExtent){const t=o.timeExtent,{start:e,end:r}=t;null==e&&null==r||(s.time=e===r?e:`${e??"null"},${r??"null"}`),delete o.timeExtent}return s}async function p(t,e,r,n){const o=await a(t,e,"json",n);return l(e,r,o.data),o}async function m(t,e,r){return null!=e.timeExtent&&e.timeExtent.isEmpty?{data:{objectIds:[]}}:a(t,e,"json",r,{returnIdsOnly:!0})}async function d(t,e,r){return null!=e.timeExtent&&e.timeExtent.isEmpty?{data:{count:0,extent:null}}:a(t,e,"json",r,{returnExtentOnly:!0,returnCountOnly:!0}).then(t=>{const e=t.data;if(e.hasOwnProperty("extent"))return t;if(e.features)throw new Error(u);if(e.hasOwnProperty("count"))throw new Error(u);return t})}function c(t,e,r){return null!=e.timeExtent&&e.timeExtent.isEmpty?Promise.resolve({data:{count:0}}):a(t,e,"json",r,{returnIdsOnly:!0,returnCountOnly:!0})}function a(n,i,l,u={},p={}){const m="string"==typeof n?e(n):n,d=i.geometry?[i.geometry]:[];return u.responseType="json",o(d,null,u).then(e=>{const n=e?.[0];null!=n&&((i=i.clone()).geometry=n);const o=s({...m.query,f:l,...p,...y(i,p)});return t(r(m.path,"queryTopFeatures"),{...u,query:{...o,...u.query}})})}export{c as executeQueryForTopCount,d as executeQueryForTopExtents,m as executeQueryForTopIds,p as executeTopFeaturesQuery,y as queryToQueryStringParameters};
