/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../geometry","./xmlUtilities","../rasterTransforms/PolynomialTransform","../../../geometry/SpatialReference"],(function(e,t,n,a,r){"use strict";function l(e,t){if(!e||!t)return null;const n=[];for(let a=0;a<e.length;a++)n.push(e[a]),n.push(t[a]);return n}function s(e){var t;const r=n.getElement(e,"GeodataXform"),s=u(n.getElementNumericValue(r,"SpatialReference/WKID")||n.getElementValue(r,"SpatialReference/WKT"));if("typens:PolynomialXform"!==r.getAttribute("xsi:type"))return{spatialReference:s,transform:null};const i=null!=(t=n.getElementNumericValue(r,"PolynomialOrder"))?t:1,o=n.getElementNumericValues(r,"CoeffX/Double"),c=n.getElementNumericValues(r,"CoeffY/Double"),m=n.getElementNumericValues(r,"InverseCoeffX/Double"),f=n.getElementNumericValues(r,"InverseCoeffY/Double"),d=l(o,c),g=l(m,f);return{spatialReference:s,transform:new a({spatialReference:s,polynomialOrder:i,forwardCoefficients:d,inverseCoefficients:g})}}function i(e){var t;const a=n.getElementNumericValue(e,"NoDataValue"),r=n.getElement(e,"Histograms/HistItem"),l=n.getElementNumericValue(r,"HistMin"),s=n.getElementNumericValue(r,"HistMax"),i=n.getElementNumericValue(r,"BucketCount"),u=null==(t=n.getElementValue(r,"HistCounts"))?void 0:t.split("|").map((e=>Number(e)));let o,c,m,f;n.getElements(e,"Metadata/MDI").forEach((e=>{var t;const n=Number(null!=(t=e.textContent)?t:e.nodeValue);switch(e.getAttribute("key").toUpperCase()){case"STATISTICS_MINIMUM":o=n;break;case"STATISTICS_MAXIMUM":c=n;break;case"STATISTICS_MEAN":m=n;break;case"STATISTICS_STDDEV":f=n}}));const d=n.getElementNumericValue(e,"Metadata/SourceBandIndex");return{noDataValue:a,histogram:null!=u&&u.length&&null!=o&&null!=c?{min:l,max:s,size:i||u.length,counts:u}:null,sourceBandIndex:d,statistics:null!=o&&null!=c?{min:o,max:c,avg:m,stddev:f}:null}}function u(e){if(!e)return null;let t=Number(e);if(!isNaN(t)&&0!==t)return new r({wkid:t});if((e=String(e)).startsWith("COMPD_CS")){if(!e.includes("VERTCS")||!e.includes("GEOGCS")&&!e.startsWith("PROJCS"))return null;const n=e.indexOf("VERTCS"),a=e.indexOf("PROJCS"),l=a>-1?a:e.indexOf("GEOGCS");if(-1===l)return null;const s=e.slice(l,e.lastIndexOf("]",n)+1).trim(),i=e.slice(n,e.lastIndexOf("]")).trim();t=o(s);const u=new r(t?{wkid:t}:{wkt:s}),c=o(i);return c&&(u.vcsWkid=c),u}return e.startsWith("GEOGCS")||e.startsWith("PROJCS")?(t=o(e),new r(0!==t?{wkid:t}:{wkt:e})):null}function o(e){var t;const n=e.replace(/\]/g,"[").replace(/\"/g,"").split("[").map((e=>e.trim())).filter((e=>""!==e)),a=n[n.length-1].split(","),r=null==(t=a[0])?void 0:t.toLowerCase();if(("epsg"===r||"esri"===r)&&e.endsWith('"]]')){const e=Number(a[1]);if(!isNaN(e)&&0!==e)return e}return 0}function c(e){var t;if("pamdataset"!==(null==e||null==(t=e.documentElement.tagName)?void 0:t.toLowerCase()))return{};const a={spatialReference:null,transform:null,metadata:{},rasterBands:[],statistics:null,histograms:null};e.documentElement.childNodes.forEach((e=>{if(1===e.nodeType)if(n.isSameTagIgnoreNS(e,"SRS")){if(!a.spatialReference){const t=n.getElementValue(e);a.spatialReference=u(t)}}else if(n.isSameTagIgnoreNS(e,"Metadata"))if("xml:ESRI"===e.getAttribute("domain")){const{spatialReference:t,transform:n}=s(e);a.transform=n,a.spatialReference||(a.spatialReference=t)}else{n.getElements(e,"MDI").forEach((e=>a.metadata[e.getAttribute("key")]=n.getElementValue(e)))}else if(n.isSameTagIgnoreNS(e,"PAMRasterBand")){const t=i(e);null!=t.sourceBandIndex&&null==a.rasterBands[t.sourceBandIndex]?a.rasterBands[t.sourceBandIndex]=t:a.rasterBands.push(t)}}));const r=a.rasterBands;if(r){const e=!!r[0].statistics;a.statistics=e?r.map((e=>e.statistics)):null;const t=!!r[0].histogram;a.histograms=t?r.map((e=>e.histogram)):null}return a}e.parsePAMInfo=c,e.parseSpatialReference=u,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
