/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"../../request.js";import t from"../../geometry/Extent.js";import a from"../../geometry/Point.js";import i from"../../geometry/SpatialReference.js";import l from"../../layers/support/RasterInfo.js";import n 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,p){const c=s(m),{rasterFunction:d,sourceJSON:h}=f||{},g=d?JSON.stringify(d.rasterFunctionDefinition||d):null,y=r({...c.query,renderingRule:g,f:"json"}),x=o(y,p);m=c.path;const S=h||await e(m,x).then(e=>e.data),b=S.hasRasterAttributeTable?e(`${m}/rasterAttributeTable`,x):null,v=S.hasColormap?e(`${m}/colormap`,x):null,V=S.hasHistograms?e(`${m}/histograms`,x):null,D=S.currentVersion>=10.3?e(`${m}/keyProperties`,x):null,I=S.hasMultidimensions?e(`${m}/multidimensionalInfo`,x):null,T=await Promise.allSettled([b,v,V,D,I]);let R=null;if(S.minValues&&S.minValues.length===S.bandCount){R=[];for(let e=0;e<S.minValues.length;e++)R.push({min:S.minValues[e],max:S.maxValues[e],avg:S.meanValues[e],stddev:S.stdvValues[e]})}const j=t.fromJSON(S.extent),w=Math.ceil(j.width/S.pixelSizeX-.1),M=Math.ceil(j.height/S.pixelSizeY-.1),N=i.fromJSON(S.spatialReference||S.extent.spatialReference),O="fulfilled"===T[0].status?T[0].value?.data:null,J=O?.features?.length?u.fromJSON(O):null,k="fulfilled"===T[1].status?T[1].value?.data.colormap:null,C=k?.length?k:null,P="fulfilled"===T[2].status?T[2].value?.data.histograms:null,z=P?.[0]?.counts?.length?P:null,F="fulfilled"===T[3].status?T[3].value?.data??{}:{},$="fulfilled"===T[4].status?T[4].value?.data.multidimensionalInfo:null,E=$?.variables?.length?$: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")})});const{defaultVariable:H,serviceDataType:L}=S;H&&H!==F.DefaultVariable&&(F.DefaultVariable=H),L?.includes("esriImageServiceDataTypeVector")&&!L.includes(F.DataType)&&(F.DataType=L.replace("esriImageServiceDataType",""));let A=S.noDataValue;S.noDataValues?.length&&S.noDataValues.some(e=>e!==A)&&(A=S.noDataValues);const q=S.transposeInfo?new n({blockWidth:256,blockHeight:256,pyramidBlockWidth:256,pyramidBlockHeight:256,pyramidScalingFactor:2,compression:"lerc",origin:new a({x:S.extent.xmin,y:S.extent.ymax,spatialReference:N}),firstPyramidLevel:1,maximumPyramidLevel:Math.max(0,Math.round(Math.log(Math.max(w,M))/Math.LN2-8)),transposeInfo:S.transposeInfo}):void 0;return new l({width:w,height:M,bandCount:S.bandCount,extent:t.fromJSON(S.extent),spatialReference:N,pixelSize:new a({x:S.pixelSizeX,y:S.pixelSizeY,spatialReference:N}),pixelType:S.pixelType.toLowerCase(),statistics:R,attributeTable:J,colormap:C,histograms:z,keyProperties:F,noDataValue:A,multidimensionalInfo:E,storageInfo:q})}function f(e,t,a){return m(e,{sourceJSON:t},a)}function p(e,t,a){return m(e,{rasterFunction:t},a)}function c(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,p as generateRasterInfo,c as patchServiceInfo};
