/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{destroyHandle as e}from"../../../../core/handleUtils.js";import{watch as n,sync as t,initial as r}from"../../../../core/reactiveUtils.js";import{createScreenPointArray as o}from"../../../../core/screenUtils.js";import{clone as i,create as a}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import s from"../../../../geometry/Point.js";import{pointEquals as l}from"../../../../layers/graphics/dehydratedFeatureComparison.js";import{makeDehydratedPoint as c}from"../../../../layers/graphics/dehydratedPoint.js";import{absoluteHeightElevationInfo as p}from"../../../../support/elevationInfoUtils.js";import{ViewshedDrawToolRenderer as d}from"./ViewshedDrawToolRenderer.js";import{Intersector as m}from"../../webgl-engine/lib/Intersector.js";import{toGraphic as u}from"../../webgl-engine/lib/intersectorUtilsConversions.js";import{DrawTool as f}from"../../../draw/DrawTool.js";import{acquire as g}from"../../../interactive/snapping/SnappingManagerPool.js";function j(o){const{analysisViewData:i,view:a,sketchOptions:s,onCancel:l,onComplete:c,onManipulatorCountChange:u}=o,j=new d({view:a}),P=g(a),w=new m(a.state.viewingMode),b=new f({elevationInfo:p,enableVertexVisuals:!1,hasZ:!0,renderer:j,sketchOptions:s,snappingManager:P.snappingManager,view:a});return j.addSphereManipulator(b),b.addHandles([P,e(j),b.on("cancel",()=>l(b)),b.on("complete",()=>c(b)),n(()=>b.manipulators.length,()=>u(b),t),n(()=>i.visible,e=>j.analysisVisible=e,r)]),{drawTool:b,intersector:w,renderer:j}}function P({drawTool:e,intersector:n,mapPoint:t,view:r}){const{interactiveHandleManager:o,lastScreenPosition:s}=e,l=e.getVertexSnappingCandidates(),c=a();if(!r.renderCoordsHelper.toRenderCoords(t,c))return null;const p=o?.capturingHandle??o?.allhandles?.at(0),d=p?.screenPosition??s,m=null!=p?w({handle:p,spatialReference:r.spatialReference,vertexSnappingCandidates:l}):null,u=null!=d?v(d,r,n):null;return{mapPoint:(u?.mapPoint??t).clone(),scenePoint:u?.scenePoint?i(u.scenePoint):c,feature:null!=m?h(m):u?.feature}}function w({handle:e,spatialReference:n,vertexSnappingCandidates:t}){const r=e.selectedSnappingResult;if(!r)return null;const o=r.spatialReference??n;return t.find(({targetPoint:e})=>l(r,c(e.x,e.y,e.z,o)))}const b={mapPoint:new s,scenePoint:a(),feature:null};function v(e,n,t){n.sceneIntersectionHelper.intersectToolIntersectorScreen(o(e.x,e.y),t);const r=b,i=t.results.min;return i.getIntersectionPoint(r.scenePoint)?(r.mapPoint.spatialReference=n.spatialReference,n.renderCoordsHelper.fromRenderCoords(r.scenePoint,r.mapPoint),r.feature=u(i,n),r):null}function h(e){const n={id:e.layer.id,objectIdField:"objectIdField"in e.layer?e.layer.objectIdField:void 0};return{attributes:{[n.objectIdField??"ObjectId"]:e.objectId},layer:n,objectId:e.objectId,uid:null}}export{j as createDrawTool,P as createPlacementResult};
