/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../../../core/Error.js";import{eachAlways as i}from"../../../core/promiseUtils.js";import{property as s,subclass as r}from"../../../core/accessorSupport/decorators.js";import a from"../../../geometry/Extent.js";import o from"../../../geometry/SpatialReference.js";import l from"./BaseRaster.js";import{getSliceIndex as n}from"./multidimensionalUtils.js";import{clipTile as m,split as p}from"../functions/pixelUtils.js";import{estimateStatisticsHistograms as c,computeStatisticsHistograms as u}from"../functions/stretchUtils.js";import h from"../../support/DimensionalDefinition.js";import d from"../../support/RasterInfo.js";let f=class extends l{constructor(){super(...arguments),this.datasetFormat="MEMORY",this.preGenerateTiles=!0,this.source=null}get url(){return""}fetchRawTile(e,t,i,s={}){if(!this._pixelBlockTiles){const{rasterInfo:r}=this,[a,o]=r.storageInfo.tileInfo.size,{sliceId:l}=s,{pixelBlocks:n}=this.source,p={pixelBlock:null==l?n[0]:n[l],useBilinear:"thematic"!==r.dataType,tileSize:{width:a,height:o},level:e,row:t,col:i},c=this.rasterJobHandler?this.rasterJobHandler.clipTile(p,s):m(p);return Promise.resolve(c)}const r=this._pixelBlockTiles.get(`${e}/${t}/${i}`);return Promise.resolve(r)}async _open(e){const t=this.source,{pixelBlocks:i,attributeTable:s,statistics:r,histograms:l,name:n,nativeExtent:m,transform:p,colormap:c}=t,u=i[0],{width:h,height:f,pixelType:y}=u,x=t.extent??new a({xmin:-.5,ymin:.5,xmax:h-.5,ymax:f-.5,spatialReference:new o({wkid:3857})}),g=t.isPseudoSpatialReference??!t.extent,b={x:x.width/h,y:x.height/f},v={...t.keyProperties};t.dataType&&(v.DataType=t.dataType),t.bandInfos&&(v.BandProperties=t.bandInfos.map(e=>e.toJSON())),s&&(v.DataType="Thematic");const I=new d({width:h,height:f,pixelType:y,extent:x,nativeExtent:m,attributeTable:s,colormap:c,transform:p,pixelSize:b,spatialReference:x.spatialReference,bandCount:u.pixels.length,keyProperties:v,multidimensionalInfo:t.multidimensionalInfo,statistics:r,isPseudoSpatialReference:g,histograms:l});this.ioConfig.skipMapInfo&&this.updateImageSpaceRasterInfo(I),this.createRemoteDatasetStorageInfo(I,512,512),this._set("rasterInfo",I),this.updateTileInfo(),I.multidimensionalInfo?await this._buildMDimStats(t.pixelBlocks,I.multidimensionalInfo):this.preGenerateTiles&&await this._buildInMemoryRaster(u,{width:512,height:512},e),I.multidimensionalInfo||(this.source=null),this.datasetName=n}async _buildInMemoryRaster(e,s,r){const{rasterInfo:a}=this,o=a.storageInfo.maximumPyramidLevel??0,l="thematic"!==a.dataType,n=this.rasterJobHandler?this.rasterJobHandler.split({pixelBlock:e,tileSize:s,maximumPyramidLevel:o,useBilinear:l},r):Promise.resolve(p(e,s,o,l)),m=null!=a.statistics,u=null!=a.histograms,h=this.ioConfig.skipStatistics||m?Promise.resolve({statistics:null,histograms:null}):this.rasterJobHandler?this.rasterJobHandler.estimateStatisticsHistograms({pixelBlock:e},r):Promise.resolve(c(e)),d=await i([n,h]);if(!d[0].value&&d[1].value)throw new t("inmemory-raster:open","failed to build in memory raster");this._pixelBlockTiles=d[0].value,m||(a.statistics=d[1].value?.statistics),u||(a.histograms=d[1].value?.histograms)}async _buildMDimStats(e,t,i){for(let s=0;s<t.variables.length;s++){const r=t.variables[s];if(r.statistics)continue;const a=r.dimensions.map(e=>new h({variableName:r.name,dimensionName:e.name,values:[e.values?.[0]??e.extent?.[0]],isSlice:!0})),o=n(a,t),l=null==o?null:e[o];if(null==l)continue;const m=this.rasterJobHandler?await this.rasterJobHandler.computeStatisticsHistograms({pixelBlock:l},i):u(l);r.statistics=m.statistics,r.histograms||(r.histograms=m.histograms)}}};e([s({type:String,json:{write:!0}})],f.prototype,"datasetFormat",void 0),e([s({constructOnly:!0})],f.prototype,"preGenerateTiles",void 0),e([s()],f.prototype,"source",void 0),e([s()],f.prototype,"url",null),f=e([r("esri.layers.raster.datasets.InMemoryRaster")],f);const y=f;export{y as default};
