/*
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 n,join as r}from"../../../core/urlUtils.js";import{normalizeCentralMeridian as e}from"../../../geometry/support/normalizeUtils.js";import{mapParameters as o}from"../../operations/urlUtils.js";import{parsePBFFeatureQuery as s}from"./pbfQueryUtils.js";import{queryLikeToQueryStringParameters as u}from"./queryUtils.js";import{applyFeatureSetZUnitScaling as i}from"./queryZScale.js";const c="Layer does not support extent calculation.";function a(t,n,r){return u(t,n,r)}async function y(t,n,r,e,o){const s=n.timeExtent?.isEmpty?{features:[]}:await d(t,n,"json",e,void 0,o);return i(n,r,s),s}async function l(t,n,r,e,o){if(n.timeExtent?.isEmpty)return r.createFeatureResult();const u=await f(t,n,e,o);return s(u,r)}function f(t,n,r,e){return d(t,n,"pbf",r,void 0,e)}function m(t,n,r,e){return n.timeExtent?.isEmpty?Promise.resolve({objectIds:[]}):d(t,n,"json",r,{returnIdsOnly:!0},e)}async function p(t,n,r,e){return n.timeExtent?.isEmpty?0:(await d(t,n,"json",r,{returnIdsOnly:!0,returnCountOnly:!0},e)).count}async function O(t,n,r){if(n.timeExtent?.isEmpty)return{count:0,extent:null};const e=await d(t,n,"json",r,{returnExtentOnly:!0,returnCountOnly:!0});if(e.hasOwnProperty("extent"))return e;const o="string"==typeof t?t:t.path;if(/\/imageserver\/?$/i.test(o)&&e.hasOwnProperty("count")){const o=await d(t,n,"json",r,{returnExtentOnly:!0});return{count:e.count,extent:o.extent}}if(e.features)throw new Error(c);if(e.hasOwnProperty("count"))throw new Error(c);return e}function j(t,n){if(!t.returnIdsOnly||!n.uniqueIdFields)return t;const r={...t,returnUniqueIdsOnly:!0};return delete r.returnIdsOnly,r}async function d(n,r,e,o={},s={},u={}){const i=await w(n,r,e,o,s,u),c="json"===e?"json":"array-buffer";return t(i.url,{responseType:c,...i.options}).then(({data:t})=>t)}async function w(t,s,u,i,c={},y={}){const l="string"==typeof t?n(t):t,f=s.geometry?[s.geometry]:[],m=await e(f,null,{signal:i.signal}),p=m?.[0];if(null!=p){const t=s.clone();t.geometry=p.clone(),s=t}const O=o({...l.query,f:u,...j(c,y),...a(s,c,y)});return i={...i,query:{...O,...i.query}},new E(r(l.path,x(s,c)?"query3d":"query"),i)}class E{constructor(t,n){this.url=t,this.options=n}}function x(t,n){return null!=t.formatOf3DObjects&&!(n.returnCountOnly||n.returnExtentOnly||n.returnIdsOnly)}export{E as RunQueryParameters,y as executeQuery,p as executeQueryForCount,O as executeQueryForExtent,m as executeQueryForIds,l as executeQueryPBF,f as executeQueryPBFBuffer,w as getQueryParameters,a as queryToQueryStringParameters,d as runQuery};
