/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__decorate as t}from"tslib";import e from"../../../core/Error.js";import has from"../../../core/has.js";import{eachAlways as s,createAbortError as r}from"../../../core/promiseUtils.js";import{property as a,subclass as o}from"../../../core/accessorSupport/decorators.js";import i from"../../../geometry/Extent.js";import n from"../../../geometry/SpatialReference.js";import l from"./BaseRaster.js";import m from"./InMemoryRaster.js";import{parsePAMInfo as f}from"./pamParser.js";import{getFormat as p}from"../formats/RasterCodec.js";import{estimateStatisticsFromHistograms as c}from"../functions/stretchUtils.js";import u from"../transforms/PolynomialTransform.js";let h=class extends l{fetchRawTile(t,e,s,r={}){return this._inMemoryRaster.fetchRawTile(t,e,s,r)}async _open(t){const e=await this._fetchData(t);let{spatialReference:s,statistics:r,histograms:a,transform:o}=await this._fetchAuxiliaryData(t);const l=!s;l&&(s=new n({wkid:3857})),a?.length&&null==r&&(r=c(a));const{width:f,height:p}=e;let u=new i({xmin:-.5,ymin:.5-p,xmax:f-.5,ymax:.5,spatialReference:s});const h=o?o.forwardTransform(u):u;let d=!0;if(o){const t=o.forwardCoefficients;d=t&&0===t[1]&&0===t[2],d&&(o=null,u=h)}const w=new m({source:{extent:h,nativeExtent:u,transform:o,pixelBlocks:[e],statistics:r,histograms:a,keyProperties:{DateType:"Processed"},isPseudoSpatialReference:l},ioConfig:{sampling:"closest",skipStatistics:!0}});this.ioConfig.skipMapInfo&&(w.ioConfig.skipMapInfo=!0),await w.open(),w.source=null,this._set("rasterInfo",w.rasterInfo),this._inMemoryRaster=w}async _fetchData(t){const{data:s}=await this.request(this.url,{responseType:"array-buffer",signal:t?.signal}),r=p(s).toUpperCase();if("JPG"!==r&&"PNG"!==r&&"GIF"!==r&&"BMP"!==r)throw new e("image-aux-raster:open","the data is not a supported format");this._set("datasetFormat",r);const a=r.toLowerCase(),o="gif"===a||"bmp"===a||!has("ios"),i=await this.decodePixelBlock(s,{format:a,useCanvas:o,hasNoZlibMask:!0});if(null==i)throw new e("image-aux-raster:open","the data cannot be decoded");return i}async _fetchAuxiliaryData(t){const e=t?.signal,{skipExtensions:a=[],skipMapInfo:o}=this.ioConfig,i=o||a.includes("aux.xml")?null:this.request(this.url+".aux.xml",{responseType:"xml",signal:e}),n=this.datasetFormat,l="JPG"===n?"jgw":"PNG"===n?"pgw":"BMP"===n?"bpw":null,m=l&&a.includes(l)?null:this.request(this.url.slice(0,this.url.lastIndexOf("."))+"."+l,{responseType:"text",signal:e}),p=await s([i,m]);if(e?.aborted)throw r();const c=f(p[0].value?.data);if(!c.transform){const t=p[1].value?p[1].value.data.split("\n").slice(0,6).map(t=>Number(t)):null;c.transform=6===t?.length?new u({forwardCoefficients:[t[4],t[5],t[0],-t[1],t[2],-t[3]]}):null}return c}};t([a({type:String,json:{write:!0}})],h.prototype,"datasetFormat",void 0),h=t([o("esri.layers.raster.datasets.ImageAuxRaster")],h);const d=h;export{d as default};
