import e from"@arcgis/core/layers/support/Field.js";import t from"@arcgis/core/popup/FieldInfo";import{Feature as o}from"../../data/Feature.js";import{FieldExtension as i}from"../../data/FieldExtension.js";import{isGmlNamespace as n,OgcNamespace as s}from"../../utilities/_ogc.js";import{checkArg as r}from"../../utilities/checkArg.js";import{gmlToEsri as l}from"../../utilities/geometry.js";export async function wfsFeatureCollectionToFeatures(m,c){r("featureCollection",m).isNotMissing(),r("featureCollection.localName",m.localName).satisfies((e=>"FeatureCollection"===e),"Unable to parse WFS FeatureCollection: unexpected element encountered");let f=[];return f=m.namespaceURI===s.WFS_2_0?Array.from(m.getElementsByTagNameNS(m.namespaceURI,"member")):Array.from(m.getElementsByTagNameNS(s.GML,"featureMember")),Promise.all(f.map((s=>async function(s,m){let c,f;r("member",s).isNotMissing();const d=new Map;if(s.childElementCount>0){const e=s.firstElementChild;if(c=function(e){r("element",e).isNotMissing();const t=Array.from(e.attributes);for(const e of t){if(n(e.namespaceURI)&&e.nodeType===Node.ATTRIBUTE_NODE)return e.nodeValue??void 0;if(!(void 0!==e.namespaceURI&&null!==e.namespaceURI||"fid"!==e.localName&&"gid"!==e.localName||e.nodeType!==Node.ATTRIBUTE_NODE))return e.nodeValue??void 0}return}(e),e.childElementCount>0){const t=Array.from(e.childNodes).filter((e=>e.nodeType===e.ELEMENT_NODE));for(const e of t)e.childElementCount>0&&n(e.firstElementChild.namespaceURI)?f=await l(e.firstElementChild):d.set(e.localName,a(e))}}const u=new o({id:c,attributes:d,geometry:f,source:m}),p=[];for(const e of u.presentableAttributes.keys())p.push(e);const{popupTemplate:N}=u.settings;N&&(u.title||(N.title="{ID}",u.attributes.set("ID",u.id)),N.fieldInfos&&0!==N.fieldInfos.length||(N.fieldInfos=p.map((e=>new t({fieldName:e})))));if(0===m?.schema.fieldExtensions.initializedItems.length)for(const t of p){const o=new i({field:new e({name:t,alias:t})});m.schema.fieldExtensions.add(o)}return u}(s,c))))}function a(e){if(r("element",e).isNotMissing(),1===e.childNodes.length&&e.childNodes[0].nodeType===Node.TEXT_NODE)return e.childNodes[0].data}