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