/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{valueInUnit as e,createAngle as n,createLength as t}from"../../../core/quantity.js";import{lengthUnitFromSpatialReference as r,verticalLengthUnitFromSpatialReference as o}from"../../../core/units.js";import{subtract as i,copy as l,add as u}from"../../../core/libs/gl-matrix-2/math/vec3.js";import{geodesicCompatibleSpatialReference as c}from"../../../geometry/geodesicUtils.js";import s from"../../../geometry/SpatialReference.js";import{projectVectorToVector as a}from"../../../geometry/projection/projectVectorToVector.js";import{makeDehydratedPoint as f}from"../../../layers/graphics/dehydratedPoint.js";import{getConvertedElevationFromXYZ as m}from"../../../support/elevationInfoUtils.js";import{CoordinateConstraint as d,VerticalCylinderConstraint as v,constraintOrSet as p,HorizontalPlaneConstraint as g,VerticalHalfPlaneConstraint as R,GeodesicConstraint as w,PointConstraint as j}from"./constraints.js";import{fromPoint as x,createWritable as y,toElevationAlignedDehydratedPoint as h}from"./normalizedPoint.js";import{getDegreesGeographic as S,directionBetweenPoints as b,pointFromDistanceAlongAzimuth as I}from"../../support/angularMeasurementUtils.js";function M(e,n,t,r,o,i){let l="geodesic",u=c(t);const s=y();return x(e,n,r,s),s[2]=0,u&&a(s,t,s,u)||(l="euclidean",u=t),{mode:l,view:n,elevationInfo:r,hasZ:o,directionMode:i,spatialReference:e.spatialReference,measurementSR:u,origin:s}}function U(n,t,o){if(null==t||null==n)return;const i=r(o.measurementSR);if(null==i)return;const l=k(n,o);if(null==l)return;const u=e(t,i);return new v(l,u)}function Z(e,t,r,o){if(null==r||null==e)return;const i=k(e,o);if(null==i)return;const l=S(r),u=10,c=e=>{if(null==e)return;const t=y(),r=n(e,"degrees","geographic");return I(t,i,o.measurementSR,u,r,o.mode)?new R(i,t):void 0},s=()=>{if(null!=t&&null!=e)return S(b(t,e))};switch(o.directionMode){case"absolute":return c(l);case"relative":{const e=s();if(null==e)return;return c(e+l)}case"relative-bilateral":{const e=s();if(null==e)return;return p([c(e+l),c(e-l)])}}}function P(e,n){const t=B(e,n);return null!=t?new g(t):void 0}function T(e,n,t){const{context:r,longitude:o,latitude:i,direction:l,distance:u,elevation:c}=t;if(null!=o||null!=i||null!=u||null!=c||null!=l){if(null!=o||null!=i){const e=S(o),n=S(i),t=B(c,r);return new d(e,n,t)}return V(e,n,t)}}function V(n,t,{context:r,direction:o,distance:i,elevation:l}){if(null==t)return P(l,r);const{view:u,elevationInfo:c,measurementSR:s}=r,f=x(t,u,c);if(!s||!a(f,t.spatialReference,D,s))return;const[m,d]=D,v=null!=i?e(i,"meters"):void 0,g=S(o),R=B(l,r),y=e=>{const n=new w([m,d],s,v,R,e);return null==v||null==e||null==R&&r.hasZ?n:new j(n.closestTo(f))};if(null==g)return y(void 0);const h=()=>{if(null!=n&&null!=t)return S(b(n,t))};switch(r.directionMode){case"absolute":return y(g);case"relative":{const e=h();if(null==e)return;return y(e+g)}case"relative-bilateral":{const e=h();if(null==e)return;return p([y(e+g),y(e-g)])}}}function q(e){return"geodesic"===e.context.mode?T(null,null,e):G(e)}function z(e,n,t){const{context:r,x:o,y:i,distance:l,direction:u,elevation:c}=t;return"geodesic"===r.mode?T(n,e,t):null!=o||null!=i?G(t):W([U(e,l,r),Z(e,n,u,r),P(c,r)])}function G({x:e,y:n,elevation:t,context:r}){H.x=e?.value??0,H.y=n?.value??0,H.spatialReference=r.spatialReference;const o=k(H,r,E);return new d(null!=e&&null!=o?o[0]:void 0,null!=n&&null!=o?o[1]:void 0,B(t,r))}function W(e){let n;for(const t of e)t&&(n=n?.intersect(t)??t);return n}function k(e,n,t=y()){const{view:r,elevationInfo:o,measurementSR:l,origin:u,mode:c}=n;if(x(e,r,o,t),a(t,e.spatialReference,t,l))return"geodesic"!==c&&i(t,t,u),t}function A(e,n,t,r){const{view:o,measurementSR:i,spatialReference:c,origin:s,mode:f}=t;if("geodesic"===f?l(F,e):u(F,e,s),a(F,i,F,c))return h(F,o,n,t,r)}function B(e,n){return C(e,n)?.value??void 0}function C(n,{view:r,origin:i,elevationInfo:l,hasZ:u,measurementSR:c}){if(null==n||!u)return;const s=o(c);if(null==s)return;const[a,f]=i,d=e(n,s),v="3d"===r?.type?m(r,a,f,d,c,l):d;return null!=v?t(v,s):void 0}const D=y(),E=y(),F=y(),H=f(0,0,0,s.WGS84);export{A as constraintSpaceToPoint,M as getConstraintContext,T as getGeodesicConstraint,q as getPointConstraint,z as getPolylineOrPolygonConstraint,W as intersectAll,k as pointToConstraintSpace};
