/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["require","exports","../../../assets"],(function(e,t,n){"use strict";const r=[{pixelType:"S8",size:1,ctor:Int8Array,range:[-128,128]},{pixelType:"U8",size:1,ctor:Uint8Array,range:[0,255]},{pixelType:"S16",size:2,ctor:Int16Array,range:[-32768,32767]},{pixelType:"U16",size:2,ctor:Uint16Array,range:[0,65536]},{pixelType:"S32",size:4,ctor:Int32Array,range:[-2147483648,2147483647]},{pixelType:"U32",size:4,ctor:Uint32Array,range:[0,4294967296]},{pixelType:"F32",size:4,ctor:Float32Array,range:[-34027999387901484e22,34027999387901484e22]},{pixelType:"F64",size:8,ctor:Float64Array,range:[-17976931348623157e292,17976931348623157e292]}];let a=null,l=!1;function o(){return a||(a=new Promise(((t,n)=>e(["../../../chunks/lerc-wasm"],t,n))).then((e=>e.lercWasm)).then((({default:e})=>e({locateFile:e=>n.getAssetUrl(`esri/layers/support/rasterFormats/${e}`)}))).then((e=>{c(e),l=!0})),a)}function s(){return l}const i={getBlobInfo:null,decode:null};function u(e){return 16+(e>>3<<3)}function f(e,t,n){n.set(e.slice(t,t+n.length))}function c(e){const{_malloc:t,_free:n,_lerc_getBlobInfo:a,_lerc_getDataRanges:l,_lerc_decode_4D:o,asm:s}=e;let c;const y=Object.values(s).find((t=>t&&"buffer"in t&&t.buffer===e.HEAPU8.buffer)),d=e=>{const n=e.map((e=>u(e))),r=n.reduce(((e,t)=>e+t)),a=t(r);c=new Uint8Array(y.buffer);let l=n[0];n[0]=a;for(let t=1;t<n.length;t++){const e=n[t];n[t]=n[t-1]+l,l=e}return n};i.getBlobInfo=e=>{const t=12,r=3,o=new Uint8Array(4*t),s=new Uint8Array(8*r),[i,u,h]=d([e.length,o.length,s.length]);c.set(e,i),c.set(o,u),c.set(s,h);let p=a(i,e.length,u,h,t,r);if(p)throw n(i),`lerc-getBlobInfo: error code is ${p}`;c=new Uint8Array(y.buffer),f(c,u,o),f(c,h,s);const g=new Uint32Array(o.buffer),b=new Float64Array(s.buffer),[m,A,w,U,x,T,C,V,I,,_]=g,z={version:m,dimCount:w,width:U,height:x,validPixelCount:C,bandCount:T,blobSize:V,maskCount:I,dataType:A,minValue:b[0],maxValue:b[1],maxZerror:b[2],statistics:[],bandCountWithNoData:_};if(_)return z;if(1===w&&1===T)return n(i),z.statistics.push({minValue:b[0],maxValue:b[1]}),z;const D=w*T*8,F=new Uint8Array(D),v=new Uint8Array(D);let k=i,B=0,S=0,M=!1;if(c.byteLength<i+2*D?(n(i),M=!0,[k,B,S]=d([e.length,D,D]),c.set(e,k)):[B,S]=d([D,D]),c.set(F,B),c.set(v,S),p=l(k,e.length,w,T,B,S),p)throw n(k),M||n(B),`lerc-getDataRanges: error code is ${p}`;c=new Uint8Array(y.buffer),f(c,B,F),f(c,S,v);const O=new Float64Array(F.buffer),P=new Float64Array(v.buffer),$=z.statistics;for(let n=0;n<T;n++)if(w>1){const e=O.slice(n*w,(n+1)*w),t=P.slice(n*w,(n+1)*w),r=Math.min.apply(null,e),a=Math.max.apply(null,t);$.push({minValue:r,maxValue:a,dimStats:{minValues:e,maxValues:t}})}else $.push({minValue:O[n],maxValue:P[n]});return n(k),M||n(B),z},i.decode=(e,t)=>{const{maskCount:a,dimCount:l,bandCount:s,width:i,height:u,dataType:h,bandCountWithNoData:p}=t,g=r[h],b=i*u,m=new Uint8Array(b*s),A=b*l*s*g.size,w=new Uint8Array(A),U=new Uint8Array(s),x=new Uint8Array(8*s),[T,C,V,I,_]=d([e.length,m.length,w.length,U.length,x.length]);c.set(e,T),c.set(m,C),c.set(w,V),c.set(U,I),c.set(x,_);const z=o(T,e.length,a,C,l,i,u,s,h,V,I,_);if(z)throw n(T),`lerc-decode: error code is ${z}`;c=new Uint8Array(y.buffer),f(c,V,w),f(c,C,m);let D=null;if(p){f(c,I,U),f(c,_,x),D=[];const e=new Float64Array(x.buffer);for(let t=0;t<U.length;t++)D.push(U[t]?e[t]:null)}return n(T),{data:w,maskData:m,noDataValues:D}}}function y(e,t,n,r,a){if(n<2)return e;const l=new r(t*n);if(a)for(let o=0,s=0;o<t;o++)for(let r=0,a=o;r<n;r++,a+=t)l[a]=e[s++];else for(let o=0,s=0;o<t;o++)for(let r=0,a=o;r<n;r++,a+=t)l[s++]=e[a];return l}function d(e,t={}){var n;const a=null!=(n=t.inputOffset)?n:0,l=e instanceof Uint8Array?e.subarray(a):new Uint8Array(e,a),o=i.getBlobInfo(l),{data:s,maskData:u}=i.decode(l,o),{width:f,height:c,bandCount:d,dimCount:h,dataType:p,maskCount:g,statistics:b}=o,m=r[p],A=new m.ctor(s.buffer),w=[],U=[],x=f*c,T=x*h;for(let r=0;r<d;r++){const e=A.subarray(r*T,(r+1)*T);if(t.returnPixelInterleavedDims)w.push(e);else{const t=y(e,x,h,m.ctor,!0);w.push(t)}U.push(u.subarray(r*T,(r+1)*T))}const C=0===g?null:1===g?U[0]:new Uint8Array(x);if(g>1){C.set(U[0]);for(let e=1;e<U.length;e++){const t=U[e];for(let e=0;e<x;e++)C[e]=C[e]&t[e]}}const{noDataValue:V}=t,I=null!=V&&m.range[0]<=V&&m.range[1]>=V;if(g>0&&I)for(let r=0;r<d;r++){const e=w[r],t=U[r]||C;for(let n=0;n<x;n++)0===t[n]&&(e[n]=V)}const _=g===d&&d>1?U:null;return{width:f,height:c,bandCount:d,pixelType:t.pixelType&&0===o.version?t.pixelType:m.pixelType,dimCount:h,statistics:b,pixels:w,mask:C,bandMasks:_}}function h(e,t={}){var n,r;const a=e instanceof Uint8Array?e.subarray(null!=(n=t.inputOffset)?n:0):new Uint8Array(e,null!=(r=t.inputOffset)?r:0);return i.getBlobInfo(a)}t.decode=d,t.getBlobInfo=h,t.isLoaded=s,t.load=o,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
