/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/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";function a(e){return e.effectiveSequenceOrderField?.[0]?.fieldName??null}function c(e){const r=a(e);return r?`${r} IS NOT NULL`:null}async function u(e,t,s,a,c){const{polygon:u}=o(t.attributes),f=r.fromJSON(n(u));f.z=void 0;const y=E(e.definitionExpression,s);let d,l;try{d=await i({layerInstanceOrURL:e,point:f,queryParams:{where:y,timeExtent:a}},c)}catch(m){l=m}return new w(f,d,l)}async function f(e,r,t,n,o){const s=e.effectiveSequenceOrderField?.map(({fieldName:e,sortOrder:r})=>`${e} ${"desc"===r?"DESC":"ASC"}`),a=E(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 w(r,c,u)}function y(e,r,t,n){const i=E(e.definitionExpression,r,c(e)),o=e.createQuery();return o.where=i,o.timeExtent=t,e.queryFeatureCount(o,n)}async function d(r,n,i,o="ASC",s,u,f){const y=a(r);let d=E(r.definitionExpression,s,c(r));const l="number"==typeof n?n-1:void 0;"string"===n&&y&&(d=e(d,`${y} = ${n}`));const m=r.createQuery();m.where=d,m.start=l,m.num=i,m.timeExtent=u,m.orderByFields=y?[`${y} ${o}`]:void 0,r.spatialReference.isGeographic&&(m.outSpatialReference=t.WebMercator);try{const e=await r.queryFeatures(m,f);return await h(e,r),new p(e)}catch(w){return new p(void 0,w)}}async function l(e,r,n,i=1,o,a,c){const u=e.effectiveSequenceOrderField?.map(({fieldName:e,sortOrder:r})=>`${e} ${"desc"===r?"DESC":"ASC"}`),f=E(e.definitionExpression,o,s(r,n,e.fields)),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 h(r,e),new p(r)}catch(d){return new p(void 0,d)}}async function m(e,r,n,i,o){const s=Array.isArray(r)?r:[r],a=E(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 h(r,e),new p(r)}catch(u){return new p(void 0,u)}}class p{constructor(e,r){this.response=e,this.error=r}}class w{constructor(e,r,t){this.nextLocation=e,this.response=r,this.error=t}}async function h(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 E(...r){let t="1=1";return r.forEach(r=>{t=e(t,r)}),t}export{w as QueryNextLocationFeaturesResponse,p as QueryOrientedImageryFeaturesResponse,E as mergeSQLstatements,l as queryAfterSequenceField,m as queryFeatureByObjectIds,u as queryNextLocationFeatures,d as queryOffsetSequenceFeatures,y as querySequenceCount,f as querySequenceFeaturesAroundPoint};
