/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../chunks/_rollupPluginBabelHelpers","../../../chunks/tslib.es6","../../../geometry","../../../core/Error","../../../core/has","../../../core/maybe","../../../core/promiseUtils","../../../core/accessorSupport/decorators/property","../../../core/arrayUtils","../../../core/accessorSupport/ensureType","../../../core/accessorSupport/decorators/subclass","./BaseRaster","./InMemoryRaster","./pamParser","../rasterFormats/RasterCodec","../rasterFunctions/stretchUtils","../rasterTransforms/PolynomialTransform","../../../geometry/SpatialReference","../../../geometry/Extent"],(function(e,t,r,s,a,n,o,i,l,c,u,p,f,h,d,m,y,g,w){"use strict";let x=function(t){function r(){return t.apply(this,arguments)||this}e._inheritsLoose(r,t);var i=r.prototype;return i.open=function(){var t=e._asyncToGenerator((function*(e){yield this.init();const t=yield this._fetchData(e);let{spatialReference:r,statistics:s,histograms:a,transform:n}=yield this._fetchAuxiliaryData(e);const o=!r;o&&(r=new g({wkid:3857})),null!=a&&a.length&&null==s&&(s=m.estimateStatisticsFromHistograms(a));const{width:i,height:l}=t;let c=new w({xmin:-.5,ymin:.5-l,xmax:i-.5,ymax:.5,spatialReference:r});const u=n?n.forwardTransform(c):c;let p=!0;if(n){const e=n.forwardCoefficients;p=e&&0===e[1]&&0===e[2],p&&(n=null,c=u)}const h=new f({data:{extent:u,nativeExtent:c,transform:n,pixelBlock:t,statistics:s,histograms:a,keyProperties:{DateType:"Processed"},isPseudoSpatialReference:o}});yield h.open(),h.data=null,this._set("rasterInfo",h.rasterInfo),this._inMemoryRaster=h}));function r(e){return t.apply(this,arguments)}return r}(),i.fetchRawTile=function(e,t,r,s={}){return this._inMemoryRaster.fetchRawTile(e,t,r,s)},i._fetchData=function(){var t=e._asyncToGenerator((function*(e){const{data:t}=yield this.request(this.url,{responseType:"array-buffer",signal:null==e?void 0:e.signal}),r=d.getFormat(t).toUpperCase();if("JPG"!==r&&"PNG"!==r&&"GIF"!==r&&"BMP"!==r)throw new s("image-aux-raster:open","the data is not a supported format");this._set("datasetFormat",r);const n=r.toLowerCase(),o="gif"===n||"bmp"===n||!a("ios");return yield this.decodePixelBlock(t,{format:n,useCanvas:o,hasNoZlibMask:!0})}));function r(e){return t.apply(this,arguments)}return r}(),i._fetchAuxiliaryData=function(){var t=e._asyncToGenerator((function*(e){var t,r;const s=n.unwrap(null==e?void 0:e.signal),a=null!=(t=this.ioConfig.skipExtensions)?t:[],i=a.includes("aux.xml")?null:this.request(this.url+".aux.xml",{responseType:"xml",signal:s}),l=this.datasetFormat,c="JPG"===l?"jgw":"PNG"===l?"pgw":"BMP"===l?"bpw":null,u=a.includes(c)?null:this.request(this.url.slice(0,this.url.lastIndexOf("."))+"."+c,{responseType:"text",signal:s}),p=yield o.eachAlways([i,u]);if(null!=s&&s.aborted)throw o.createAbortError();const f=h.parsePAMInfo(null==(r=p[0].value)?void 0:r.data);if(!f.transform){const e=p[1].value?p[1].value.data.split("\n").slice(0,6).map((e=>Number(e))):null;f.transform=6===(null==e?void 0:e.length)?new y({forwardCoefficients:[e[4],e[5],e[0],-e[1],e[2],-e[3]]}):null}return f}));function r(e){return t.apply(this,arguments)}return r}(),r}(p);t.__decorate([i.property({type:String,json:{write:!0}})],x.prototype,"datasetFormat",void 0),x=t.__decorate([u.subclass("esri.layers.support.rasterDatasets.ImageAuxRaster")],x);return x}));
