import e from"@arcgis/core/layers/support/Field.js";import t from"@arcgis/core/popup/FieldInfo.js";import o from"@arcgis/core/request";import{Feature as r}from"../../data/Feature.js";import{wfsFeatureCollectionToFeatures as s}from"../../mapping/support/ogc.js";import{geoJSONToEsri as i}from"../../utilities/_geoJson.js";import{take as a,toArray as n}from"../../utilities/asyncIterable.js";import{isWMSLayerExtension as l}from"../../utilities/extensions.js";import{IdentifyProviderBase as m}from"./IdentifyProviderBase.js";import{isIdentifiable as u}from"./IdentifyService.js";const f=["application/json","application/geojson","text/xml"],c=["OBJECTID","GLOBALID"];export class WMSLayerFeatureInfoIdentifyProvider extends m{isSupported(e,t,o){return l(e)&&e.layer?.featureInfoFormats?.some((e=>f.includes(e)))&&!e.wfsUrl&&"point"===t?.type}_identify(e,t,r,s){const i=this,n=e._layerView,{extent:l}=n.view,m="point"===t.type?t:t.extent.center,c=n.view.toScreen(m),p=e.layer.featureInfoFormats.find((e=>f.includes(e))),{xmin:y,ymin:d,xmax:g,ymax:F,spatialReference:x}=l,I=`${y},${d},${g},${F}`,h=`EPSG:${x.latestWkid??x.wkid}`,w=e.sublayerExtensions.filter(u).map((({sublayer:e})=>e.name)).filter(Boolean).join();return async function*(){const t=await o(e.layer.featureInfoUrl,{query:{SERVICE:"WMS",LAYERS:w,QUERY_LAYERS:w,REQUEST:"GetFeatureInfo",FORMAT:e.layer.imageFormat,INFO_FORMAT:p,FEATURE_COUNT:r.maxResults,BBOX:I,CRS:h,WIDTH:n.view.width.toString(),HEIGHT:n.view.height.toString(),I:Math.round(c.x).toString(),J:Math.round(c.y).toString(),version:e.layer.version},responseType:"text/xml"===p?"xml":"json"});let s;switch(p){case"application/json":case"application/geojson":s=i._jsonToFeatures(t,r,x,e);break;case"text/xml":s=i._xmlToFeatures(t,e,r)}r.maxResults?yield*a(s,r.maxResults):yield*s}()}async _count(e,t,o){return{source:e,count:(await n(this.identify(e,t,o))).length}}_normalizeFeature(o,r){const s=[];for(const e of o.attributes.keys())s.push(e);!r.returnGeometry&&o.geometry&&(o.geometry=void 0);const{featureSettings:i}=o.source,{popupTemplate:a}=i;if(a&&i._isDefaultPopupTemplate){if(!o.title){const e=c.find((e=>o.attributes.has(e)))??"ID";a.title=`{${e}}`,o.attributes.has(e)||o.attributes.set(e,o.id)}for(const e of s)a.fieldInfos.some((t=>t.fieldName===e))||a.fieldInfos.push(new t({fieldName:e}))}const{source:n}=o;for(const t of s)n.schema.findFieldByName(t)||n.schema.fields.add(new e({name:t,alias:t}));return o}async*_xmlToFeatures(e,t,o){const r=e.data.childNodes[0];if("FeatureCollection"===r.localName)for(const e of await s(r,t))yield this._normalizeFeature(e,o);else yield*this._membersToFeature(r,t,o)}async*_jsonToFeatures(e,t,o,s){const a=e.data,n=new Map;for(const e of a.features){let a;const l=e.properties;t.returnGeometry&&e.geometry&&(a=i(e.geometry),a&&(a.spatialReference=o));let m=n.get(e.layerName);m||(m=s.sublayerExtensions.find((t=>t.sublayer.name===e.layerName))??s,n.set(e.layerName,m)),yield this._normalizeFeature(new r({attributes:l,geometry:a,source:m,id:e.id}),t)}}*_membersToFeature(e,t,o){for(const s of e.getElementsByTagName("FIELDS")){const e=new Map;for(const t of s.getAttributeNames())e.set(t,s.getAttribute(t));yield this._normalizeFeature(new r({attributes:e,source:t}),o)}}}