/* COPYRIGHT Esri - https://js.arcgis.com/5.0/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 i}from"../../../core/libs/gl-matrix-2/math/vec3.js";import{geodesicLengths as a,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 m}from"../../../geometry/projectionUtils.js";import p from"../../../geometry/SpatialReference.js";import{getElevationOffset as d}from"../../../support/elevationInfoUtils.js";import{TooComplexError as g}from"./elevationProfileErrors.js";async function y(e,t,s,o,i,c,u){let l,f,d;const y=e.spatialReference,j=y.isGeographic||y.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 P=1/r(o);if(j){await h([{source:y,dest:o},{source:y,dest:p.WGS84}],u);const n=q(e);l=a([n],"meters")[0];const r=w(l,i);if(C(e,l,r)>c)throw new g;const j=R(n,t,s);({densifiedPath:f,distances:d}=W(j,r,P)),f=m(f,o)}else{await h([{source:y,dest:o}],u),l=v;const r=w(l,i);if(C(e,l,r)>c)throw new g;const a=R(e,t,s);({densifiedPath:f,distances:d}=G(a,r,P)),n(u),f=m(f,o)}return{densifiedPath:f,pathLength:l*P,distances:d}}function w(e,t){const n=e/t.densificationMaxSamples;return Math.max(t.samplingDistance,n)}function R(e,t,n){if(null==t)return P(e);const r=e.spatialReference,s=t.mode,o=d(t,r);let i=null;switch(n.type){case"2d":i=j(e,s,o);break;case"3d":i=v(e,s,o,n)}return null==i?P(e):new f({hasZ:!0,hasM:!1,spatialReference:r,paths:Z(e.paths,i)})}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 P(e){return e.hasZ?new f({hasZ:!1,hasM:!1,spatialReference:e.spatialReference,paths:Z(e.paths,([e,t])=>[e,t])}):e}function Z(e,t){const n=e.length,r=new Array(n);for(let s=0;s<n;++s){const n=e[s],o=n.length,i=new Array(n.length);r[s]=i;for(let e=0;e<o;++e)i[e]=t(n[e])}return r}function x(e){return S(e)&&e.paths.some(e=>e.length>1)}function M(e){return b(e)&&x(e.geometry)}function b(e){return A(e)&&S(e?.geometry)}function S(e){return!A(e)&&null!=e&&"polyline"===e.type}function A(e){return null!=e&&"geometry"in e}function U(e){const t=e?.layer;return null!=t&&"graphics"===t.type&&t.internal}function z(t,n,s,o,i){const{spatialReference:a,hasZ:c}=t,u={from:null,to:null,distance:0,azimuth:0,reverseAzimuth:0,spatialReference:a,metersPerSR:r(a)},l=[],h=[];let m=0;for(let r=0;r<t.paths.length;++r){const a=t.paths[r],c=new Array,f=new Array;let p=0;for(let t=1;t<a.length;++t){const r=a[t-1],l=a[t],h=o(u,r,l);let d;for(d=p;d<h.distance&&!e(d,h.distance);d+=n)c.push(i(h,d)),f.push((m+d)*s);p=d-h.distance,m+=h.distance,c.push(l),f.push(m*s)}l[r]=c,h[r]=f}return{densifiedPath:new f({spatialReference:a,hasZ:c,paths:l}),distances:h}}function G(e,t,n){const{hasZ:r}=e;return z(e,t,n,k,r?O:L)}function W(e,t,n){const{hasZ:r}=e;return z(e,t,n,E,r?I:D)}function E(e,t,n){return e.distance=0,u(e,t,n,e.spatialReference),e.from=t,e.to=n,e}function k(e,t,n){return e.from=t,e.to=n,e.distance=s(n,t)*e.metersPerSR,e}function D({from:e,azimuth:t,spatialReference:n},r){return l([0,0],e,t,r,n)}function I({from:e,to:n,azimuth:r,distance:s,spatialReference:o},i){const a=i/s,c=[0,0,t(e[2],n[2],a)];return l(c,e,r,i,o),c}function L({from:e,to:t,distance:n},r){return o([0,0],e,t,r/n)}function O({from:e,to:t,distance:n},r){return i([0,0,0],e,t,r/n)}function q(e){return c(e.spatialReference)?e:m(e,p.WGS84)}function B(e){return e.paths.reduce((e,t)=>e+t.length,0)}function C(e,t,n){return B(e)+Math.floor(t/n)+Math.max(0,e.paths.reduce(e=>1+e,0)-1)}export{B as countPoints,y as densifyPath,A as isGraphic,U as isInternalGraphic,S as isPolyline,b as isPolylineGraphic,M as isValidInputGraphic,x as isValidInputPath,R as toAbsoluteHeightElevation};
