/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{isSome as e}from"../../../core/arrayUtils.js";import t from"../../../geometry/SpatialReference.js";import{isWKT2 as n}from"../../../geometry/support/spatialReferenceUtils.js";import{isSameTagIgnoreNS as r,getElementValue as s,getElements as a,getElement as i,getElementNumericValue as l,getElementNumericValues as o}from"./xmlUtilities.js";import f from"../transforms/PolynomialTransform.js";function u(e,t){if(!e||!t)return null;const n=[];for(let r=0;r<e.length;r++)n.push(e[r]),n.push(t[r]);return n}function c(e){const t=i(e,"GeodataXform"),n=d(l(t,"SpatialReference/WKID")||s(t,"SpatialReference/WKT"));if("typens:PolynomialXform"!==t.getAttribute("xsi:type"))return{spatialReference:n,transform:null};const r=l(t,"PolynomialOrder")??1,a=o(t,"CoeffX/Double"),c=o(t,"CoeffY/Double"),m=o(t,"InverseCoeffX/Double"),p=o(t,"InverseCoeffY/Double"),S=u(a,c),C=u(m,p);return{spatialReference:n,transform:S&&C&&S.length&&C.length?new f({spatialReference:n,polynomialOrder:r,forwardCoefficients:S,inverseCoefficients:C}):null}}function m(e){const t=l(e,"NoDataValue"),n=i(e,"Histograms/HistItem"),r=l(n,"HistMin"),o=l(n,"HistMax"),f=l(n,"BucketCount"),u=s(n,"HistCounts")?.split("|").map(e=>Number(e));let c,m,d,p;a(e,"Metadata/MDI").forEach(e=>{const t=Number(e.textContent??e.nodeValue);switch(e.getAttribute("key").toUpperCase()){case"STATISTICS_MINIMUM":c=t;break;case"STATISTICS_MAXIMUM":m=t;break;case"STATISTICS_MEAN":d=t;break;case"STATISTICS_STDDEV":p=t}});const S=l(e,"Metadata/SourceBandIndex");return{noDataValue:t,histogram:u?.length&&null!=r&&null!=o?{min:r,max:o,size:f||u.length,counts:u}:null,sourceBandIndex:S,statistics:null!=c&&null!=m?{min:c,max:m,avg:d,stddev:p}:null}}function d(e){if(!e)return null;let r=Number(e);if(!isNaN(r)&&0!==r)return new t({wkid:r});if(e=String(e).trim(),n(e))return new t({wkt2:e});const s=e.toUpperCase();if(s.startsWith("COMPD_CS")){if(!s.includes("VERTCS")||!s.includes("GEOGCS")&&!s.startsWith("PROJCS"))return null;const n=s.indexOf("VERTCS"),a=s.indexOf("PROJCS"),i=a>-1?a:s.indexOf("GEOGCS");if(-1===i)return null;const l=e.slice(i,e.lastIndexOf("]",n)+1).trim(),o=e.slice(n,e.lastIndexOf("]")).trim();r=p(l);const f=new t(r?{wkid:r}:{wkt:l}),u=p(o);return u&&(f.vcsWkid=u),f}return s.startsWith("GEOGCS")||s.startsWith("PROJCS")?(r=p(e),new t(0!==r?{wkid:r}:{wkt:e})):null}function p(e){const t=e.replaceAll("]","[").replaceAll('"',"").split("[").map(e=>e.trim()).filter(e=>""!==e),n=t[t.length-1].split(","),r=n[0]?.toLowerCase();if(("epsg"===r||"esri"===r)&&e.endsWith('"]]')){const e=Number(n[1]);if(!isNaN(e)&&0!==e)return e}return 0}function S(t){if("pamdataset"!==t?.documentElement.tagName?.toLowerCase())return{};const n={spatialReference:null,transform:null,metadata:{},rasterBands:[],statistics:null,histograms:null};t.documentElement.childNodes.forEach(e=>{if(1===e.nodeType)if(r(e,"SRS")){if(!n.spatialReference){const t=s(e);n.spatialReference=d(t)}}else if(r(e,"Metadata"))if("xml:ESRI"===e.getAttribute("domain")){const{spatialReference:t,transform:r}=c(e);n.transform=r,n.spatialReference||(n.spatialReference=t)}else{a(e,"MDI").forEach(e=>n.metadata[e.getAttribute("key")]=s(e))}else if(r(e,"PAMRasterBand")){const t=m(e);null!=t.sourceBandIndex&&null==n.rasterBands[t.sourceBandIndex]?n.rasterBands[t.sourceBandIndex]=t:n.rasterBands.push(t)}});const i=n.rasterBands;if(i.length){const t=!!i[0].statistics;n.statistics=t?i.map(e=>e.statistics).filter(e):null;const r=!!i[0].histogram;n.histograms=r?i.map(e=>e.histogram).filter(e):null}return n}export{S as parsePAMInfo,d as parseSpatialReference};
