/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["require","exports","../../../chunks/_rollupPluginBabelHelpers","../../../core/mathUtils","../../../core/maybe","../../../core/promiseUtils","../../../core/unitUtils","../../../chunks/vec2","../../../chunks/vec3","../../../geometry/Polyline","../../../geometry/projection","../../../geometry/SpatialReference","../../../geometry/support/geodesicUtils","./ProfileGenerationError"],(function(e,t,n,r,o,i,s,a,c,l,u,h,f,p){"use strict";function d(e,t,n,r,o,i,s){return g.apply(this,arguments)}function g(){return(g=n._asyncToGenerator((function*(t,n,r,o,a,c,l){let d,g,P;const w=t.spatialReference,R=w.isGeographic||w.isWebMercator,v=yield new Promise(((t,n)=>e(["../../../geometry/geometryEngineAsync"],t,n)));i.throwIfAborted(l);let S=0;R||(S=yield v.planarLength(t,"meters"),i.throwIfAborted(l));const b=1/s.getMetersPerUnitForSR(o);if(R||S>=a.geodesicDistanceThreshold){yield u.initializeProjection([{source:w,dest:o},{source:w,dest:h.WGS84}],l);const e=T(t);d=f.geodesicLengths([e],"meters")[0];const i=m(d,a);if(C(t,d,i)>c)throw new p.ProfileGenerationError(p.ProfileGenerationErrorCause.TooComplex);const s=y(e,n,r);({densifiedPath:g,distances:P}=M(s,i,b)),g=u.project(g,o)}else{yield u.initializeProjection([{source:w,dest:o}],l),d=S;const e=m(d,a);if(C(t,d,e)>c)throw new p.ProfileGenerationError(p.ProfileGenerationErrorCause.TooComplex);const s=y(t,n,r);({densifiedPath:g,distances:P}=G(s,e,b)),i.throwIfAborted(l),g=u.project(g,o)}return{densifiedPath:g,pathLength:d*b,distances:P}}))).apply(this,arguments)}function m(e,t){const n=e/t.densificationMaxSamples;return Math.max(t.samplingDistance,n)}function y(e,t,n){if(o.isNone(t))return R(e);const r=e.spatialReference,i=t.mode,s=o.unwrapOr(t.offset,0);let a=null;switch(n.type){case"2d":a=P(e,i,s);break;case"3d":a=w(e,i,s,n)}return o.isNone(a)?R(e):new l({hasZ:!0,hasM:!1,spatialReference:r,paths:v(e.paths,a)})}function P({hasZ:e},t,n){return"absolute-height"===t?e?([e,t,r])=>[e,t,r+n]:([e,t])=>[e,t,n]:null}function w({spatialReference:e,hasZ:t},n,r,{elevationProvider:i}){const s=(t,n,r,s)=>o.unwrapOr(i.getElevation(t,n,r,e,s),0);switch(n){case"on-the-ground":return([e,t])=>[e,t,s(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+s(e,t,n,"ground")+r]:([e,t])=>[e,t,s(e,t,0,"ground")+r];case"relative-to-scene":return t?([e,t,n])=>[e,t,n+s(e,t,n,"scene")+r]:([e,t])=>[e,t,s(e,t,0,"scene")+r];default:return null}}function R(e){return e.hasZ?new l({hasZ:!1,hasM:!1,spatialReference:e.spatialReference,paths:v(e.paths,(([e,t])=>[e,t]))}):e}function v(e,t){const n=e.length,r=new Array(n);for(let o=0;o<n;++o){const n=e[o],i=n.length,s=new Array(n.length);r[o]=s;for(let e=0;e<i;++e)s[e]=t(n[e])}return r}function S(e){return b(e)&&e.paths.length>0&&e.paths.every((e=>e.length>=2))}function b(e){return o.isSome(e)&&"polyline"===e.type}function A(e,t,n,r,o){const{spatialReference:i,hasZ:a}=e,c={from:null,to:null,distance:0,azimuth:0,reverseAzimuth:0,spatialReference:i,metersPerSR:s.getMetersPerUnitForSR(i)},u=new Array(e.paths.length),h=new Array(e.paths.length);let f=0;for(let s=0;s<e.paths.length;++s){const i=e.paths[s],a=new Array,l=new Array;let p=0;for(let e=1;e<i.length;++e){const s=i[e-1],u=i[e],h=r(c,s,u);let d;for(d=p;d<h.distance;d+=t)a.push(o(h,d)),l.push((f+d)*n);p=d-h.distance,f+=h.distance,a.push(u),l.push(f*n)}u[s]=a,h[s]=l}return{densifiedPath:new l({spatialReference:i,hasZ:a,paths:u}),distances:h}}function G(e,t,n){const{hasZ:r}=e;return A(e,t,n,E,r?U:z)}function M(e,t,n){const{hasZ:r}=e;return A(e,t,n,Z,r?x:j)}function Z(e,t,n){return e.distance=0,f.inverseGeodeticSolver(e,t,n,e.spatialReference),e.from=t,e.to=n,e}function E(e,t,n){return e.from=t,e.to=n,e.distance=a.distance(n,t)*e.metersPerSR,e}function j({from:e,azimuth:t,spatialReference:n},r){return f.directGeodeticSolver([0,0],e,t,r,n)}function x({from:e,to:t,azimuth:n,distance:o,spatialReference:i},s){const a=s/o,c=[0,0,r.lerp(e[2],t[2],a)];return f.directGeodeticSolver(c,e,n,s,i),c}function z({from:e,to:t,distance:n},r){return a.lerp([0,0],e,t,r/n)}function U({from:e,to:t,distance:n},r){return c.lerp([0,0,0],e,t,r/n)}function T(e){return f.isSupported(e.spatialReference)?e:u.project(e,h.WGS84)}function k(e){return e.paths.reduce(((e,t)=>e+t.length),0)}function C(e,t,n){return k(e)+Math.floor(t/n)+Math.max(0,e.paths.reduce((e=>1+e),0)-1)}t.countPoints=k,t.densifyPath=d,t.isPolyline=b,t.isValidInputPath=S,t.toAbsoluteHeightElevation=y,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
