/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import{__decorate as t}from"tslib";import e from"../../../../Graphic.js";import{pointToCoordinate as i}from"../../../../arcade/functions/measures.js";import o from"../../../../core/Accessor.js";import{isSome as r}from"../../../../core/arrayUtils.js";import{getContrast as s}from"../../../../core/colorUtils.js";import{debounce as a,throwIfNotAbortError as n}from"../../../../core/promiseUtils.js";import{initial as p}from"../../../../core/reactiveUtils.js";import{property as c,subclass as l}from"../../../../core/accessorSupport/decorators.js";import{UpdatingHandles as h}from"../../../../core/support/UpdatingHandles.js";import{e as y}from"../../../../chunks/distanceOperator.js";import{load as m,execute as d}from"../../../../geometry/operators/projectOperator.js";import{g as u}from"../../../../chunks/proximityOperator.js";import{normalizeCentralMeridian as g}from"../../../../geometry/support/normalizeUtils.js";import f from"../../../../layers/GraphicsLayer.js";import w from"../../../../rest/support/Stop.js";import v from"../../../../symbols/SimpleLineSymbol.js";import _ from"../../../../symbols/SimpleMarkerSymbol.js";import{ViewEventPriorities as b}from"../../../input/InputManager.js";const j=Symbol(),L=20;let S=class extends o{constructor(t){super(t),this._graphicsLayer=new f({listMode:"hide",internal:!0}),this._updatingHandles=new h,this._debouncedHandleClick=a(async t=>{const e=this._graphicsLayer.graphics.at(0);if("point"!==e?.geometry?.type)return;t.stopPropagation();const i=new w({geometry:e.geometry.clone(),locationType:"waypoint",symbol:this.waypointSymbol.clone()}),o=await this._getWaypointIndex(i);this.interaction.layer.stops.add(i,o),this.interaction.layer.stops.forEach(t=>t.sequence=null),this.interaction.addNetworkFeature(i)}),this._debouncedHandlePointerMove=a(async t=>{if(!this.interaction.enabled||!this.interaction.interactiveOptions.waypointAdditionEnabled||this.interaction.sketchActive||this.interaction.layer.stops.length<2||!this.interaction.layer.routeInfo?.geometry)return void this._clearWaypointManipulator();const{spatialReference:i}=this.view;await m();const o=d(this.interaction.layer.routeInfo.geometry,i);if(!o)return void this._clearWaypointManipulator();const s=this.view.resolution*window.devicePixelRatio*L,a=this.view.toMap(t),n=(await g(a))[0],{coordinate:p,distance:c,isEmpty:l}=u(o,n);if(l||c>s)return void this._clearWaypointManipulator();if(this.interaction.layer.stops.map(({geometry:t})=>t).filter(r).map(t=>d(t,i)).filter(r).some(t=>y(t,p)<s))return void this._clearWaypointManipulator();this.addHandles(this.view.acquireCursor("pointer","high"),j);const h=this._graphicsLayer.graphics.at(0);if(h)return void(h.geometry=p);const{waypointSymbol:f}=this,w=new e({geometry:p,symbol:f});this._graphicsLayer.graphics.destroyAll(),this._graphicsLayer.add(w)})}initialize(){this.view.map?.add(this._graphicsLayer),this._updatingHandles.add(()=>({enabled:this.interaction.enabled,graphic:this._graphicsLayer.graphics.at(0),symbol:this.waypointSymbol}),({enabled:t,graphic:e,symbol:i})=>{t?e&&(e.symbol=i):this._clearWaypointManipulator()},p),this.addHandles([this.view.on("click",t=>this._debouncedHandleClick(t).catch(n),b.TOOL),this.view.on("pointer-move",t=>this._debouncedHandlePointerMove(t).catch(n),b.TOOL)])}destroy(){this._graphicsLayer.removeFromParent(),this._graphicsLayer.graphics.destroyAll(),this._graphicsLayer.destroy(),this._updatingHandles.destroy()}get updating(){return!this._graphicsLayer.loaded||this._updatingHandles.updating}get waypointSymbol(){const t=this.interaction.layer.defaultSymbols.stops?.waypoint;if(t)return t;const{accentColor:e}=this.view.effectiveTheme;return new _({color:e,outline:new v({color:s(e),width:1.5}),size:10})}_clearWaypointManipulator(){this._graphicsLayer.graphics.destroyAll(),this.removeHandles(j)}async _getWaypointIndex(t){const{routeInfo:e,stops:o}=this.interaction.layer;if(2===o.length||!e?.geometry||!t.geometry)return 1;const{spatialReference:r}=this.view;await m();const s=d(e.geometry,r),a=d(t.geometry,r);if(!s||!a)return 1;const n=i(s,a)?.distanceAlong;if(!n)return 1;for(let p=1;p<o.length;p++){const t=o.at(p)?.geometry;if(!t)continue;const e=d(t,r);if(!e)continue;const a=i(s,e)?.distanceAlong;if(null!=a&&a>n)return p}return o.length-1}};t([c()],S.prototype,"interaction",void 0),t([c()],S.prototype,"updating",null),t([c()],S.prototype,"waypointSymbol",null),t([c()],S.prototype,"view",void 0),S=t([l("esri.views.2d.layers.support.RouteLayerWaypointVisualization")],S);export{S as RouteLayerWaypointVisualization};
