/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{equals as e}from"../../../../../core/arrayUtils.js";import{fromArray as t}from"../../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{getHeightUnitCorrectionFactor as s,interpolateM as n}from"../../../../geometry/coordUtils.js";import{segmentLength3d as a,distanceAlongLineSegmentWithZ as r,distanceAlongLineSegment as o}from"../../../../geometry/lineUtils.js";import c from"../../../../../geometry/Point.js";import h from"../../../../../geometry/Polyline.js";import{getLength as i}from"../../../../../geometry/support/coordsUtils.js";import{getPaths as f,getEndpoint as l}from"../../../../../geometry/support/curves/curveUtils.js";function p(e){let t=0;const n=s(e.spatialReference);for(let s=0;s<e.paths.length;s++){const r=e.paths[s].length;for(let o=0;o<r;o++)o>0&&(t+=e.hasZ?a(e.paths[s][o-1],e.paths[s][o],n):i(e.paths[s][o-1],e.paths[s][o]))}return t}function u(e){const t=e.geometry.clone();return t.paths.map(s=>new h({spatialReference:e.geometry.spatialReference,paths:[s],hasZ:t.hasZ,hasM:t.hasM}))}function m(e){const t=[];for(const s of f(e))for(let n=1;n<s.length;n++){const a=[[l(s[n-1]).slice(),l(s[n]).slice()]];t.push(new h({paths:a,spatialReference:e.spatialReference,hasZ:e.hasZ,hasM:e.hasM}))}return t}function y(e,t,s){const n=t[0],a=t[1],r=i(n,a),o=e.x+s*((t[1][0]-t[0][0])/r),h=e.y+s*((t[1][1]-t[0][1])/r),f={};return e.hasM&&(f.m=e.m),new c({x:o,y:h,spatialReference:e.spatialReference,...f})}function R(e,t,s,n){const r=t[0],o=t[1],h=a(r,o,n),i=e.x+s*((t[1][0]-t[0][0])/h),f=e.y+s*((t[1][1]-t[0][1])/h),l=e.z+s*((t[1][2]-t[0][2])/h),p={};return e.hasM&&(p.m=e.m),new c({x:i,y:f,z:l,spatialReference:e.spatialReference,...p})}function g(e,t,s){const n=t[0],a=t[1],r=[a[0]-n[0],a[1]-n[1]],o=Math.sqrt(r[0]**2+r[1]**2),c=[r[0]/o,r[1]/o];return e.x+=s*c[0],e.y+=s*c[1],e}function M(e,t,s){let a=0,r=null;if(t<=0){const n=[e.paths[0][0],e.paths[0][1]];let a=w(e,0,0);return a=y(a,n,t),g(a,n,s)}const h=e.hasM?e.hasZ?3:2:-1;for(const m of e.paths)for(let f=0;f<m.length-1;f++){const l=i(m[f],m[f+1]),p=a+l;if(p===t)return r=new c({x:m[f+1][0],y:m[f+1][1],...e.hasM?{m:m[f+1][h]}:{},spatialReference:e.spatialReference}),0===s?r:g(r,[m[f],m[f+1]],s);if(p>t){const i=o(m[f],m[f+1],t-a);return r=new c({x:i[0],y:i[1],...h>-1?{m:n(m[f][h],m[f+1][h],l,t-a)??void 0}:{},spatialReference:e.spatialReference}),0===s?r:g(r,[m[f],m[f+1]],s)}a+=l}if(0===a)return null;const f=e.paths.length-1,l=e.paths[f].length-1,p=[e.paths[f][l-1],e.paths[f][l]];let u=w(e,f,l);return u=y(u,p,t-a),g(u,p,s)}function Z(e,s,o,h){let i=0;if(s<=0){const t=[e.paths[0][0],e.paths[0][1]];let n=w(e,0,0);return n=R(n,t,s,h),g(n,t,o)}const f=e.hasM?e.hasZ?3:2:-1;for(const y of e.paths)for(let l=0;l<y.length-1;l++){const p=a(y[l],y[l+1],h),u=i+p;if(u===s){const t=new c({x:y[l+1][0],y:y[l+1][1],z:y[l+1][2],...e.hasM?{m:y[l+1][f]}:{},hasZ:!0,spatialReference:e.spatialReference});return 0===o?t:g(t,[y[l],y[l+1]],o)}if(u>s){const a=r(t(y[l]),t(y[l+1]),s-i,h),u=new c({x:a[0],y:a[1],z:a[2],...f>-1?{m:n(y[l][f],y[l+1][f],p,s-i)}:{},hasZ:!0,spatialReference:e.spatialReference});return 0===o?u:g(u,[y[l],y[l+1]],o)}i+=p}if(0===i)return null;const l=e.paths.length-1,p=e.paths[l].length-1,u=[e.paths[l][p-1],e.paths[l][p]];let m=w(e,l,p);return m=R(m,u,s-i,h),g(m,u,o)}function x(e,t,s){const n=l(f(e)[t][s]);return new c({spatialReference:e.spatialReference,x:n[0],y:n[1],hasZ:e.hasZ,hasM:e.hasM,z:e.hasZ?n[2]:void 0,m:e.hasM?e.hasZ?n[3]:n[2]:void 0})}function w(e,t,s){const n=l(f(e)[t][s]);return new c({spatialReference:e.spatialReference,x:n[0],y:n[1],hasZ:e.hasZ,hasM:e.hasM,z:e.hasZ?n[2]:void 0,m:e.hasM?e.hasZ?n[3]:n[2]:void 0})}function j(t){return e(l(t.at(0)),l(t.at(-1)))}export{p as computedTotalLength,u as convertRadialToPolylines,m as convertToTwoPoint,g as offsetPointFromSegment,M as pointAlongLineAndOffset,Z as pointAlongLineWithZAndOffset,x as pointFromPolygonVertex,w as pointFromPolylineVertex,j as sameStartEndPoint};
