/* COPYRIGHT Esri - https://js.arcgis.com/5.2/LICENSE.txt */
import{sqlAnd as e}from"../../../core/sql.js";import r from"../../../geometry/Point.js";import t from"../../../geometry/SpatialReference.js";import{polygonCentroidPoint as n}from"../../../geometry/support/centroid.js";import{searchImages as i}from"../../../layers/orientedImagery/queries.js";import{createCoveragePolygon as o}from"../../../layers/orientedImagery/core/coverageUtils.js";import{constructWhereClauseForSequenceFields as s}from"./utils.js";async function a(e,t,s,a,c){const{polygon:u}=o(t.attributes),f=r.fromJSON(n(u));f.z=void 0;const y=w(e.definitionExpression,s);let d,m;try{d=await i({layerInstanceOrURL:e,point:f,queryParams:{where:y,timeExtent:a}},c)}catch(l){m=l}return new p(f,d,m)}async function c(e,r,t,n,o){const s=e.effectiveSequenceOrderField?.map(({fieldName:e,sortOrder:r})=>`${e} ${"desc"===r?"DESC":"ASC"}`),a=w(e.definitionExpression,t);let c,u;try{c=await i({layerInstanceOrURL:e,point:r,queryParams:{where:a,orderByFields:s,returnGeometry:!0,outFields:["*"],timeExtent:n}},o)}catch(f){u=f}return new p(r,c,u)}function u(e,r,t,n){const i=e.effectiveSequenceOrderField,o=w(e.definitionExpression,r,`${i} IS NOT NULL`),s=e.createQuery();return s.where=o,s.timeExtent=t,e.queryFeatureCount(s,n)}async function f(r,n,i,o="ASC",s,a,c){const u=r.effectiveSequenceOrderField;let f=w(r.definitionExpression,s,`${u} IS NOT NULL`);const y="number"==typeof n?n-1:void 0;"string"===n&&(f=e(f,`${u} = ${n}`));const d=r.createQuery();d.where=f,d.start=y,d.num=i,d.timeExtent=a,d.orderByFields=[`${u} ${o}`],r.spatialReference.isGeographic&&(d.outSpatialReference=t.WebMercator);try{const e=await r.queryFeatures(d,c);return await l(e,r),new m(e)}catch(p){return new m(void 0,p)}}async function y(e,r,n,i=1,o,a,c){const u=e.effectiveSequenceOrderField?.map(({fieldName:e,sortOrder:r})=>`${e} ${"desc"===r?"DESC":"ASC"}`),f=w(e.definitionExpression,o,s(r,n)),y=e.createQuery();y.where=f,y.num=i,y.orderByFields=u,y.timeExtent=a,e.spatialReference.isGeographic&&(y.outSpatialReference=t.WebMercator);try{const r=await e.queryFeatures(y,c);return await l(r,e),new m(r)}catch(d){return new m(void 0,d)}}async function d(e,r,n,i,o){const s=Array.isArray(r)?r:[r],a=w(e.definitionExpression,n,`${e.objectIdField} IN (${s.join(",")})`),c=e.createQuery();c.where=a,e.spatialReference.isGeographic&&(c.outSpatialReference=t.WebMercator),i&&(c.timeExtent=i);try{const r=await e.queryFeatures(c,o);return await l(r,e),new m(r)}catch(u){return new m(void 0,u)}}class m{constructor(e,r){this.response=e,this.error=r}}class p{constructor(e,r,t){this.nextLocation=e,this.response=r,this.error=t}}async function l(e,r){const t=await import("../../../layers/orientedImagery/core/ExposurePoint.js").then(e=>e.default);e.features.forEach(e=>{const n=t.fromJSON({...e.toJSON(),layer:r});n&&(e.attributes=n)})}function w(...r){let t="1=1";return r.forEach(r=>{t=e(t,r)}),t}export{p as QueryNextLocationFeaturesResponse,m as QueryOrientedImageryFeaturesResponse,w as mergeSQLstatements,y as queryAfterSequenceField,d as queryFeatureByObjectIds,a as queryNextLocationFeatures,f as queryOffsetSequenceFeatures,u as querySequenceCount,c as querySequenceFeaturesAroundPoint};
