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