/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import e from"../../request.js";import t from"../../geometry/Extent.js";import a from"../../geometry/Point.js";import i from"../../geometry/SpatialReference.js";import n from"../../layers/support/RasterInfo.js";import l from"../../layers/support/RasterStorageInfo.js";import{parseUrl as s,encode as r,asValidOptions as o}from"../utils.js";import u from"../support/FeatureSet.js";async function m(m,f,c){const d=s(m),{rasterFunction:p,sourceJSON:h}=f||{},g=p?JSON.stringify(p.rasterFunctionDefinition||p):null,x=r({...d.query,renderingRule:g,f:"json"}),y=o(x,c);m=d.path;const v=h||await e(m,y).then(e=>e.data),b=v.hasRasterAttributeTable?e(`${m}/rasterAttributeTable`,y):null,S=v.hasColormap?e(`${m}/colormap`,y):null,V=v.hasHistograms?e(`${m}/histograms`,y):null,D=v.currentVersion>=10.3?e(`${m}/keyProperties`,y):null,I=v.hasMultidimensions?e(`${m}/multidimensionalInfo`,y):null,T=v.currentVersion>=10.81?e(`${m}/statistics`,y):null,R=await Promise.allSettled([b,S,V,D,I,T]),j=t.fromJSON(v.extent),w=Math.ceil(j.width/v.pixelSizeX-.1),M=Math.ceil(j.height/v.pixelSizeY-.1),N=i.fromJSON(v.spatialReference||v.extent.spatialReference),O="fulfilled"===R[0].status?R[0].value?.data:null,C=O?.features?.length?u.fromJSON(O):null,J="fulfilled"===R[1].status?R[1].value?.data.colormap:null,k=J?.length?J:null,P="fulfilled"===R[2].status?R[2].value?.data.histograms:null,$=P?.[0]?.counts?.length?P:null,z="fulfilled"===R[3].status?R[3].value?.data??{}:{},F="fulfilled"===R[4].status?R[4].value?.data.multidimensionalInfo:null,E=F?.variables?.length?F:null;E&&E.variables.forEach(e=>{e.statistics?.length&&e.statistics.forEach(e=>{e.avg=e.mean,e.stddev=e.standardDeviation}),e.dimensions?.forEach(e=>{"StdTime"!==e.name||e.recurring||e.unit||(e.unit="ISO8601")})});let H=null;const L="fulfilled"===R[5].status?R[5].value?.data.statistics:null;if(L&&L.length===v.bandCount)H=L.map(e=>({avg:e.mean,count:e.count,max:e.max,median:e.median,min:e.min,mode:e.mode,stddev:e.standardDeviation,sum:e.sum}));else if(v.minValues&&v.minValues.length===v.bandCount){H=[];for(let e=0;e<v.minValues.length;e++)H.push({min:v.minValues[e],max:v.maxValues[e],avg:v.meanValues[e],stddev:v.stdvValues[e]})}const{defaultVariable:A,serviceDataType:q}=v;A&&A!==z.DefaultVariable&&(z.DefaultVariable=A),q?.includes("esriImageServiceDataTypeVector")&&!q.includes(z.DataType)&&(z.DataType=q.replace("esriImageServiceDataType",""));let B=v.noDataValue;v.noDataValues?.length&&v.noDataValues.some(e=>e!==B)&&(B=v.noDataValues);const W=v.transposeInfo?new l({blockWidth:256,blockHeight:256,pyramidBlockWidth:256,pyramidBlockHeight:256,pyramidScalingFactor:2,compression:"lerc",origin:new a({x:v.extent.xmin,y:v.extent.ymax,spatialReference:N}),firstPyramidLevel:1,maximumPyramidLevel:Math.max(0,Math.round(Math.log(Math.max(w,M))/Math.LN2-8)),transposeInfo:v.transposeInfo}):void 0;return new n({width:w,height:M,bandCount:v.bandCount,extent:t.fromJSON(v.extent),spatialReference:N,pixelSize:new a({x:v.pixelSizeX,y:v.pixelSizeY,spatialReference:N}),pixelType:v.pixelType.toLowerCase(),statistics:H,attributeTable:C,colormap:k,histograms:$,keyProperties:z,noDataValue:B,multidimensionalInfo:E,storageInfo:W})}function f(e,t,a){return m(e,{sourceJSON:t},a)}function c(e,t,a){return m(e,{rasterFunction:t},a)}function d(e,t){e.attributeTable||(t.hasRasterAttributeTable=!1),e.histograms||(t.hasHistograms=!1),e.colormap||(t.hasColormap=!1),e.multidimensionalInfo||(t.hasMultidimensions=!1)}export{f as fetchServiceRasterInfo,c as generateRasterInfo,d as patchServiceInfo};
