/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
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{e as i,d as l,i as u}from"../../../chunks/vec32.js";import{geodesicCompatibleSpatialReference as s}from"../../../geometry/geodesicUtils.js";import c 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{createWritable as y,fromPoint as h,toElevationAlignedDehydratedPoint as x}from"./normalizedPoint.js";import{getDegreesGeographic as S,directionBetweenPoints as I,pointFromDistanceAlongAzimuth as b}from"../../support/angularMeasurementUtils.js";function M(e,n,t,r,o,i){let l="geodesic",u=s(t);const c=y();return h(e,n,r,c),c[2]=0,u&&a(c,t,c,u)||(l="euclidean",u=t),{mode:l,view:n,elevationInfo:r,hasZ:o,directionMode:i,spatialReference:e.spatialReference,measurementSR:u,origin:c}}function U(n,t,o){if(null==t||null==n)return;const i=r(o.measurementSR);if(null==i)return;const l=W(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=W(e,o);if(null==i)return;const l=S(r),u=10,s=e=>{if(null==e)return;const t=y(),r=n(e,"degrees","geographic");return b(t,i,o.measurementSR,u,r,o.mode)?new R(i,t):void 0},c=()=>{if(null!=t&&null!=e)return S(I(t,e))};switch(o.directionMode){case"absolute":return s(l);case"relative":{const e=c();if(null==e)return;return s(e+l)}case"relative-bilateral":{const e=c();if(null==e)return;return p([s(e+l),s(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:s}=t;if(null!=o||null!=i||null!=u||null!=s||null!=l){if(null!=o||null!=i){const e=S(o),n=S(i),t=B(s,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:s,measurementSR:c}=r,f=h(t,u,s);if(!c||!a(f,t.spatialReference,D,c))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],c,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 x=()=>{if(null!=n&&null!=t)return S(I(n,t))};switch(r.directionMode){case"absolute":return y(g);case"relative":{const e=x();if(null==e)return;return y(e+g)}case"relative-bilateral":{const e=x();if(null==e)return;return p([y(e+g),y(e-g)])}}}function k(e){return"geodesic"===e.context.mode?T(null,null,e):z(e)}function q(e,n,t){const{context:r,x:o,y:i,distance:l,direction:u,elevation:s}=t;return"geodesic"===r.mode?T(n,e,t):null!=o||null!=i?z(t):G([U(e,l,r),Z(e,n,u,r),P(s,r)])}function z({x:e,y:n,elevation:t,context:r}){H.x=e?.value??0,H.y=n?.value??0,H.spatialReference=r.spatialReference;const o=W(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 G(e){let n;for(const t of e)t&&(n=n?.intersect(t)??t);return n}function W(e,n,t=y()){const{view:r,elevationInfo:o,measurementSR:l,origin:u,mode:s}=n;if(h(e,r,o,t),a(t,e.spatialReference,t,l))return"geodesic"!==s&&i(t,t,u),t}function A(e,n,t,r){const{view:o,measurementSR:i,spatialReference:s,origin:c,mode:f}=t;if("geodesic"===f?l(F,e):u(F,e,c),a(F,i,F,s))return x(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:s}){if(null==n||!u)return;const c=o(s);if(null==c)return;const[a,f]=i,d=e(n,c),v="3d"===r?.type?m(r,a,f,d,s,l):d;return null!=v?t(v,c):void 0}const D=y(),E=y(),F=y(),H=f(0,0,0,c.WGS84);export{A as constraintSpaceToPoint,M as getConstraintContext,T as getGeodesicConstraint,k as getPointConstraint,q as getPolylineOrPolygonConstraint,G as intersectAll,W as pointToConstraintSpace};
