/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{getJsonType as e}from"../../../geometry/support/jsonTypeUtils.js";import{srToRESTValue as t}from"../../../geometry/support/spatialReferenceUtils.js";function i(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 i=e.toJSON();return delete i.spatialReference,JSON.stringify(i)}function n(n,r,a){const{geometry:l}=n,s=n.compactGeometryEnabled??!1,o=n.toJSON();delete o.compactGeometryEnabled,delete o.defaultSpatialReferenceEnabled;const u=o;let d,c,y;if(l&&(c=l.spatialReference,y=t(c),u.geometryType=e(l),u.geometry=i(l,s),u.inSR=y),o.groupByFieldsForStatistics&&(u.groupByFieldsForStatistics=o.groupByFieldsForStatistics.join(",")),o.objectIds)switch(a?.uniqueIdFields?.length){case void 0:u.objectIds=o.objectIds.join(",");break;case 1:u.uniqueIds=JSON.stringify(o.objectIds),delete u.objectIds;break;default:u.uniqueIds=JSON.stringify(o.objectIds.map(e=>JSON.parse(e))),delete u.objectIds}if(o.orderByFields&&(u.orderByFields=o.orderByFields.join(",")),!o.outFields||!o.returnDistinctValues&&(r?.returnCountOnly||r?.returnExtentOnly||r?.returnIdsOnly)?delete u.outFields:o.outFields.includes("*")?u.outFields="*":u.outFields=o.outFields.join(","),o.outSR?(u.outSR=t(o.outSR),d=n.outSpatialReference):l&&(o.returnGeometry||o.returnCentroid)&&(u.outSR=u.inSR,d=c),o.returnGeometry&&delete o.returnGeometry,o.outStatistics&&(u.outStatistics=JSON.stringify(o.outStatistics)),o.fullText&&(u.fullText=JSON.stringify(o.fullText)),o.pixelSize&&(u.pixelSize=JSON.stringify(o.pixelSize)),o.quantizationParameters&&(n.defaultSpatialReferenceEnabled&&null!=c&&null!=n.quantizationParameters?.extent&&c.equals(n.quantizationParameters.extent.spatialReference)&&delete o.quantizationParameters.extent.spatialReference,u.quantizationParameters=JSON.stringify(o.quantizationParameters)),o.parameterValues&&(u.parameterValues=JSON.stringify(o.parameterValues)),o.rangeValues&&(u.rangeValues=JSON.stringify(o.rangeValues)),o.dynamicDataSource&&(u.layer=JSON.stringify({source:o.dynamicDataSource}),delete o.dynamicDataSource),o.timeExtent){const e=o.timeExtent,{start:t,end:i}=e;null==t&&null==i||(u.time=t===i?t:`${t??"null"},${i??"null"}`),delete o.timeExtent}return n.defaultSpatialReferenceEnabled&&null!=c&&null!=d&&c.equals(d)&&(u.defaultSR=u.inSR,delete u.inSR,delete u.outSR),u}const r="json";export{i as encodeGeometry,n as queryLikeToQueryStringParameters,r as safeExtentResultFormat};
