/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{copy as e,subtract as r,add as t,distance as n,direction as o}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as i}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{wrap as c}from"../../../../geometry/support/ray.js";import{Sphere as s}from"../../../../geometry/support/sphere.js";import{defaultConstraintOptions as a,interactionDirectionTowardsConstraintMinimization as f,adjustRangeForInteraction as m}from"./common.js";function u(e,r,t=a){if(!e.state.isLocal)return 0;const n=e.state.constraints.distance;if(!e.pointsOfInterest.surfaceOrigin.renderLocation||n===1/0)return 0;g.min=0,g.max=n,d(e,t,g);const o=y(e,r),i=g.max-o;return i>=-1e-6?0:i}function p(n,o,i=a){const m=u(n,o,i);if(0===m)return!1;const p=n.pointsOfInterest.surfaceOrigin;if(!p.renderLocation)return!1;const d=y(n,o)+m,g=e(x,o.eye),C=f(o,i.interactionDirection,l(o,p.renderLocation,j),O);if(!new s(p.renderLocation,d).intersectRay(c(o.eye,C),I))return!1;o.eye=I;const b=r(L,o.eye,g);o.center=t(I,o.center,b);const h=n.renderCoordsHelper.getAltitude(o.center),v=n.renderCoordsHelper.intersectInfiniteManifold(o.ray,h,I);return null!=v&&(o.center=v),!0}function d(e,r,t){const n=r.interactionType;if(0===n)return;const{min:o,max:i}=t,{interactionStartCamera:c,interactionFactor:s}=r;if(!c)return;const a=1===n||4===n,f=u(e,c),p=0===f?0:y(e,c);t.min=o,t.max=i;m(f,p,a,s,.05*p,t)}function y(e,r){const t=e.pointsOfInterest.surfaceOrigin;return t.renderLocation?n(r.eye,t.renderLocation):0}function l(e,r,t){return o(t,e.eye,r)}const g={min:0,max:0},x=i(),L=i(),O=i(),j=i(),I=i();export{p as applyDistanceConstraint,u as getDistanceConstraintError};
