/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{__decorate as t}from"tslib";import e from"../../../core/Error.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 c from"./InMemoryRaster.js";import{parsePAMInfo as m}from"./pamParser.js";import{getFormat as f}from"../formats/RasterCodec.js";import{estimateStatisticsFromHistograms as p}from"../functions/stretchUtils.js";import h from"../transforms/PolynomialTransform.js";let u=class extends l{set rasterJobHandler(t){this._set("rasterJobHandler",t),this._inMemoryRaster&&(this._inMemoryRaster.rasterJobHandler=t)}fetchRawTile(t,e,s,r={}){if(this._imageBlob){const{blockWidth:r,blockHeight:a}=this.rasterInfo.storageInfo;return this.decodePixelBlock(this._imageBlob,{tileSize:[r,a],tileIndex:[t,e,s],canvasUsage:"canvas-only",cloneSource:!0,skipStatistics:!0})}return this._inMemoryRaster.fetchRawTile(t,e,s,r)}async _open(t){const s=await this._fetchData(t);if(null==s)throw new e("image-aux-raster:open","the data cannot be decoded");let{spatialReference:r,statistics:a,histograms:o,transform:l}=await this._fetchAuxiliaryData(t);const m=!r;m&&(r=new n({wkid:3857})),o?.length&&null==a&&(a=p(o));const{width:f,height:h}=s;let u=new i({xmin:-.5,ymin:.5-h,xmax:f-.5,ymax:.5,spatialReference:r});const d=l?l.forwardTransform(u):u;let g=!0;if(l){const t=l.forwardCoefficients;g=t&&0===t[1]&&0===t[2],g&&(l=null,u=d)}const{rasterJobHandler:y,ioConfig:w}=this,x=new c({preGenerateTiles:!1,rasterJobHandler:y,source:{extent:d,nativeExtent:u,transform:l,pixelBlocks:[s],statistics:a,histograms:o,keyProperties:{DateType:"Processed"},isPseudoSpatialReference:m},ioConfig:{...w,sampling:"closest",skipStatistics:!0}});await x.open(),x.source=null,this._set("rasterInfo",x.rasterInfo),this._inMemoryRaster=x}async _fetchData(t){const{data:s}=await this.request(this.url,{responseType:"array-buffer",signal:t?.signal}),r=f(s);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.toUpperCase());const a=r.toLowerCase();try{const t=await this.decodePixelBlock(s,{format:a,canvasUsage:"canvas-only",cloneSource:!0,skipStatistics:!0});return this._imageBlob=s,t}catch{return this.decodePixelBlock(s,{format:a,canvasUsage:"no-canvas",skipStatistics:!0})}}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,c=l&&a.includes(l)?null:this.request(this.url.slice(0,this.url.lastIndexOf("."))+"."+l,{responseType:"text",signal:e}),f=await s([i,c]);if(e?.aborted)throw r();const p=m(f[0].value?.data);if(!p.transform){const t=f[1].value?f[1].value.data.split("\n").slice(0,6).map(t=>Number(t)):null;p.transform=6===t?.length?new h({forwardCoefficients:[t[4],t[5],t[0],-t[1],t[2],-t[3]]}):null}return p}};t([a({type:String,json:{write:!0}})],u.prototype,"datasetFormat",void 0),t([a()],u.prototype,"rasterJobHandler",null),u=t([o("esri.layers.raster.datasets.ImageAuxRaster")],u);const d=u;export{d as default};
