/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
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 d,execute as m}from"../../../../geometry/operators/projectOperator.js";import{g as u}from"../../../../chunks/proximityOperator.js";import g from"../../../../layers/GraphicsLayer.js";import f from"../../../../rest/support/Stop.js";import w from"../../../../symbols/SimpleLineSymbol.js";import v from"../../../../symbols/SimpleMarkerSymbol.js";import{ViewEventPriorities as _}from"../../../input/InputManager.js";const b=Symbol(),j=20;let L=class extends o{constructor(t){super(t),this._graphicsLayer=new g({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 f({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 d();const o=m(this.interaction.layer.routeInfo?.geometry,i);if(!o)return void this._clearWaypointManipulator();const s=this.view.resolution*window.devicePixelRatio*j,{coordinate:a,distance:n,isEmpty:p}=u(o,this.view.toMap(t));if(p||n>s)return void this._clearWaypointManipulator();if(this.interaction.layer.stops.map(({geometry:t})=>t).filter(r).map(t=>m(t,i)).filter(r).some(t=>y(t,a)<s))return void this._clearWaypointManipulator();this.addHandles(this.view.acquireCursor("pointer","high"),b);const c=this._graphicsLayer.graphics.at(0);if(c)return void(c.geometry=a);const{waypointSymbol:l}=this,h=new e({geometry:a,symbol:l});this._graphicsLayer.graphics.destroyAll(),this._graphicsLayer.add(h)})}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),_.TOOL),this.view.on("pointer-move",t=>this._debouncedHandlePointerMove(t).catch(n),_.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 v({color:e,outline:new w({color:s(e),width:1.5}),size:10})}_clearWaypointManipulator(){this._graphicsLayer.graphics.destroyAll(),this.removeHandles(b)}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 d();const s=m(e.geometry,r),a=m(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=m(t,r);if(!e)continue;const a=i(s,e)?.distanceAlong;if(null!=a&&a>n)return p}return o.length-1}};t([c()],L.prototype,"interaction",void 0),t([c()],L.prototype,"updating",null),t([c()],L.prototype,"waypointSymbol",null),t([c()],L.prototype,"view",void 0),L=t([l("esri.views.2d.layers.support.RouteLayerWaypointVisualization")],L);export{L as RouteLayerWaypointVisualization};
