/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../../../core/Error.js";import t from"../../../geometry/Extent.js";import{getElement as r,getElementValue as o,getElementValues as i,getElements as n,getSpaceDelimitedNumericValues as a,isSameTagIgnoreNS as s,getNodeNameIgnoreNS as c}from"./xmlUtilities.js";function l(e){return e.endsWith("?")?e.slice(0,-1):e}function u(e){return e.filter(({coverageSubType:e})=>null==e||""===e||/^rectified(grid|dataset)/i.test(e))}function p(e){const i=o(e,"Service/name"),s=r(e,"Capability"),c=r(s,"GetCapabilities/Get/OnlineResource")?.getAttribute("xlink:href")??"",p=r(s,"DescribeCoverage/Get/OnlineResource")?.getAttribute("xlink:href")??"",m=r(s,"GetCoverage/Get/OnlineResource")?.getAttribute("xlink:href")??"",g={getCapabilities:l(c),describeCoverage:l(p),getCoverage:l(m)},v=n(e,"CoverageOfferingBrief"),d=[];for(let r=0;r<v.length;r++){const e=v[r],i=o(e,"name"),s=n(e,"pos"),c=a(s[0]),l=a(s[1]),u=new t({xmin:c[0],ymin:c[1],xmax:l[0],ymax:l[1],spatialReference:{wkid:4326}});d.push({id:i,lonLatEnvelope:u})}return{name:i,onlineResources:g,coverages:d,gridCoverages:u(d),supportedVersions:["1.0.0"],version:"1.0.0"}}function m(e){const r={};for(let i=0;i<e.childNodes.length;i++){const n=e.childNodes[i];if(1!==n.nodeType)continue;const s=c(n).toLowerCase();switch(s){case"title":case"abstract":r[s]=o(n);break;case"identifier":r.id=o(n);break;case"wgs84boundingbox":{const e=a(n,"LowerCorner"),o=a(n,"UpperCorner");r.lonLatEnvelope=new t({xmin:e[0],ymin:e[1],xmax:o[0],ymax:o[1],spatialReference:{wkid:4326}})}break;case"coveragesummary":r.coverageSummaries=r.coverageSummaries||[],r.coverageSummaries.push(m(n))}}return r}function g(e,t){if(e.coverageSummaries)for(let r=0;r<e.coverageSummaries.length;r++)e.coverageSummaries[r].abstract=e.coverageSummaries[r].abstract||e.abstract,e.coverageSummaries[r].lonLatEnvelope=e.coverageSummaries[r].lonLatEnvelope||e.lonLatEnvelope,e.coverageSummaries[r].title=e.coverageSummaries[r].title||e.title,g(e.coverageSummaries[r],t);null!=e.id&&t.push(e)}function v(e){const t=r(e.querySelector("Operation[name=GetCapabilities]"),"Get")?.getAttribute("xlink:href")||"",o=r(e.querySelector("Operation[name=DescribeCoverage]"),"Get")?.getAttribute("xlink:href")||"",i=r(e.querySelector("Operation[name=GetCoverage]"),"Get")?.getAttribute("xlink:href")||"";return{getCapabilities:l(t),describeCoverage:l(o),getCoverage:l(i)}}function d(e){const t=o(e,"ServiceIdentification/Title"),n=i(e,"ServiceIdentification/ServiceTypeVersion"),a=v(r(e,"OperationsMetadata")),c=[],l=r(e,"Contents");for(let r=0;r<l.childNodes.length;r++){const e=l.childNodes[r];1===e.nodeType&&(s(e,"CoverageSummary")&&g(m(e),c))}const p=i(l,"SupportedFormat");return{name:t,onlineResources:a,coverages:c,gridCoverages:u(c),supportedVersions:n,supportedFormats:p,version:"1.1.0"}}function f(e){const s=r(e,"ServiceIdentification"),c=o(s,"Title"),l=i(s,"ServiceTypeVersion"),p=i(s,"Profile"),m=v(r(e,"OperationsMetadata")),g=n(e,"Contents/CoverageSummary"),d=[];for(let i=0;i<g.length;i++){const e=g[i],n=o(e,"CoverageId"),s=r(e,"WGS84BoundingBox");let c;if(s){const e=a(s,"LowerCorner"),r=a(s,"UpperCorner");c=new t({xmin:e[0],ymin:e[1],xmax:r[0],ymax:r[1],spatialReference:{wkid:4326}})}const l=o(e,"CoverageSubtype")||"RectifiedGridCoverage";d.push({id:n,lonLatEnvelope:c,coverageSubType:l})}const f=r(e,"ServiceMetadata");return{name:c,supportedVersions:l,supportedFormats:i(f,"formatSupported"),supportedInterpolations:i(f,"interpolationSupported").concat(i(f,"InterpolationSupported")),onlineResources:m,profiles:p,coverages:d,gridCoverages:u(d),version:"2.0.1"}}function S(e){let t=null;if("string"==typeof e){t=(new DOMParser).parseFromString(e,"text/xml")}else t=e;const r=t.documentElement.getAttribute("version"),o=r?.slice(0,3);return null!=o&&o<"2.1"}function b(t,r=null){let o=null;if("string"==typeof t){o=(new DOMParser).parseFromString(t,"text/xml")}else o=t;let i=o.documentElement.getAttribute("version");"1.0"===i?i="1.0.0":"1.1"===i&&(i="1.1.0");const n=i||r||"1.0.0",a=n.slice(0,3);let s;if("2.0"===a)s=f(o);else if("1.1"===a)s=d(o);else{if("1.0"!==a)throw new e("wcsraster:parsecapabilities","the capabilities version is not supported");s=p(o)}return s.version=n,s}export{S as isSupportedVersion,b as parseCapabilities};
