/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{colorVectorEquals as r}from"../../../../core/colorUtils.js";import e from"../../../../core/Handles.js";import{destroyMaybe as t}from"../../../../core/maybe.js";import{watch as s}from"../../../../core/reactiveUtils.js";import{create as i}from"../../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{copy as o,subtract as a,sub as n,cross as h,len as l}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as c}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{copy as d}from"../../../../core/libs/gl-matrix-2/math/vec4.js";import{create as _}from"../../../../core/libs/gl-matrix-2/factories/vec4f64.js";import{sv3d as m}from"../../../../geometry/support/vectorStacks.js";import{Object3DVisualElement as p}from"./Object3DVisualElement.js";import{headingTiltToDirectionUp as g}from"../../support/cameraUtils.js";import{createDirectionUp as u}from"../../support/cameraUtilsInternal.js";import{createPolylineGeometry as w}from"../../webgl-engine/lib/GeometryUtil.js";import{MeasurementArrowMaterial as f}from"../../webgl-engine/materials/MeasurementArrowMaterial.js";class C extends p{constructor(r){super(r),this._arrowWidth=16,this._arrowSubdivisions=128,this._origin=c(),this._originTransform=i(),this._arrowCenter=c(),this._renderOccluded=4,this._geometry=null,this._stripeLength=1,this._stripesEnabled=!0,this._color=_(),this._contrastColor=_(),this.applyProperties(r)}get renderOccluded(){return this._renderOccluded}set renderOccluded(r){r!==this._renderOccluded&&(this._renderOccluded=r,this._arrowMaterial?.setParameters({renderOccluded:r}))}get transparentRenderOrder(){return this._transparentRenderOrder}set transparentRenderOrder(r){this._transparentRenderOrder=r,this._arrowMaterial&&this._arrowMaterial.setParameters({testsTransparentRenderOrder:r})}get geometry(){return this._geometry}set geometry(r){this._geometry=r,this._geometryChanged()}get stripeLength(){return this._stripeLength}set stripeLength(r){this._stripeLength=r,this.attached&&this._arrowMaterial.setParameters({stripeLength:this._stripeLength})}get stripesEnabled(){return this._stripesEnabled}set stripesEnabled(r){if(this._stripesEnabled=r,this.attached){const r=this._stripesEnabled?this._contrastColor:this._color;this._arrowMaterial.setParameters({stripeEvenColor:r})}}get color(){return this._color}set color(e){r(e,this._color)||(d(this._color,e),this._updateArrowColor())}get contrastColor(){return this._contrastColor}set contrastColor(e){r(e,this._color)||(d(this._contrastColor,e),this._updateArrowColor())}createExternalResources(){const r=this._color,t=this._contrastColor,i=this._stripesEnabled?t:r;this._arrowMaterial=new f({outlineColor:r,stripeEvenColor:i,stripeOddColor:r,renderOccluded:this.renderOccluded,isDecoration:this.isDecoration,testsTransparentRenderOrder:this._transparentRenderOrder}),this._handles=new e,this._handles.add(s(()=>this.view.state.camera,()=>{this._viewChanged()}))}destroyExternalResources(){this._arrowMaterial=null,this._handles=t(this._handles)}forEachMaterial(r){r(this._arrowMaterial)}createGeometries(r){if(null==this._geometry?.startRenderSpace||null==this._geometry.endRenderSpace)return;const e=this._createArrowGeometry(this._geometry.startRenderSpace,this._geometry.endRenderSpace,this._origin,this._geometry);e.transformation=this._originTransform,r.addGeometry(e),this._viewChanged()}_createArrowGeometry(r,e,t,s){const i=this.view,n=i.renderCoordsHelper,h=[],l=[],c=(r,e)=>{const s=m.get();a(s,r,t),h.push(s),l.push(e)};if("euclidean"===s.type){s.eval(.5,this._arrowCenter);const t=m.get();if(n.worldUpAtPosition(this._arrowCenter,t),y(r,e,t)){const{heading:e,tilt:s}=i.camera,{direction:a}=g(i,r,e,s,b);o(t,a)}c(r,t),c(e,t)}else{s.eval(.5,this._arrowCenter);const r=this._arrowSubdivisions+1&-2;for(let e=0;e<r;++e){const t=e/(r-1),i=m.get(),o=m.get();s.eval(t,i),n.worldUpAtPosition(i,o),c(i,o)}}return w(this._arrowMaterial,h,l)}_geometryChanged(){this.recreateGeometry()}_viewChanged(){if(this.view.ready&&this.attached&&null!=this._geometry){const r=this.view.state.camera.computeScreenPixelSizeAt(this._arrowCenter);this._arrowMaterial.setParameters({width:this._arrowWidth*r})}}_updateArrowColor(){if(!this.attached)return;const r=this._color,e=this._contrastColor,t=this._stripesEnabled?e:r,s=r,i=r;this._arrowMaterial.setParameters({stripeEvenColor:t,outlineColor:s,stripeOddColor:i})}}function y(r,e,t){const s=n(v,e,r),i=h(v,s,t);return 0===l(i)}const v=c(),b=u();export{C as MeasurementArrowVisualElement};
