/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import e from"../../../core/Error.js";import t from"../../../core/Logger.js";import{isAbortError as r,waitTick as a}from"../../../core/promiseUtils.js";import o from"../../../geometry/Extent.js";import{projectAsync as n}from"../../../geometry/projectionUtils.js";import{load as i,execute as s}from"../../../geometry/operators/projectOperator.js";import{fromExtent as l}from"../../../geometry/support/aaBoundingRect.js";import c from"../../ElevationLayer.js";import p from"../../ImageryLayer.js";import{getMetersPerUnitOfSR as u}from"../core/utils.js";import{defaultOptions as m}from"../../support/ElevationQuery.js";import{ElevationTile as f}from"../../support/ElevationTile.js";import y from"../../support/ElevationTileData.js";import w from"../../support/RasterFunction.js";import{TileElevationSampler as g}from"../../support/TileElevationSampler.js";import d from"../../support/TileInfo.js";import{TileKey as v}from"../../support/TileKey.js";import{requestServiceJSON as j}from"../../../request/serviceJSON.js";import{logAndThrow as h}from"../../../widgets/OrientedImageryViewer/utils.js";async function R(t,r,a){if(!r.extent||!r.url)throw h("esri.layer.orientedImagery.transformations",new e("update-elevation:missing-property","both extent and url are required to create a sampler",r));const o=await E(r);if(!o)throw h("esri.layers.orientedImagery.transformations",new e("update-elevation:elevation-source","could not create a sampler using provided elevation source",r));return x(t,o,a)}async function x(e,t,r){await a(r);const o=z(t,r),n=Array.isArray(e)?e:[e];return await Promise.all(n.map(o))}async function E(e,a){let o;const{extent:n,rasterFunction:i,url:s,lod:l}=e;try{o=await q(s)}catch(c){if(r(c))return;t.getLogger("esri.layers.orientedImagery.transformations").error("updateElevationUsingElevationSource",c)}switch(o){case 0:return await L(s,n,i,a);case 1:return await I(s,n,l,a);default:return}}async function I(e,a,o,n){const i=new c(e);let s;try{const{tileInfo:e}=await i.load(),t=(o&&e.lodAt(Math.min(e.lods.length-1,o))?.resolution)??"finest-contiguous";s=await i.createElevationSampler(a,{...n,demResolution:t})}catch(l){if(r(l))return;t.getLogger(i).error(l)}finally{i.destroy()}return s}async function L(e,t,r,a){const n=r?new w({functionName:r}):void 0,c=new p({url:e,rasterFunction:n,format:"lerc"});await c.load(a);const u=512,j=t.center,h=Math.max(t.width,t.height);let R=new o({xmin:j.x-h/2,ymin:j.y-h/2,xmax:j.x+h/2,ymax:j.y+h/2,spatialReference:t.spatialReference});c.spatialReference.equals(R.spatialReference)||(await i(),R=s(R,c.spatialReference));const x=await c.fetchPixels(R,u,u,a),E=d.create({scales:[h/u],size:u,spatialReference:R.spatialReference}),I=new v(0,0,0,l(R)),L=new y({values:x.pixelBlock.pixels[0],width:u,height:u,noDataValue:0}),q=[new f(I,L)],z=m.noDataValue;return new g({tiles:q,tileInfo:E,noDataValue:z})}async function q(t,r){await a(r);const o=await j(t),{tileInfo:n,cacheType:i}=o;if(!o.hasOwnProperty("bandCount")||!o.hasOwnProperty("pixelSizeX"))throw new e("elevation-source:invalid-service-url",`ElevationSource URL expects an elevation 3D image service but given ${t}`);return n?"LERC"!==n?.format?.toUpperCase()||i&&"elevation"!==i.toLowerCase()?2:1:0}function z(e,t){return async r=>{let a=r.clone();const o=r.spatialReference.equals(e.spatialReference)?a:await n(r,e.spatialReference,t),i=e.queryElevation(o);if(i){a=r.spatialReference.equals(e.spatialReference)?i.clone():await n(i,r.spatialReference,t)}return a.z=a.z??1,a}}async function S(e,t,r){return await a(r),e.map(e=>(e.z=t,e))}function T(e,t){const{averageGroundElevation:r,spatialReference:a}=t,o=u(a);return e.map(e=>{const t=e.clone();return t.z=r*o/e.spatialReference.metersPerUnit,t})}export{E as getElevationSampler,L as getElevationSamplerFromDynamicImageService,T as updateUsingAverageGroundElevation,S as updateUsingConstant,x as updateUsingElevationSampler,R as updateUsingElevationSource,q as validateElevationSourceURL};
