/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{createScreenPointArray as e,screenPointObjectToArray as r,castScreenPointArray as n}from"../../../../core/screenUtils.js";import{create as t}from"../../../../core/libs/gl-matrix-2/factories/vec2f64.js";import{cross as o,subtract as l,length as s,copy as c,dot as i,scale as a,add as u}from"../../../../core/libs/gl-matrix-2/math/vec3.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 E,closestPoints as j}from"../../../../geometry/support/ray.js";import{projectPoint as x}from"../../../../geometry/support/vector.js";import{sv2d as v}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,r){return U(e,()=>r)}function R(e){return U(e,e=>e.plane)}function U(e,t){const o=p(),l=p();let s=!1;return c=>{const i=t(c);if("start"===c.action){const t=r(c.screenStart,n(v.get())),l=b(e.state.camera,t,K);null!=l&&(s=f(i,l,o))}if(!s)return null;const a=r(c.screenEnd,n(v.get())),u=b(e.state.camera,a,K);return null==u?null:f(i,u,l)?{...c,renderStart:o,renderEnd:l,plane:i,ray:u}:null}}function M(r,n,t=0,o=null,l=null){let s=null;return c=>{if("start"===c.action&&(s=r.sceneIntersectionHelper.intersectElevationFromScreen(e(c.screenStart.x,c.screenStart.y),n,t,l),null!=s&&null!=o&&!d(s,s,o)))return null;if(null==s)return null;const i=r.sceneIntersectionHelper.intersectElevationFromScreen(e(c.screenEnd.x,c.screenEnd.y),n,t,l);return null!=i&&(null==o||d(i,i,o))?{...c,mapStart:s,mapEnd:i}:null}}function h(e,r,n,t=null,o=null){return M(e,n,w(r,e,n),t,o)}function k(e,r,n,t){const o=n.toMap(e.screenStart);return null!=o?h(r,o,n.elevationInfo,t):null}function F(e,r){const n=B,t=J,s=g();e.renderCoordsHelper.worldUpAtPosition(r,n);const c=o(S(s),n,l(t,r,e.state.camera.eye));return o(c,c,n),y(r,c,s)}function T(e,r,n){let t=null;const o=new H;return o.next(P(e,F(e,r))).next(A(e,r)).next(q(e,n)).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,r){const n=p(),t=s(r);e.renderCoordsHelper.worldUpAtPosition(r,n);const o=p(),m=p(),d=o=>{if(l(o,o,r),x(n,o,o),"global"===e.viewingMode){s(o)*Math.sign(i(n,o))<.001-t&&l(o,a(o,n,.001),r)}return u(o,o,r),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=r(o,n(z)),s=b(e.state.camera,l,K);if(null==s)return null;const c=j(t,s,B,J);return c?.[0]};return e=>{const r=o(e.screenStart);if(null==r)return null;const n=o(e.screenEnd);return null==n?null:{...e,renderStart:r,renderEnd:n}}}function q(e,r){const n=e.renderCoordsHelper;return e=>{const t=n.fromRenderCoords(e.renderStart,new m({spatialReference:r})),o=n.fromRenderCoords(e.renderEnd,new m({spatialReference:r}));return null!=t&&null!=o?{...e,mapStart:t,mapEnd:o}:null}}function G(e){let r=null;return n=>{switch(n.action){case"start":r=e.disableDisplay();break;case"end":case"cancel":null!=r&&(r.remove(),r=null)}return n}}function O(n,t=null){const o=new I(n.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(),i=t??n.spatialReference,a=e=>{n.map.ground&&n.map.ground.opacity<1?s.exclude.add(C):s.exclude.delete(C),n.sceneIntersectionHelper.intersectIntersectorScreen(r(e,l),o,s);const t=o.results.min;let a;if(t.getIntersectionPoint(c))a=7===t.intersector?0:1;else{if(!o.results.ground.getIntersectionPoint(c))return null;a=0}return{location:n.renderCoordsHelper.fromRenderCoords(c,new m({spatialReference:i})),surfaceType:a}};let u;return e=>{if("start"===e.action){const r=a(e.screenStart);u=null!=r?r.location:null}if(null==u)return null;const r=a(e.screenEnd);return null!=r?.location?{...e,mapStart:u,mapEnd:r.location,surfaceType:r.surfaceType}:null}}const z=t(),B=p(),J=p(),K=E();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};
