/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{createScreenPointArray as e,screenPointObjectToArray as n,castScreenPointArray as r}from"../../../../core/screenUtils.js";import{create as t}from"../../../../core/libs/gl-matrix-2/factories/vec2f64.js";import{f as o,e as l,b as s,d as c,g as a,j as i,i as u}from"../../../../chunks/vec32.js";import{create as p}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import m from"../../../../geometry/Point.js";import{projectPoint as d}from"../../../../geometry/projectionUtils.js";import{intersectRay as f,create as g,getNormal as S,fromPositionAndNormal as y}from"../../../../geometry/support/plane.js";import{create as j,closestPoints as E}from"../../../../geometry/support/ray.js";import{projectPoint as v}from"../../../../geometry/support/vector.js";import{sv2d as x}from"../../../../geometry/support/vectorStacks.js";import{getZForElevationMode as w}from"../../../../support/elevationInfoUtils.js";import{fromScreen as b}from"../../support/geometryUtils/ray.js";import{Intersector as I}from"../../webgl-engine/lib/Intersector.js";import{terrainId as C}from"../../webgl-engine/lib/verticalOffsetUtils.js";import{EventPipeline as H}from"../../../interactive/dragEventPipeline.js";function P(e,n){return U(e,()=>n)}function R(e){return U(e,e=>e.plane)}function U(e,t){const o=p(),l=p();let s=!1;return c=>{const a=t(c);if("start"===c.action){const t=n(c.screenStart,r(x.get())),l=b(e.state.camera,t,K);null!=l&&(s=f(a,l,o))}if(!s)return null;const i=n(c.screenEnd,r(x.get())),u=b(e.state.camera,i,K);return null==u?null:f(a,u,l)?{...c,renderStart:o,renderEnd:l,plane:a,ray:u}:null}}function M(n,r,t=0,o=null,l=null){let s=null;return c=>{if("start"===c.action&&(s=n.sceneIntersectionHelper.intersectElevationFromScreen(e(c.screenStart.x,c.screenStart.y),r,t,l),null!=s&&null!=o&&!d(s,s,o)))return null;if(null==s)return null;const a=n.sceneIntersectionHelper.intersectElevationFromScreen(e(c.screenEnd.x,c.screenEnd.y),r,t,l);return null!=a&&(null==o||d(a,a,o))?{...c,mapStart:s,mapEnd:a}:null}}function h(e,n,r,t=null,o=null){return M(e,r,w(n,e,r),t,o)}function k(e,n,r,t){const o=r.toMap(e.screenStart);return null!=o?h(n,o,r.elevationInfo,t):null}function F(e,n){const r=B,t=J,s=g();e.renderCoordsHelper.worldUpAtPosition(n,r);const c=o(S(s),r,l(t,n,e.state.camera.eye));return o(c,c,r),y(n,c,s)}function T(e,n,r){let t=null;const o=new H;return o.next(P(e,F(e,n))).next(A(e,n)).next(q(e,r)).next(e=>{e.mapEnd.x=e.mapStart.x,e.mapEnd.y=e.mapStart.y,t=e}),e=>(t=null,o.execute(e),t)}function A(e,n){const r=p(),t=s(n);e.renderCoordsHelper.worldUpAtPosition(n,r);const o=p(),m=p(),d=o=>{if(l(o,o,n),v(r,o,o),"global"===e.viewingMode){s(o)*Math.sign(a(r,o))<.001-t&&l(o,i(o,r,.001),n)}return u(o,o,n),o};return e=>(e.renderStart=d(c(o,e.renderStart)),e.renderEnd=d(c(m,e.renderEnd)),e)}function D(e,t){const o=o=>{const l=n(o,r(z)),s=b(e.state.camera,l,K);if(null==s)return null;const c=E(t,s,B,J);return c?.[0]};return e=>{const n=o(e.screenStart);if(null==n)return null;const r=o(e.screenEnd);return null==r?null:{...e,renderStart:n,renderEnd:r}}}function q(e,n){const r=e.renderCoordsHelper;return e=>{const t=r.fromRenderCoords(e.renderStart,new m({spatialReference:n})),o=r.fromRenderCoords(e.renderEnd,new m({spatialReference:n}));return null!=t&&null!=o?{...e,mapStart:t,mapEnd:o}:null}}function G(e){let n=null;return r=>{switch(r.action){case"start":n=e.disableDisplay();break;case"end":case"cancel":null!=n&&(n.remove(),n=null)}return r}}function O(r,t=null){const o=new I(r.state.viewingMode);o.options.selectionMode=!0,o.options.store=0,o.options.hud=!1;const l=e(),s={requiresGroundFeedback:!0,enableDraped:!0,exclude:new Set},c=p(),a=t??r.spatialReference,i=e=>{r.map.ground&&r.map.ground.opacity<1?s.exclude.add(C):s.exclude.delete(C),r.sceneIntersectionHelper.intersectIntersectorScreen(n(e,l),o,s);const t=o.results.min;let i;if(t.getIntersectionPoint(c))i=7===t.intersector?0:1;else{if(!o.results.ground.getIntersectionPoint(c))return null;i=0}return{location:r.renderCoordsHelper.fromRenderCoords(c,new m({spatialReference:a})),surfaceType:i}};let u;return e=>{if("start"===e.action){const n=i(e.screenStart);u=null!=n?n.location:null}if(null==u)return null;const n=i(e.screenEnd);return null!=n?.location?{...e,mapStart:u,mapEnd:n.location,surfaceType:n.surfaceType}:null}}const z=t(),B=p(),J=p(),K=j();export{q as convertToMapCoordinates,G as hideManipulatorWhileDragging,A as projectToWorldUp,O as screenToMap3D,h as screenToMapXYAtLocation,k as screenToMapXYForManipulatedObject,P as screenToRenderPlane,R as screenToRenderPlaneFromEvent,D as screenToRenderRay,T as screenToZConstrained};
