import e from"@arcgis/core/geometry/Point";import t from"@arcgis/core/geometry/Polygon";import{identify as r}from"@arcgis/core/rest/imageService";import a from"@arcgis/core/rest/support/ImageIdentifyParameters";import i from"@arcgis/core/rest/support/ProjectParameters";import{Feature as s,toFeature as o}from"../../data/Feature.js";import{cancelifyWithAbortSignal as n}from"../../support/Cancellable.js";import{constrainQueryTimeExtentToView as c}from"../../support/_query.js";import{toArray as l}from"../../utilities/asyncIterable.js";import{isPoint as u,isPolygon as m,isPolyline as f}from"../../utilities/esri.js";import{isImageryLayerExtension as p}from"../../utilities/extensions.js";import{getLogger as y}from"../../utilities/log.js";import{isNumeric as g}from"../../utilities/number.js";import{IdentifyProviderBase as d}from"./IdentifyProviderBase.js";const I=y("ImageryLayerIdentifyProvider"),h="Raster.";export class ImageryLayerIdentifyProvider extends d{isSupported(e,t,r){return p(e)}async*_identify(r,a,i){const s=r;let o=a;if(a&&!u(a)&&!m(a))if(f(a)){I.debug("An identify on an Imagery Layer (ArcGISImageServiceLayer) was performed using polyline geometry, which is not supported. The center vertex of in the input geometry was used instead.");const t=a.paths[0][Math.floor(a.paths[0].length/2)];o=new e({spatialReference:a.spatialReference,x:t[0],y:t[1]})}else o=t.fromExtent(a.extent);const n=await this._executeIdentifyTask(s,o,i);yield*this._getResultFeatures(n,s,i)}async _count(e,t,r){const a=e;r.returnGeometry=!1;const i=await this._executeIdentifyTask(a,t,r);return{source:e,count:(await l(this._getResultFeatures(i,a,r))).length}}async _executeIdentifyTask(e,t,i){const{layer:s}=e,o=new a({geometry:t,mosaicRule:s.mosaicRule,rasterFunction:s.rasterFunction,returnCatalogItems:e.includeCatalogItems,returnGeometry:e.includeCatalogItems&&i.returnGeometry});c(o,e);return n((async e=>r(s.url,o,{signal:e})),i.cancelToken)}_getPixelData(e,t,r,a){const{layer:i}=a;let s=r.properties?.Values[e]??r.value,o=r.value;g(s)&&(s=parseFloat(s)),g(o)&&(o=parseFloat(o)),t.attributes.set(`${h}ServicePixelValue`,o),t.attributes.set(`${h}ServicePixelValue.Raw`,o),t.attributes.set(`${h}ItemPixelValue`,s);const n=i.serviceRasterInfo?.attributeTable??i.rasterInfo?.attributeTable;if(n){const{features:e,fields:r}=n,a=e.find((e=>e.getAttribute("Value")===s));r.forEach((e=>{"Value"!==e.name&&"oid"!==e.type&&t.attributes.set(`${h}${e.name}`,a?.getAttribute(e.name))}))}}async*_getResultFeatures(e,t,r){if("NoData"===e.value)return;const a=r.outSpatialReference&&r.outSpatialReference.wkid!==t.layer.spatialReference.wkid;if(t.includeCatalogItems&&e.catalogItems){let i=Promise.resolve([]);a&&r.returnGeometry&&(i=this._projectGeometries(t.layer.url,e.catalogItems.features.map((e=>e.geometry)),r.outSpatialReference,r.cancelToken));const s=[],n=r.outFields||t.featureSettings.outFields;e.catalogItems.features.forEach(((r,a)=>{const i=o(r,{source:t});if(n&&!n.includes("*"))for(const e of i.attributes.keys())n.includes(e)||i.attributes.delete(e);i.attributes.set("visible",1===e.catalogItemVisibilities[a]),this._getPixelData(a,i,e,t),s.push(i)}));const c=await i;c.length>0&&c.forEach(((e,t)=>s[t].geometry=e)),yield*s}if(!t.includeCatalogItems||!e.catalogItems){const i=new s({source:t});i.geometry=a?(await this._projectGeometries(t.layer.url,[e.location],r.outSpatialReference,r.cancelToken))[0]:e.location,this._getPixelData(0,i,e,t),yield i}}async _projectGeometries(e,t,r,a){const s=new i({geometries:t,outSpatialReference:r}),{project:o}=await import("@arcgis/core/rest/geometryService");return n((async t=>o(e,s,{signal:t})),a)}}