/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"../../../Color.js";import t from"../../../geometry/Extent.js";import n from"../../../geometry/SpatialReference.js";import{getLocaleLanguage as l}from"../../../intl/locale.js";import{getIntegerPixelType as s}from"../formats/pixelRangeUtils.js";import a from"../../support/PixelBlock.js";function i(e){return["x","e","east","long","longitude"].includes(e.toLowerCase())}function r(e){return["y","n","west","lat","latitude"].includes(e.toLowerCase())}function o(e){const{axes:l}=e.domain,s=Object.keys(l),a=[],o=[];let u=-1,m=-1,c=[];for(let t=0;t<s.length;t++){const e=s[t];i(e)?u=t:r(e)&&(m=t);const n=l[e],d=[];if("values"in n){n.values.forEach(e=>d.push("string"==typeof e?new Date(e).getTime():e));const e=d[1]-d[0];a.push([d[0]-.5*e,d[d.length-1]+.5*e]),o.push(e)}else{const{start:e,stop:t,num:l}=n,s=(t-e)/(l-1);a.push([e-.5*s,t+.5*s]),o.push(s);for(let n=0;n<l;n++)d.push(e+s*n)}c.push({name:e,values:d,extent:[d[0],d[d.length-1]]})}u>-1&&-1===m?m=0===u?1:0:m>-1&&-1===u?u=0===m?1:0:-1===m&&-1===u&&(u=0,m=1),c=c.filter((e,t)=>!(t===u||t===m));const{referencing:d}=e.domain,f=d.find(e=>e.coordinates.includes(s[u])).system.id,p=f?.slice(f.lastIndexOf("/")+1),h=null==p||"CRS84"===p?4326:Number(p),g=new n({wkid:h}),[b,y]=a[u],[x,j]=a[m],v=new t({xmin:b,xmax:y,ymin:x,ymax:j,spatialReference:g});return{width:Math.round(v.width/o[u]),height:Math.round(v.height/o[m]),extent:v,dimensions:c}}function u(e){const t=l();return t?e[t]??Object.values(e)[0]:Object.values(e)[0]}function m(){return Math.round(255*Math.random())}function c(t){const n={},{parameters:l}=t;if(!l)return n;for(const[s,a]of Object.entries(l)){const{type:t,description:l,unit:i,categoryEncoding:r,observedProperty:o}=a;if("Parameter"===t&&(n[s]={},l&&(n[s].description=u(l)),i&&(n[s].unit=i.label?u(i.label):null,n[s].symbol=i.symbol?.value),r)){const t=Object.entries(r).map((e,t)=>({OID:t,Value:Number(e[1]),ClassName:e[0].slice(e[0].lastIndexOf("/")+1),Count:1}));let l=!1;o?.categories?.length&&(o.categories.forEach(n=>{if(!n.id)return;const s=n.id.slice(n.id.lastIndexOf("/")+1),a=t.find(e=>e.ClassName===s);if(!a)return;const i=n.label?u(n.label):null;if(a.Label=i,n.preferredColor){const t=e.fromHex(n.preferredColor);t&&(l=!0,a.Red=t.r,a.Green=t.g,a.Blue=t.b)}}),l&&t.forEach(e=>{null==e.Red&&(e.Red=m(),e.Green=m(),e.Blue=m())}));const a={objectIdFieldName:"",fields:[{name:"OID",type:"esriFieldTypeOID",alias:"OID",domain:null},{name:"Value",type:"esriFieldTypeInteger",alias:"Value",domain:null},{name:"Count",type:"esriFieldTypeDouble",alias:"Count",domain:null},{name:"ClassName",type:"esriFieldTypeString",alias:"ClassName",domain:null,length:50},{name:"Label",type:"esriFieldTypeString",alias:"Label",domain:null,length:50}],features:t.map(e=>({attributes:e}))};l&&a.fields.push({name:"Red",type:"esriFieldTypeInteger",alias:"Red",domain:null},{name:"Green",type:"esriFieldTypeInteger",alias:"Green",domain:null},{name:"Blue",type:"esriFieldTypeInteger",alias:"Blue",domain:null}),n[s].attributeTable=a}}return n}function d(e){let t=Number.MAX_VALUE,n=-Number.MAX_VALUE;for(let l=0;l<e.length;l++){const s=e[l];null!=s&&(s<t&&(t=s),s>n&&(n=s))}return s(t,n)}function f(e,t,n){const l=e.map((e,n)=>({name:e,count:t[n]})).sort((e,t)=>e.name>t.name?-1:1),s=(a=1,e=>a*=e.count);var a;const i=[...l.slice(1),{name:"",count:1}].reverse().map(s).reverse();let r=0;for(let o=e.length-1;o>=0;o--){r+=i[l.findIndex(({name:t})=>t===e[o])]*(n%t[o]),n=Math.floor(n/t[o])}return r}function p(e){const{width:t,height:n,extent:l,dimensions:s}=o(e),{ranges:i}=e,r=Object.keys(i).sort((e,t)=>e<t?-1:1),u=[];for(let a=0;a<r.length;a++){const e=r[a];s?.length&&u.push({name:e,dimensions:s})}const m=c(e);u.forEach(e=>m[e.name]&&Object.assign(e,m[e.name]));const p=u.length?{variables:u}:void 0,h=[];for(let o=0;o<r.length;o++){const e=r[o],{values:l,dataType:u,axisNames:m,shape:c}=i[e],p=c.length>2?o*c.slice(0,-2).reduce((e,t)=>e*t):0,g=m.slice(0,-2),b=c.slice(0,-2),y="float"===u?"f32":d(l),x=t*n,j=l.length/x;for(let i=0;i<j;i++){const e=a.createEmptyBand(y,x),r=new Uint8Array(x).fill(255);let u=!1;const m=i*x;for(let t=0;t<x;t++){const n=l[m+t];null==n?(r[t]=0,u=!0):e[t]=n}if(0===o||s?.length){const l=new a({width:t,height:n,mask:u?r:null,pixels:[e],pixelType:y});if(l.updateStatistics(),s?.length){h[f(g,b,i)+p]=l}else h.push(l)}else{const t=h[i];t.pixels.push(e),u?t.mask&&(t.mask=a.combineBandMasks([t.mask,r])):t.mask=u?r:null}}}const g=Object.values(m).find(e=>e.attributeTable)?.attributeTable;return{extent:l,pixelBlocks:h,multidimensionalInfo:p,attributeTable:g,bandNames:p?void 0:r}}export{i as isXAxis,r as isYAxis,p as parseGridCoverage};
