/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{floatEqualUlp as e,lerp as t}from"../../../core/mathUtils.js";import{throwIfAborted as n}from"../../../core/promiseUtils.js";import{getMetersPerUnitForSR as r}from"../../../core/units.js";import{distance as s,lerp as o}from"../../../core/libs/gl-matrix-2/math/vec2.js";import{lerp as a}from"../../../core/libs/gl-matrix-2/math/vec3.js";import{geodesicLengths as i,isSupported as c,inverseGeodeticSolver as u,directGeodeticSolver as l}from"../../../geometry/geodesicUtils.js";import f from"../../../geometry/Polyline.js";import{initializeProjection as h,project as p}from"../../../geometry/projectionUtils.js";import m from"../../../geometry/SpatialReference.js";import{getElevationOffset as d}from"../../../support/elevationInfoUtils.js";import{ProfileGenerationError as g}from"./ProfileGenerationError.js";async function w(e,t,s,o,a,c,u){let l,f,d;const w=e.spatialReference,j=w.isGeographic||w.isWebMercator;let v=0;if(!j){const{execute:t}=await import("../../../geometry/operators/lengthOperator.js");n(u),v=t(e,{unit:"meters"}),n(u)}const x=1/r(o);if(j){await h([{source:w,dest:o},{source:w,dest:m.WGS84}],u);const n=D(e);l=i([n],"meters")[0];const r=R(l,a);if(L(e,l,r)>c)throw new g("too-complex");const j=y(n,t,s);({densifiedPath:f,distances:d}=A(j,r,x)),f=p(f,o)}else{await h([{source:w,dest:o}],u),l=v;const r=R(l,a);if(L(e,l,r)>c)throw new g("too-complex");const i=y(e,t,s);({densifiedPath:f,distances:d}=S(i,r,x)),n(u),f=p(f,o)}return{densifiedPath:f,pathLength:l*x,distances:d}}function R(e,t){const n=e/t.densificationMaxSamples;return Math.max(t.samplingDistance,n)}function y(e,t,n){if(null==t)return x(e);const r=e.spatialReference,s=t.mode,o=d(t,r);let a=null;switch(n.type){case"2d":a=j(e,s,o);break;case"3d":a=v(e,s,o,n)}return null==a?x(e):new f({hasZ:!0,hasM:!1,spatialReference:r,paths:P(e.paths,a)})}function j({hasZ:e},t,n){return"absolute-height"===t?e?([e,t,r])=>[e,t,r+n]:([e,t])=>[e,t,n]:null}function v({spatialReference:e,hasZ:t},n,r,{elevationProvider:s}){const o=(t,n,r,o)=>s?.getElevation(t,n,r,e,o)??0;switch(n){case"on-the-ground":return([e,t])=>[e,t,o(e,t,0,"ground")];case"absolute-height":return t?([e,t,n])=>[e,t,n+r]:([e,t])=>[e,t,r];case"relative-to-ground":return t?([e,t,n])=>[e,t,n+o(e,t,n,"ground")+r]:([e,t])=>[e,t,o(e,t,0,"ground")+r];case"relative-to-scene":return t?([e,t,n])=>[e,t,n+o(e,t,n,"scene")+r]:([e,t])=>[e,t,o(e,t,0,"scene")+r];default:return null}}function x(e){return e.hasZ?new f({hasZ:!1,hasM:!1,spatialReference:e.spatialReference,paths:P(e.paths,([e,t])=>[e,t])}):e}function P(e,t){const n=e.length,r=new Array(n);for(let s=0;s<n;++s){const n=e[s],o=n.length,a=new Array(n.length);r[s]=a;for(let e=0;e<o;++e)a[e]=t(n[e])}return r}function Z(e){return M(e)&&e.paths.length>0&&e.paths.every(e=>e.length>=2)}function M(e){return null!=e&&"polyline"===e.type}function b(t,n,s,o,a){const{spatialReference:i,hasZ:c}=t,u={from:null,to:null,distance:0,azimuth:0,reverseAzimuth:0,spatialReference:i,metersPerSR:r(i)},l=[],h=[];let p=0;for(let r=0;r<t.paths.length;++r){const i=t.paths[r],c=new Array,f=new Array;let m=0;for(let t=1;t<i.length;++t){const r=i[t-1],l=i[t],h=o(u,r,l);let d;for(d=m;d<h.distance&&!e(d,h.distance);d+=n)c.push(a(h,d)),f.push((p+d)*s);m=d-h.distance,p+=h.distance,c.push(l),f.push(p*s)}l[r]=c,h[r]=f}return{densifiedPath:new f({spatialReference:i,hasZ:c,paths:l}),distances:h}}function S(e,t,n){const{hasZ:r}=e;return b(e,t,n,z,r?k:E)}function A(e,t,n){const{hasZ:r}=e;return b(e,t,n,U,r?W:G)}function U(e,t,n){return e.distance=0,u(e,t,n,e.spatialReference),e.from=t,e.to=n,e}function z(e,t,n){return e.from=t,e.to=n,e.distance=s(n,t)*e.metersPerSR,e}function G({from:e,azimuth:t,spatialReference:n},r){return l([0,0],e,t,r,n)}function W({from:e,to:n,azimuth:r,distance:s,spatialReference:o},a){const i=a/s,c=[0,0,t(e[2],n[2],i)];return l(c,e,r,a,o),c}function E({from:e,to:t,distance:n},r){return o([0,0],e,t,r/n)}function k({from:e,to:t,distance:n},r){return a([0,0,0],e,t,r/n)}function D(e){return c(e.spatialReference)?e:p(e,m.WGS84)}function I(e){return e.paths.reduce((e,t)=>e+t.length,0)}function L(e,t,n){return I(e)+Math.floor(t/n)+Math.max(0,e.paths.reduce(e=>1+e,0)-1)}export{I as countPoints,w as densifyPath,M as isPolyline,Z as isValidInputPath,y as toAbsoluteHeightElevation};
