import e from"@arcgis/core/layers/support/Field.js";import t from"@arcgis/core/popup/FieldInfo";import{identify as r}from"@arcgis/core/rest/identify";import i from"@arcgis/core/rest/support/IdentifyParameters";import{toFeature as s}from"../../data/Feature.js";import{isMapImageSublayer as a}from"../../utilities/_layers.js";import{toArray as o}from"../../utilities/asyncIterable.js";import{IdentifyProviderBase as n}from"./IdentifyProviderBase.js";export class ArcGISSublayerIdentifyProvider extends n{isSupported(e,t,r){return a(e.sublayer)&&!e.capabilities.supportsQuery}_identify(e,t,s,a){const o=this,{sublayer:n}=e,l=e.layerExtension.mapExtension._allLayerViews.find((t=>t.layer===e.layerExtension.layer));return async function*(){if(n.layer?.url&&l){const s=new i({geometry:t,tolerance:3,width:l.view.width,height:l.view.height,mapExtent:l.view.extent,returnGeometry:!0,layerIds:[e.sublayer.id]}),{results:a}=await r(n.layer.url,s);yield*a.map((t=>o._constructFeature(t.feature,e)))}}()}async _count(e,t,r){return{source:e,count:(await o(this.identify(e,t,r))).length}}_constructFeature(r,i){const a=s(r,{source:i}),o=[];for(const e of a.attributes.keys())o.push(e);const{featureSettings:n}=a.source,{popupTemplate:l}=n;if(l&&n._isDefaultPopupTemplate){if(!a.title){const e=["Raster.OBJECTID","Raster.GLOBALID","OBJECTID","GLOBALID"].find((e=>a.attributes.has(e)))??"ID";l.title=`{${e}}`,a.attributes.has(e)||a.attributes.set(e,a.source.title)}for(const e of o)l?.fieldInfos?.some((t=>t.fieldName===e))||l?.fieldInfos?.push(new t({fieldName:e}))}for(const t of o)i.schema.findFieldByName(t)||i.schema._fieldsBackingCollection.add(new e({name:t,alias:t}));return a}}