/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
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{scaleAndAdd as c,subtract as g,set as p}from"../../../core/libs/gl-matrix-2/math/vec3.js";import{create as h}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{absoluteHeightElevationInfo as l}from"../../../support/elevationInfoUtils.js";import{VerticalHalfPlaneConstraint as m}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 F}from"../../support/angularMeasurementUtils.js";class w 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,l,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,l,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)}const g=n.getFirstVertex()?.rightSegment;return!c&&v(g)&&this._checkForSnappingCandidate(3,r,g,g.leftVertex,g.rightVertex,e,o,t),r}_checkForSnappingCandidate(e,t,r,i,n,s,o,a){if(!v(r)||!this.edgeExceedsShortLineThreshold(r,a))return;const c=this.view,g=f(i.pos,c,a),p=f(n.pos,c,a);y(P,p,g,s,a,this._geodesicLengthMeasurementUtils),this._checkForSnappingCandidateAlongProjectedRay(e,t,p,g,P,s,o,a)}_checkForSnappingCandidateAlongProjectedRay(e,t,r,a,g,p,f,V){const{spatialReference:v,pointer:_}=V,F=i(M,d(p),d(a)),w=n(g,F)/s(g),y=o(M,d(a),g,w),R=u(x(y[0],y[1],p[2]));if(j(f,C(R,v,l,this.view))>this.squaredProximityThreshold(_)||this.isVertical(R,a,V)||this.isVertical(a,r,V))return;const A=c(h(),a,g,Math.sign(w));t.push(new k({targetPoint:R,constraint:new m(a,S(A)),previousVertex:r,otherVertex:a,otherVertexType:1,selfSnappingType:e,isDraped:"on-the-ground"===V.elevationInfo?.mode}))}}function y(e,t,r,i,n,s){R(e,t,r,i,n,s)||A(e,t,r)}function R(i,n,s,o,{spatialReference:a},c){const p=_(n,s,a,a);if(null==p)return!1;const l=_(s,o,a,a);if(null==l)return!1;const m=Math.sign(e.shortestSignedDiff(p,l))*Math.PI*.5,f=t(p+m,"radians","geographic"),d=h(),u=c.geodesicDistance(s,o,a);return null!=u&&(F(d,s,a,r(u,"meters"),f,"geodesic"),g(i,d,s),!0)}function A(e,t,r){const n=i(M,d(r),d(t));p(e,n[1],-n[0],0)}const M=a(),P=h();export{w as RightAngleSnapper};
