/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import e from"../../core/Error.js";import{throwIfAborted as r}from"../../core/promiseUtils.js";import t from"../../geometry/SpatialReference.js";import{isPoint as o}from"../../geometry/support/typeUtils.js";import n from"../OrientedImageryLayer.js";import a from"./core/ExposurePoint.js";import{convertGeographicToWebMercator as i}from"./core/utils.js";import s from"../../rest/support/Query.js";import{createExtentAroundPoint as u}from"../../widgets/OrientedImageryViewer/navigation/utils.js";const m={},c=1e3;async function l(e,t){const{point:o,queryParams:n}=e;p(o,n);const a=w(e.layerInstanceOrURL);await a.load(t);const i=y(a,o,n),s=await a.queryFeatures(i,t);return r(t),d(a)(s)}function p(r,t){if(!o(r)&&null==t?.geometry)throw new e("invalid-parameters","searchImages requires a point and query parameters to be provided.");return!0}function y(e,r,o){const n=r?i(r):null,a=o?.maximumDistance??e.maximumDistance??c,m=o?.geometry??(n?u(n,a):null),l=o?.timeExtent??e.timeExtent??null;return new s({outSpatialReference:o?.outSpatialReference??e.spatialReference.isGeographic?t.WebMercator:e.spatialReference,returnGeometry:o?.returnGeometry??!0,outFields:o?.outFields??["*"],geometry:m,where:o?.where??"1=1",returnZ:e.hasZ,orderByFields:o?.orderByFields,maxRecordCountFactor:o?.maxRecordCountFactor??5,timeExtent:l})}async function f(e,r){const t=new s;t.where="1=1",t.outFields=["*"],t.orderByFields=["OffsetFromStart ASC"],t.outSpatialReference=r,t.returnGeometry=!0;const o=await e.queryFeatures(t);return d(e)(o)}function d(e){return r=>(r.features.forEach(r=>{const t=a.fromJSON({...r.toJSON(),layer:e});t&&(r.attributes=t)}),r)}function w(e){return"string"!=typeof e?e:(m.layer?.url!==e&&(m.layer?.destroy(),m.layer=new n({url:e})),m.layer)}export{f as getSortedLayerFeatures,l as searchImages};
