/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import e from"../../geometry/Extent.js";import r from"../../geometry/Point.js";import t from"../../geometry/operators/support/GeographicTransformation.js";import{decode as s}from"../raster/formats/RasterCodec.js";import{transformPixels as o}from"../raster/functions/pixelTransformUtils.js";import{convertPixelBlockToFeatures as i,compositeBands as n,highlightPixels as l,split as a,clipTile as m,mosaic as c,approximateTransform as f,getLocalArithmeticNorthRotations as p}from"../raster/functions/pixelUtils.js";import{create as u}from"../raster/functions/rasterFunctionHelper.js";import{load as x,getProjectionOffsetGrid as d}from"../raster/functions/rasterProjectionHelper.js";import{computeStatisticsHistograms as S,estimateStatisticsHistograms as h}from"../raster/functions/stretchUtils.js";import{convertVectorFieldData as k,convertToLocalDirections as g}from"../raster/functions/vectorFieldUtils.js";import{readTransform as B}from"../raster/transforms/utils.js";import O from"./PixelBlock.js";import y from"../../renderers/support/RasterSymbolizer.js";import{createFlowMesh as P}from"../../views/2d/engine/flow/dataUtils.js";function N(e){if(!e)return{result:null,transferList:[]};const{pixelBlock:r,transferList:t}=e.getTransferableObject();return{result:r,transferList:t}}class J{convertVectorFieldData(e){const r=O.fromJSON(e.pixelBlock),t=N(k(r,e.type));return Promise.resolve(t)}convertPixelBlockToFeatures(r){const t=i({pixelBlock:O.fromJSON(r.pixelBlock),extent:e.fromJSON(r.extent),fieldNames:r.fieldNames,skipFactor:r.skipFactor,skipSpatialReference:!0,pixelIdOffset:r.pixelIdOffset,imageRowSize:r.imageRowSize});return Promise.resolve(t)}transformPixels(e){const r=O.fromJSON(e.pixelBlock),t=o(r,e.transformType,e.transformParameters);return Promise.resolve(N(t))}computeStatisticsHistograms(e){const r=O.fromJSON(e.pixelBlock),t=S(r,{histogramSize:e.histogramSize,includeSkewnessKurtosis:e.includeSkewnessKurtosis});return Promise.resolve(t)}compositeBands(e){const r=e.pixelBlocks.map(e=>e&&O.fromJSON(e)),t=N(n(r));return Promise.resolve(t)}async decode(e){return N(await s(e.data,e.options))}symbolize(r){const t=r.pixelBlock?O.fromJSON(r.pixelBlock):null,s=r.extent?e.fromJSON(r.extent):null,o=N(this.symbolizer.symbolize({...r,pixelBlock:t,extent:s}));return Promise.resolve(o)}highlightPixels(e){const r=O.fromJSON(e.pixelBlock),t=O.fromJSON(e.renderedPixelBlock);return l({pixelBlock:r,renderedPixelBlock:t,highlightOptions:e.highlightOptions}),Promise.resolve(t.toJSON())}async updateSymbolizer(e){this.symbolizer=y.fromJSON(e.symbolizerJSON),e.histograms&&"rasterStretch"===this.symbolizer?.rendererJSON.type&&(this.symbolizer.rendererJSON.histograms=e.histograms)}async updateRasterFunction(e){this.rasterFunction=u(e.rasterFunctionJSON)}async process(t){return N(this.rasterFunction.process({extent:e.fromJSON(t.extent),primaryPixelBlocks:t.primaryPixelBlocks.map(e=>null!=e?O.fromJSON(e):null),primaryPixelSizes:t.primaryPixelSizes?.map(e=>null!=e?r.fromJSON(e):null),primaryRasterIds:t.primaryRasterIds},t.parameters))}stretch(e){const r=N(this.symbolizer.simpleStretch(O.fromJSON(e.srcPixelBlock),e.stretchParams));return Promise.resolve(r)}estimateStatisticsHistograms(e){const r=h(O.fromJSON(e.srcPixelBlock));return Promise.resolve(r)}split(e){const r=a(O.fromJSON(e.srcPixelBlock),e.tileSize,e.maximumPyramidLevel??0,!1===e.useBilinear),t=[];let s;return r&&(s=new Map,r.forEach((e,r)=>{if(e){const{pixelBlock:o,transferList:i}=e.getTransferableObject();s.set(r,o),i.forEach(e=>{t.includes(e)||t.push(e)})}})),Promise.resolve({result:s,transferList:t})}clipTile(e){const r=O.fromJSON(e.pixelBlock),t=N(m({...e,pixelBlock:r}));return Promise.resolve(t)}async mosaicAndTransform(e){const r=e.srcPixelBlocks.map(e=>e?new O(e):null),t=c(r,e.srcMosaicSize,{blockWidths:e.blockWidths,alignmentInfo:e.alignmentInfo,clipOffset:e.clipOffset,clipSize:e.clipSize});let s,o=t;e.coefs&&(o=f(t,e.destDimension,e.coefs,e.sampleSpacing,e.interpolation)),e.projectDirections&&e.gcsGrid&&(s=p(e.destDimension,e.gcsGrid),o=g(o,e.isUV?"vector-uv":"vector-magdir",s));const{result:i,transferList:n}=N(o);return{result:{pixelBlock:i,localNorthDirections:s},transferList:n}}async createFlowMesh(e,r){const t={data:new Float32Array(e.flowData.buffer),mask:new Uint8Array(e.flowData.maskBuffer),width:e.flowData.width,height:e.flowData.height},{vertexData:s,indexData:o,pathData:i}=await P(e.meshType,e.simulationSettings,t,e.startInfo,r.signal);return{result:{vertexBuffer:s.buffer,indexBuffer:o.buffer,pathBuffer:i.buffer},transferList:[s.buffer,o.buffer]}}async getProjectionOffsetGrid(r){const s=e.fromJSON(r.projectedExtent),o=e.fromJSON(r.srcBufferExtent);let i=null;r.datumTransformationSteps?.length&&(i=new t({steps:r.datumTransformationSteps})),await x();const n=r.rasterTransform?B(r.rasterTransform):null;return d({...r,projectedExtent:s,srcBufferExtent:o,datumTransformation:i,rasterTransform:n})}}export{J as default};
