/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import e from"../../../../core/Handles.js";import"../../../../core/has.js";import{equals as t}from"../../../../core/lang.js";import{destroyMaybe as s}from"../../../../core/maybe.js";import{formatDecimal as i}from"../../../../core/quantityFormatUtils.js";import{watch as n,initial as l}from"../../../../core/reactiveUtils.js";import{pt2px as o}from"../../../../core/screenUtils.js";import{create as a}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{computeSpanningSegment as m,maxScreenLengthSquaredFromGeometry as r,offsetSegment as f}from"./lengthDimensionUtils.js";import{smallScreenLengthLineSizeFactor as d,minScreenLengthFontSizeFactor as c,labelMarginPx as h}from"./settings.js";import{LabelVisualElement as u}from"../../interactive/visualElements/LabelVisualElement.js";import{LineVisualElement as S}from"../../interactive/visualElements/LineVisualElement.js";import{MarkerVisualElement as g}from"../../interactive/visualElements/MarkerVisualElement.js";import{EuclideanSegment as p}from"../../interactive/visualElements/support/Segment.js";class v{set visible(e){for(const t of this._visualElements.values())t.attached=e}constructor(s){this.destroyed=!1,this._handles=new e,this._messages=null,this._labelSegment=new p;const{analysis:i,computation:a,view:m,messages:r,isDecoration:f}=s;this.analysis=i,this.computation=a,this.view=m,this._messages=r;const d=s.visible,c={view:m,attached:d,isDecoration:f},{fontSize:h,textColor:v,textBackgroundColor:b}=i.style;this._visualElements=new w({marker:new g(c,s.markerMaterial),dimension:new S(c,s.dimensionLineMaterial),startOffset:new S(c,s.offsetLineMaterial),endOffset:new S(c,s.offsetLineMaterial),dimensionSmall:new S(c,s.smallDimensionLineMaterial),startOffsetSmall:new S(c,s.smallOffsetLineMaterial),endOffsetSmall:new S(c,s.smallOffsetLineMaterial),label:new u({view:m,attached:d,distance:0,geometry:{type:"segment",sampleLocation:"center",segment:this._labelSegment,callout:!1},fontSize:o(h),textColor:v.clone(),backgroundColor:b.clone(),isDecoration:f})}),this._handles.add([n(()=>a.geometry,e=>{this.updateCameraDependentElements(m.state.camera,e,i.style),null!=a.geometry&&this._updateLines(a.geometry)},{...l,equals:t}),n(()=>a.length,e=>this._updateLabelContent(e),l)])}destroy(){this.destroyed=!0,this._handles=s(this._handles);for(const e of this._visualElements.values())e.destroy()}get testInfo(){}_updateLines(e){const t=m(O,0,e.directSegment,e.dimensionSegment),s=m(y,1,e.directSegment,e.dimensionSegment),i=this._visualElements;i.marker.setGeometryFromSegment(e.dimensionSegment,e.primaryOffsetAxis),i.dimension.setGeometryFromSegment(e.dimensionSegment),i.startOffset.setGeometryFromSegment(t),i.endOffset.setGeometryFromSegment(s),i.dimensionSmall.setGeometryFromSegment(e.dimensionSegment),i.startOffsetSmall.setGeometryFromSegment(t),i.endOffsetSmall.setGeometryFromSegment(s)}updateCameraDependentElements(e,t,s){const i=this._visualElements;if(null==t){for(const e of i.values())e.visible=!1;return}const n=e.computeScreenPixelSizeAt(t.dimensionSegment.eval(.5,_)),l=r(t,n),a=l<(o(s.lineSize)*d)**2,m=!a;i.marker.visible=m,i.dimension.visible=m,i.startOffset.visible=m,i.endOffset.visible=m,i.dimensionSmall.visible=a,i.startOffsetSmall.visible=a,i.endOffsetSmall.visible=a;const h=o(s.fontSize)*c,{label:u}=i;if(u.visible=l>=h**2,!u.visible)return;const{dimensionSegment:S,primaryOffsetAxis:g}=t,{offset:p}=this.computation.dimension,v=(Math.sign(p)>=0?1:-1)*b(s)*n;f(this._labelSegment,S,g,v),u.updateLabelPosition()}updateLabelStyle(e){const{label:t}=this._visualElements;t.fontSize=o(e.fontSize),t.textColor=e.textColor,t.backgroundColor=e.textBackgroundColor}updateUnitsMessages(e){this._messages=e;const{length:t}=this.computation;this._updateLabelContent(t)}_updateLabelContent(e){const{label:t}=this._visualElements;null!=e&&null!=this._messages?t.text=i(this._messages,e,e.unit):t.text=""}}function b(e){return 1.5*o(e.fontSize)+h+o(e.lineSize/2)}const O=new p,y=new p,_=a();class w{constructor(e){this.marker=e.marker,this.dimension=e.dimension,this.startOffset=e.startOffset,this.endOffset=e.endOffset,this.dimensionSmall=e.dimensionSmall,this.startOffsetSmall=e.startOffsetSmall,this.endOffsetSmall=e.endOffsetSmall,this.label=e.label}values(){return[this.marker,this.dimension,this.startOffset,this.endOffset,this.dimensionSmall,this.startOffsetSmall,this.endOffsetSmall,this.label]}}export{w as LengthDimensionVisualElements,v as LengthDimensionVisualization};
