/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../chunks/_rollupPluginBabelHelpers","../../../request","../../../core/maybe","../../../core/urlUtils","../../../geometry/support/jsonUtils","../../../geometry/support/normalizeUtils","../../operations/urlUtils","./pbfQueryUtils","./queryZScale"],(function(e,t,r,n,i,o,a,u,s,l){"use strict";const y="Layer does not support extent calculation.";function c(e,t){if(t&&"extent"===e.type)return`${e.xmin},${e.ymin},${e.xmax},${e.ymax}`;if(t&&"point"===e.type)return`${e.x},${e.y}`;const r=e.toJSON();return delete r.spatialReference,JSON.stringify(r)}function m(e,t){const r=e.geometry,i=e.toJSON();delete i.compactGeometryEnabled,delete i.defaultSpatialReferenceEnabled;const a=i;let u,s,l;if(n.isSome(r)&&(s=r.spatialReference,l=r.spatialReference.wkid||JSON.stringify(r.spatialReference),a.geometryType=o.getJsonType(r),a.geometry=c(r,e.compactGeometryEnabled),a.inSR=l),i.groupByFieldsForStatistics&&(a.groupByFieldsForStatistics=i.groupByFieldsForStatistics.join(",")),i.objectIds&&(a.objectIds=i.objectIds.join(",")),i.orderByFields&&(a.orderByFields=i.orderByFields.join(",")),!i.outFields||!i.returnDistinctValues&&(null!=t&&t.returnCountOnly||null!=t&&t.returnExtentOnly||null!=t&&t.returnIdsOnly)?delete a.outFields:i.outFields.includes("*")?a.outFields="*":a.outFields=i.outFields.join(","),i.outSR?(a.outSR=i.outSR.wkid||JSON.stringify(i.outSR),u=e.outSpatialReference):r&&(i.returnGeometry||i.returnCentroid)&&(a.outSR=a.inSR,u=s),i.returnGeometry&&delete i.returnGeometry,i.outStatistics&&(a.outStatistics=JSON.stringify(i.outStatistics)),i.pixelSize&&(a.pixelSize=JSON.stringify(i.pixelSize)),i.quantizationParameters&&(e.defaultSpatialReferenceEnabled&&n.isSome(s)&&n.isSome(e.quantizationParameters)&&n.isSome(e.quantizationParameters.extent)&&s.equals(e.quantizationParameters.extent.spatialReference)&&delete i.quantizationParameters.extent.spatialReference,a.quantizationParameters=JSON.stringify(i.quantizationParameters)),i.parameterValues&&(a.parameterValues=JSON.stringify(i.parameterValues)),i.rangeValues&&(a.rangeValues=JSON.stringify(i.rangeValues)),i.dynamicDataSource&&(a.layer=JSON.stringify({source:i.dynamicDataSource}),delete i.dynamicDataSource),i.timeExtent){const e=i.timeExtent,{start:t,end:r}=e;null==t&&null==r||(a.time=t===r?t:`${null==t?"null":t},${null==r?"null":r}`),delete i.timeExtent}return e.defaultSpatialReferenceEnabled&&n.isSome(s)&&n.isSome(u)&&s.equals(u)&&(a.defaultSR=a.inSR,delete a.inSR,delete a.outSR),a}function d(e,t,r,n){return p.apply(this,arguments)}function p(){return(p=t._asyncToGenerator((function*(e,t,r,i){const o=n.isSome(t.timeExtent)&&t.timeExtent.isEmpty?{data:{features:[]}}:yield O(e,t,"json",i);return l.applyFeatureSetZUnitScaling(t,r,o.data),o}))).apply(this,arguments)}function f(e,t,r,n){return S.apply(this,arguments)}function S(){return(S=t._asyncToGenerator((function*(e,t,r,i){if(n.isSome(t.timeExtent)&&t.timeExtent.isEmpty)return{data:r.createFeatureResult()};const o=yield x(e,t,i),a=o;return a.data=s.parsePBFFeatureQuery(o.data,r),a}))).apply(this,arguments)}function x(e,t,r){return O(e,t,"pbf",r)}function g(e,t,r){return n.isSome(t.timeExtent)&&t.timeExtent.isEmpty?Promise.resolve({data:{objectIds:[]}}):O(e,t,"json",r,{returnIdsOnly:!0})}function E(e,t,r){return n.isSome(t.timeExtent)&&t.timeExtent.isEmpty?Promise.resolve({data:{count:0}}):O(e,t,"json",r,{returnIdsOnly:!0,returnCountOnly:!0})}function F(e,t,r){return n.isSome(t.timeExtent)&&t.timeExtent.isEmpty?Promise.resolve({data:{count:0,extent:null}}):O(e,t,"json",r,{returnExtentOnly:!0,returnCountOnly:!0}).then((e=>{const t=e.data;if(t.hasOwnProperty("extent"))return e;if(t.features)throw new Error(y);if(t.hasOwnProperty("count"))throw new Error(y);return e}))}function O(e,t,o,s={},l={}){const y="string"==typeof e?i.urlToObject(e):e,c=t.geometry?[t.geometry]:[];return s.responseType="pbf"===o?"array-buffer":"json",a.normalizeCentralMeridian(c,null,s).then((e=>{const a=e&&e[0];n.isSome(a)&&((t=t.clone()).geometry=a);const c=u.mapParameters({...y.query,f:o,...l,...m(t,l)});return r(i.join(y.path,"query"),{...s,query:{...c,...s.query}})}))}e.encodeGeometry=c,e.executeQuery=d,e.executeQueryForCount=E,e.executeQueryForExtent=F,e.executeQueryForIds=g,e.executeQueryPBF=f,e.executeQueryPBFBuffer=x,e.queryToQueryStringParameters=m,e.runQuery=O,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
