/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{removeHandles as e,handlesGroup as t,destroyHandle as n,refHandle as a}from"../../../../core/handleUtils.js";import{deg2rad as l}from"../../../../core/mathUtils.js";import{watch as i,initial as o}from"../../../../core/reactiveUtils.js";import{signal as s}from"../../../../core/signal.js";import{h as r,i as c,j as u}from"../../../../chunks/vec32.js";import{create as h}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{Manipulator3D as m}from"../Manipulator3D.js";import{manipulatedObjectGeometry as p}from"./manipulatedObjectUtils.js";import{ManipulatorState as d}from"./ManipulatorState.js";import{Settings as f}from"./settings.js";import{ExtendedLineVisualElement as v}from"../visualElements/ExtendedLineVisualElement.js";import{LaserlineVisualElement as g}from"../visualElements/LaserlineVisualElement.js";import{OutlineVisualElement as b}from"../visualElements/OutlineVisualElement.js";function E(t){const{object:n}=t,a=j(t),l=[a,w(t,a.visualElement)];return n.maskOccludee&&l.push(n.maskOccludee()),{visualElement:a.visualElement,remove:()=>e(l)}}function j(e){const{view:a,object:l}=e,s=p(l),r=new b({view:a,geometry:M(s)?s:null,elevationInfo:l.elevationInfo,attached:!1,isDecoration:!0}),c=new f({getTheme:()=>a.effectiveTheme}),u=()=>{r.attached=l.visible},h=t([i(()=>c.visualElements.lineObjects.outline,e=>e.apply(r),o),i(()=>c.visualElements.lineObjects.shadowStyle,e=>e.apply(r),o),i(()=>l.visible,u),i(()=>l.isDraped,e=>{r.isDraped=e},o),l.on("committed",()=>{const e=p(l);r.geometry=M(e)?e:null}),n(r)]);return u(),{visualElement:r,remove:()=>h.remove()}}function w(r,c){const{object:u,view:h}=r,m=[],b=u.elevationInfo,E="on-the-ground"===b.mode||!b.offset&&"absolute-height"!==b.mode,j=new d,w=new f({getTheme:()=>h.effectiveTheme}),O=w.visualElements,D=new v({view:h,extensionType:O.zVerticalLine.extensionType,innerWidth:1,attached:!1,writeDepthEnabled:!1,renderOccluded:4,isDecoration:!0}),T=l(O.heightPlaneAngleCutoff),L=new g({view:h,attached:!1,angleCutoff:T,isDecoration:!0}),x=s(1);m.push(i(()=>({lineShadowStyle:w.visualElements.lineObjects.shadowStyle,pointShadowStyle:w.visualElements.pointObjects.shadowStyle,alpha:x.value}),({lineShadowStyle:e,pointShadowStyle:t,alpha:n})=>{e.apply(c,n),t.apply(D,n)},o));const C=s(1);m.push(i(()=>({heightPlane:w.visualElements.heightPlane,alpha:C.value}),({heightPlane:e,alpha:t})=>e.apply(L,t),o));const V=()=>{j.update(r);const e=p(u);if(!u.visible||E&&(u.isDraped||!M(e)||!e.hasZ))return c.laserlineEnabled=!1,D.attached=!1,void(L.attached=!1);c.laserlineEnabled=!0;const t=4&j.grabbingState?O.laserlineAlphaMultiplier:1;x.value=t;const n=2&j.grabbingState?O.laserlineAlphaMultiplier:1;C.value=n,y(D,j),S(r,c,L,j)};m.push(i(()=>w.visualElements.zVerticalLine,e=>e.apply(D),o)),m.push(u.on("committed",V),i(()=>u.visible,V),c.events.on("attachment-origin-changed",V),n(D),n(L));const A=[],P=()=>{e(A),A.length=0,r.forEachManipulator(e=>A.push(e.events.on("grab-changed",V))),r.forEachManipulator(e=>A.push(e.events.on("select-changed",V))),V()};return P(),m.push(r.onManipulatorsChanged(P),a(()=>t(A))),t(m)}function y(e,t){const n=1===t.numSelected?t.firstSelected:t.numSelected>1&&null!=t.firstGrabbedXY?t.firstGrabbedXY:null;null!=n?(e.setStartEndFromWorldDownAtLocation(n.renderLocation),e.attached=!0):e.attached=!1}function S(e,t,n,a){if(a.numSelected>0){r(O,0,0,0);let t=0;e.forEachManipulator((e,n)=>{1===n&&e.selected&&e instanceof m&&(c(O,O,e.renderLocation),t++)}),t>0?(n.heightManifoldTarget=u(O,O,1/t),n.attached=!0):n.attached=!1}else{const a=t.attachmentOrigin;null!=a&&e.view.renderCoordsHelper.toRenderCoords(a,O)?(n.heightManifoldTarget=O,n.attached=!0):n.attached=!1}}function M(e){return null!=e&&("polygon"===e.type||"polyline"===e.type)}const O=h();export{E as createVisualElements};
