/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{cyclical2PI as e}from"../../../core/Cyclical.js";import{createAngle as t,valueInUnit as r}from"../../../core/quantity.js";import{subtract as i,dot as n,squaredLength as s,scaleAndAdd as o}from"../../../core/libs/gl-matrix-2/math/vec2.js";import{create as a}from"../../../core/libs/gl-matrix-2/factories/vec2f64.js";import{c,e as p,h as g}from"../../../chunks/vec32.js";import{create as h}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{absoluteHeightElevationInfo as m}from"../../../support/elevationInfoUtils.js";import{VerticalHalfPlaneConstraint as l}from"../sketch/constraints.js";import{fromAnyMapPoint as f,asVec2 as d,markAsTarget as u,fromValues as x,fromVec3 as S}from"../sketch/normalizedPoint.js";import{SnappingAlgorithm as v}from"./SnappingAlgorithm.js";import{isLine as V,squaredScreenDistance as j}from"./snappingUtils.js";import{RightAngleSnappingCandidate as k}from"./candidates/RightAngleSnappingCandidate.js";import{vectorToScreenPoint as C}from"../support/viewUtils.js";import{radiansGeographicBetweenVec as _,pointFromDistanceAlongAzimuth as w}from"../../support/angularMeasurementUtils.js";class y extends v{constructor(e,t,r){super(e,t),this._geodesicLengthMeasurementUtils=r}snapNewVertex(e,t){const r=t.editGeometryOperations.data.parts.at(-1),i=[];if(!r||r.vertices.length<2)return i;const{view:n}=this,s=C(e,t.spatialReference,m,n),o=r.vertices.at(-1);V(o.leftSegment)&&this._checkForSnappingCandidate(1,i,o.leftSegment,o,o.leftSegment.leftVertex,e,s,t);const a=r.vertices[0];return V(a.rightSegment)&&this._checkForSnappingCandidate(2,i,a.rightSegment,a,a.rightSegment.rightVertex,e,s,t),i}snapExistingVertex(e,t){const r=[],i=t.vertexHandle,{part:n}=i;if(n.vertices.length<3)return r;const{view:s}=this,o=C(e,t.spatialReference,m,s),a=i.leftSegment,c=i.rightSegment;if(V(a?.leftVertex.leftSegment)){const i=a.leftVertex.leftSegment;this._checkForSnappingCandidate(3,r,i,i.rightVertex,i.leftVertex,e,o,t)}if(V(c)&&V(c.rightVertex.rightSegment)){const i=c.rightVertex.rightSegment;this._checkForSnappingCandidate(3,r,i,i.leftVertex,i.rightVertex,e,o,t)}return r}_checkForSnappingCandidate(e,t,r,i,n,s,o,a){if(!V(r)||!this.edgeExceedsShortLineThreshold(r,a))return;const c=this.view,p=f(i.pos,c,a),g=f(n.pos,c,a);F(U,g,p,s,a,this._geodesicLengthMeasurementUtils),this._checkForSnappingCandidateAlongProjectedRay(e,t,g,p,U,s,o,a)}_checkForSnappingCandidateAlongProjectedRay(e,t,r,a,p,g,f,v){const{spatialReference:V,pointer:_}=v,w=i(P,d(g),d(a)),y=n(p,w)/s(p),F=o(P,d(a),p,y),R=u(x(F[0],F[1],g[2]));if(j(f,C(R,V,m,this.view))>this.squaredProximityThreshold(_)||this.isVertical(R,a,v)||this.isVertical(a,r,v))return;const M=c(h(),a,p,Math.sign(y));t.push(new k({targetPoint:R,constraint:new l(a,S(M)),previousVertex:r,otherVertex:a,otherVertexType:1,selfSnappingType:e,isDraped:"on-the-ground"===v.elevationInfo?.mode}))}}function F(e,t,r,i,n,s){R(e,t,r,i,n,s)||M(e,t,r)}function R(i,n,s,o,{spatialReference:a},c){const g=_(n,s,a,a);if(null==g)return!1;const m=_(s,o,a,a);if(null==m)return!1;const l=Math.sign(e.shortestSignedDiff(g,m))*Math.PI*.5,f=t(g+l,"radians","geographic"),d=h(),u=c.geodesicDistance(s,o,a);return null!=u&&(w(d,s,a,r(u,"meters"),f,"geodesic"),p(i,d,s),!0)}function M(e,t,r){const n=i(P,d(r),d(t));g(e,n[1],-n[0],0)}const P=a(),U=h();export{y as RightAngleSnapper};
