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