/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{handlesGroup as t}from"../../../../../core/handleUtils.js";import{scale as e,zeroMeters as o}from"../../../../../core/quantity.js";import{watch as n,syncAndInitial as i,sync as a}from"../../../../../core/reactiveUtils.js";import{ensureType as s}from"../../../../../core/accessorSupport/ensureType.js";import r from"../../../../../geometry/Point.js";import{manipulatedObjectGeometry as l}from"../manipulatedObjectUtils.js";import{updateXYZFromTooltipInfo as c,updateTooltipInfoFromManipulatedObject as p,updateElevationFromPoint as f}from"../tooltipUtils3D.js";import{axisConstrainedDragSign as m}from"../manipulations/moveUtils.js";import{connectPasteEvent as u,getXYZDeltasFromTooltipInfo as d}from"../../../../interactive/tooltip/tooltipCommonUtils.js";import{MovePointTooltipInfo as h}from"../../../../interactive/tooltip/infos/MovePointTooltipInfo.js";import{ReshapeEdgeOffsetTooltipInfo as y}from"../../../../interactive/tooltip/infos/ReshapeEdgeOffsetTooltipInfo.js";import{SelectedVertexTooltipInfo as v}from"../../../../interactive/tooltip/infos/SelectedVertexTooltipInfo.js";import{TranslateTooltipInfo as g}from"../../../../interactive/tooltip/infos/TranslateTooltipInfo.js";import{TranslateXYTooltipInfo as k}from"../../../../interactive/tooltip/infos/TranslateXYTooltipInfo.js";import{TranslateZTooltipInfo as T}from"../../../../interactive/tooltip/infos/TranslateZTooltipInfo.js";import{verticalSignedDistanceBetweenPoints as w}from"../../../../support/euclideanLengthMeasurementUtils.js";function j(t,e){const o=e?.type;return{edgeOffset:new y({sketchOptions:t,viewType:o}),movePoint:new h({sketchOptions:t,viewType:o}),selectedVertex:new v({sketchOptions:t,viewType:o}),translate:new g({sketchOptions:t,viewType:o}),translateXY:new k({sketchOptions:t,viewType:o}),translateZ:new T({sketchOptions:t,viewType:o})}}function I(e,o,s){function r(){const t=s(),e=t.sketchOptions.tooltips.effectiveEnabled?t.activeTooltipInfo:null;return{...t,activeTooltipInfo:e}}let c=!1;function p(t){c||(c=!0,t(),c=!1)}function f(t){p(()=>{D(o,t),e.info=t.activeTooltipInfo})}return t([n(r,f,i),n(()=>{const t=r(),{activeTooltipInfo:e}=t;if(e&&"key"in e)return{context:t,activeTooltipInfo:e,key:e.key,geometry:l(o)}},(t,e)=>{t&&e&&e.key!==t.key&&p(()=>O(o,t.context))},a),o.on("committed",()=>f(r())),u(e,{onBeforePaste:()=>{c=!0},onAfterPaste:()=>{O(o,r()),c=!1}})])}function O(t,{activeTooltipInfo:e,selectedVertexManipulatorInfo:o,callbacks:n}){if(!e)return;n.onBeforeReshape();const i=l(t);switch(i?.type){case"mesh":"move-point"===e.type&&c(e,i.origin,t,n);break;case"point":"move-point"===e.type&&c(e,i,t,n);break;case"polyline":case"polygon":"selected-vertex"===e.type&&b(e,o,t,n)}}function b(t,e,o,n){if(!e)return;const{dx:i,dy:a,dz:s}=d(t,e.manipulator.location);0===i&&0===a&&0===s||(n.onReshapeStart(),o.operations?.moveVertices([e.handle],i,a,s,0),n.onReshape(),n.onReshapeStop())}function D(t,e){const{activeTooltipInfo:o,selectedVertexManipulatorInfo:n,sketchOptions:i}=e;if(o)switch(o.sketchOptions=i,o?.type){case"move-point":p(o,t);break;case"selected-vertex":L(o,n,t,e)}}function L(t,e,o,{automaticAreaMeasurementUtils:n,automaticLengthMeasurementUtils:i}){if(!t||"selected-vertex"!==t.type)return;const a=e?.manipulator.location;t.setLocationFromPoint(a),f(t,a,o);const s=o.operations?.data.geometry;switch(s?.type){case"polygon":t.geometryType="polygon",t.totalLength.visible=!1,t.area.actual=n.autoArea2D(s);break;case"polyline":t.geometryType="polyline",t.totalLength.actual=i.autoLength2D(s),t.area.visible=!1}}const U=s(r);function x(t,o,{sketchOptions:n,tooltipInfos:i,automaticLengthMeasurementUtils:a}){let s=null;switch(t){case 0:s=i.translate,M(s,o,(t,e)=>a.autoDistanceBetweenPoints2D(U(t),U(e)));break;case 1:s=i.translateXY,M(s,o,(t,n)=>e(a.autoDistanceBetweenPoints2D(U(t),U(n)),m(o)));break;case 2:s=i.translateZ,M(s,o,w)}return s.sketchOptions=n,s}function M(t,e,n){if(null!=e&&"end"!==e.action){const{mapStart:i,mapEnd:a}=e,s=n(i,a);t.distance=null!=s?s:o}else t.distance=o}function P({operations:t},n,{sketchOptions:i,tooltipInfos:a,automaticAreaMeasurementUtils:s,automaticLengthMeasurementUtils:r}){const l=a.edgeOffset;if(l.distance.committed=null,null!=n&&null!=n.signedDistance&&"end"!==n.action&&null!=t){const o=t.data.coordinateHelper,{start:i,end:a}=n.operation.clampedStartAndEnd(n.mapEnd),s=r.autoDistance2D(i,a,o.spatialReference),c=Math.sign(n.signedDistance*n.operation.selectedArrow);l.distance.actual=null!=s?e(s,c):null}else l.distance.actual=o;const c=t?.data.geometry;switch(l.area.actual=null,l.area.visible=!1,l.totalLength.actual=null,l.totalLength.visible=!1,c?.type){case"polygon":l.area.actual=s.autoArea2D(c),l.area.visible=!0;break;case"polyline":l.totalLength.actual=r.autoLength2D(c),l.totalLength.visible=!0}return l.sketchOptions=i,l}export{I as connectTooltipToManipulatedObject,j as createTooltipInfos,P as getUpdatedEdgeOffsetTooltipInfo,x as getUpdatedTranslateObjectTooltipInfo};
