/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{__decorate as e}from"tslib";import{zeroDegreesGeographic as t,createLength as i}from"../../../core/quantity.js";import{watch as o,initial as s}from"../../../core/reactiveUtils.js";import{property as n,subclass as r}from"../../../core/accessorSupport/decorators.js";import{isPolyline as l}from"../../../geometry/support/typeUtils.js";import{PluginBase as a}from"./PluginBase.js";import{knownFieldDisplayOrders as d}from"../support/types.js";import{directionForVertices as p}from"../../support/angularMeasurementUtils.js";const m=Symbol("polyline-committed-length-measurement"),u=Symbol("polyline-staged-measurement"),c=Symbol("polyline-cursor-distance-measurement"),h=Symbol("polyline-distance-field"),_=Symbol("polyline-direction-field"),y=Symbol("polyline-total-length-field");let g=class extends a{constructor(){super(...arguments),this._lineVisualId=Symbol()}get _downstreamGeometry(){return this._downstreamEditSession?.generatePreviewGeometry({includeAutomaticConnection:!0,geometryType:"polyline"})}get _downstreamEditSession(){const{_downstreamPlugin:e,_context:t}=this;return e?t?.getSession(e):null}get _committed(){return this._getCommittedGeometry()}get helpMessageKey(){return this._session?.lastPointAddedBySession?"polylineOneVertex":"polylineZeroVertices"}start(e){super.start(e);const{session:t}=e;t.automaticRestart=!1,e.addOrUpdateVisual(this._lineVisualId,{role:"outline",state:"active"}),this.addHandles([o(()=>this._downstreamEditSession?.lastPointAddedBySession,t=>{this._cursorVertex=t;const i=this._downstreamGeometry,o=this._session;t&&o?(this._lastVertex=null!=t.pointIndex&&t.pointIndex>0?o.getInformationAboutPoint(t.pointIndex-1,t.partIndex):null,this._secondToLastVertex=null!=t.pointIndex&&t.pointIndex>1?o.getInformationAboutPoint(t.pointIndex-2,t.partIndex):null):(this._lastVertex=null,this._secondToLastVertex=null),e.addOrUpdateVisual(this._lineVisualId,{geometry:i})},s)],this._ownHandlesKey),this._setupTooltips()}complete(){this._session?.complete(),this._isComplete=!0,this.removeHandles(this._ownHandlesKey),this.removeHandles(this._downstreamHandlesKey)}attachPlugin(e){this._downstreamPlugin=e,this.notifyChange("_downstreamEditSession")}detachPlugin(e,t){this.notifyChange("_committed"),"complete"===t&&e.configuration&&this._context?.requestBeginDownstreamPlugin(e.configuration)}_getCommittedGeometry(){return this._session?.generatePreviewGeometry({includeAutomaticConnection:!1,geometryType:"polyline"})}_setupTooltips(){this._context&&(this._distanceField??=this._context.createMeasuredField({id:h,preset:"distance",getMeasurementInput:()=>[this._lastVertex?.point,this._cursorVertex?.point],measure:async(e,t)=>{if(!e||!e[0]||!e[1])return null;const i=t.inputUnitInfos.length.unit;return this._context?.measureDistance([e[0],e[1]],c,"tooltip",i)},fieldProperties:{displayOrder:d.Coordinates+11},fieldFilters:{appliesToHandlesWithRole:"primary"}}),this._directionField??=this._context.createMeasuredField({id:_,preset:"direction",getMeasurementInput:()=>[this._lastVertex?.point,this._cursorVertex?.point],measure:async([e,i],o)=>{const s=o.sketchOptions.values.effectiveDirectionMode;if(e&&i){const o=p(this._secondToLastVertex?.point,e,i,s);return this._directionField&&(this._directionField.readOnly="absolute"===s?!e:!this._secondToLastVertex?.point),o??("absolute"===s||this._secondToLastVertex?t:null)}return null},fieldProperties:{displayOrder:d.Coordinates+10},fieldFilters:{appliesToHandlesWithRole:"primary"}}),this._setupGeometrySpecificTooltip())}_setupGeometrySpecificTooltip(){this._context&&(this._lengthField??=this._context.createMeasuredField({id:y,preset:"totalLength",getMeasurementInput:()=>[this._committed,this._downstreamGeometry],measure:async([e,t],o,s)=>{const n=o.inputUnitInfos.length.unit,r=t&&l(t)?await(this._context?.measureLength(t,u,"tooltip",n)):void 0,a=e&&e!==s?.[0]&&l(e)?await(this._context?.measureLength(e,m,"tooltip",n)):e?this._cachedCommittedLength:null;return this._cachedCommittedLength=a,(a||r)&&(e||0!==r?.value)?i((a?.value??0)+(r?.value??0),n):null},fieldProperties:{displayOrder:d.ShapeMeasurements}}))}};e([n()],g.prototype,"_downstreamPlugin",void 0),e([n()],g.prototype,"_lastVertex",void 0),e([n()],g.prototype,"_cursorVertex",void 0),e([n()],g.prototype,"_secondToLastVertex",void 0),e([n()],g.prototype,"_lengthField",void 0),e([n()],g.prototype,"_distanceField",void 0),e([n()],g.prototype,"_directionField",void 0),e([n()],g.prototype,"_downstreamGeometry",null),e([n()],g.prototype,"_downstreamEditSession",null),e([n()],g.prototype,"_committed",null),e([n()],g.prototype,"helpMessageKey",null),g=e([r("esri.views.draw.plugins.PolylinePlugin")],g);export{g as PolylinePlugin};
