/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{handlesGroup as e}from"../../../core/handleUtils.js";import{debounce as t}from"../../../core/promiseUtils.js";import{zeroMeters as o}from"../../../core/quantity.js";import{watch as n,syncAndInitial as i,sync as r}from"../../../core/reactiveUtils.js";import{createScreenPoint as a}from"../../../core/screenUtils.js";import s from"../../../geometry/Point.js";import{isCurvedGeometry as c}from"../../../geometry/support/curves/curveUtils.js";import{connectPasteEvent as l,getXYZDeltasFromTooltipInfo as p}from"../../interactive/tooltip/tooltipCommonUtils.js";import{MovePointTooltipInfo as m}from"../../interactive/tooltip/infos/MovePointTooltipInfo.js";import{SelectedVertexTooltipInfo as u}from"../../interactive/tooltip/infos/SelectedVertexTooltipInfo.js";import{TranslateTooltipInfo as f}from"../../interactive/tooltip/infos/TranslateTooltipInfo.js";import{TranslateVertexTooltipInfo as y}from"../../interactive/tooltip/infos/TranslateVertexTooltipInfo.js";import{elevationFromPoint as v}from"../../support/euclideanLengthMeasurementUtils.js";function g(e){const t={sketchOptions:e,viewType:"2d"};return{movePoint:new m(t),selectedVertex:new u(t),translateGraphic:new f(t),translateVertices:new y(t)}}function h(o,a,s){function c(){const e=a(),t=e.sketchOptions.tooltips.effectiveEnabled?e.activeTooltipInfo:null;return{...e,activeTooltipInfo:t}}let p=!1;function m(e){p||(p=!0,e(),p=!1)}const u=t(e=>x(e)),f=e=>{m(()=>{d(e),o.info=e.activeTooltipInfo,s.consumePromise(u(e))})};return e([n(()=>{const e=c();return{context:e,geometry:e.graphic?.geometry,vertexGeometry:e.selectedVertex?.geometry}},({context:e})=>f(e),i),n(()=>{const e=c(),{activeTooltipInfo:t}=e;if(t&&"key"in t)return{context:e,key:t.key}},(e,t)=>{e&&t&&e.key!==t.key&&m(()=>b(e.context))},r),l(o,{onBeforePaste:()=>{p=!0},onAfterPaste:()=>{b(c()),p=!1}})])}function d(e){const t=e.activeTooltipInfo;if(t)switch(t.sketchOptions=e.sketchOptions,t?.type){case"move-point":w(t,e);break;case"selected-vertex":k(t,e)}}async function x(e){const t=e.activeTooltipInfo;if("selected-vertex"===t?.type)return await T(t,e)}function w(e,{graphic:t}){j(e,t,t?.geometry)}function k(e,{graphic:t,selectedVertex:o}){j(e,t,o?.geometry);const n=t?.geometry;if(!n||!c(n))switch(n?.type){case"polygon":e.geometryType="polygon",e.totalLength.visible=!1;break;case"polyline":e.geometryType="polyline",e.area.visible=!1;break;case"multipoint":e.geometryType="multipoint",e.area.visible=!1,e.totalLength.visible=!1}}async function T(e,{graphic:t,measurementWorker:o}){const n=t?.geometry;switch(n?.type){case"polygon":e.area.actual=(await o.autoArea2D(n)).area;break;case"polyline":e.totalLength.actual=await o.autoLength2D(n)}}function j(e,t,o){t&&"point"===o?.type&&(e.setLocationFromPoint(o),e.elevation.actual=v(o),e.elevation.visible=!!t.geometry?.hasZ,e.elevation.readOnly=!1,e.elevation.showAsZ=!0)}function b(e){function t(t,o){const n=o?.geometry;if(!n||"point"!==n.type)return;const{dx:i,dy:r,dz:a}=p(t,n);if(0===i&&0===r&&0===a)return;const{x:c,y:l,z:m,m:u,spatialReference:f}=n,y=new s({x:c+i,y:l+r,z:null!=m?m+a:void 0,m:u,spatialReference:f});e.updateGeometry(o,y,n,i,r)}const{activeTooltipInfo:o,graphic:n,selectedVertex:i}=e;switch(o?.type){case"move-point":return t(o,n);case"selected-vertex":return t(o,i)}}function P(e,t,o,n,i){n.consumePromise(I(e,t,i,o))}const I=t(V);function L(e,t,o,n,i,r){e.elevation=null,i.consumePromise(D(e,t,r,o,n))}const D=t(U);async function U(e,t,n,i,{measurementWorker:r}){const s=[];if(n){const{x:o,y:i}=n.origin,c=t.toMap(n),l=t.toMap(a(o,i));s.push(r.autoDistance2DBetweenPoints(l,c).then(t=>{e.distance=t}))}else e.distance=o;switch(i?.type){case"polygon":s.push(r.autoArea2D(i).then(({area:t})=>{e.area=t,e.totalLength=null}));break;case"polyline":s.push(r.autoLength2D(i).then(t=>{e.area=null,e.totalLength=t}));break;default:e.area=null}await Promise.allSettled(s)}async function V(e,t,n,{measurementWorker:i}){if(!n)return void(e.distance=o);const{x:r,y:s}=n.origin,c=t.toMap(n),l=t.toMap(a(r,s));e.distance=await i.autoDistance2DBetweenPoints(l,c)}export{h as connectTooltip,g as createTooltipInfos,P as updateTranslateTooltipInfo,L as updateTranslateVertexTooltipInfo};
