/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{GeometryCursor as t}from"../../../geometry/GeometryCursor.js";import{collectPath as e}from"../../../geometry/geometryCursorCollectUtils.js";import{PathEffectCursor as r}from"../CIMCursor.js";import{CurveHelper as n,pixelTolerance as o}from"../CurveHelper.js";const s=1.7320508075688772,i=5,c="OpenEnded";class u{static{this.instance=null}static local(){return null===u.instance&&(u.instance=new u),u.instance}execute(t,e,r){return new h(t,e,r)}}class h extends r{constructor(t,e,r){super(t,!1,!0),this._curveHelper=new n;const s=r.geomUnitsPerPoint;this._width=(void 0!==e.width?e.width:i)*s,this._arrowType=void 0!==e.geometricEffectArrowType?e.geometricEffectArrowType:void 0!==e.arrowType?e.arrowType:c,this._offsetFlattenError=o*s}processPath(e){const r=t.createEmptyOptimizedCIM(e.geometryType);switch(this._arrowType){case"OpenEnded":default:this._constructSimpleArrow(r,e,!0);break;case"Block":this._constructSimpleArrow(r,e,!1);break;case"Crossed":this._constructCrossedArrow(r,e)}return r}_constructSimpleArrow(t,r,n){const o=r.pathLength();let s=this._width;o<2*s&&(s=o/2);const i=this._curveHelper.getSubCurve(r,0,o-s);if(!i||!i.nextPath())return;i.seekPathStart();const c=s/2;if(this._curveHelper.isEmpty(i))return;const u=e(i),h=this._constructOffset(u,-c);if(!h)return;const P=this._constructOffset(u,c);if(!P)return;const l=this._constructArrowBasePoint(h,-c/2);if(!l)return;const p=this._constructArrowBasePoint(P,c/2);if(!p)return;r.seekInPath(r.pathSize-1);const a=[r.x,r.y];t.pushPath(P),t.nextPath(),t.nextPoint(),t.setControlPoint(),t.pushPoint(p),t.nextPoint(),t.setControlPoint(),t.pushPoint(a),t.nextPoint(),t.setControlPoint(),t.pushPoint(l),t.nextPoint(),t.setControlPoint(),t.pushPoints(h.reverse()),t.setControlPoint(),n||(t.setControlPointAt(0),t.setControlPointAt(t.pathSize-1),t.pushPoint(P[0])),t.reset()}_constructCrossedArrow(t,r){const n=r.pathLength();let o=this._width;n<o*(1+s+1)&&(o=n/(1+s+1)),r.seekPathStart();const i=this._curveHelper.getSubCurve(r,0,n-o*(1+s));if(!i)return;i.nextPath();const c=o/2;if(this._curveHelper.isEmpty(i))return;const u=e(i),h=this._constructOffset(u,c);if(!h)return;const P=this._constructOffset(u,-c);if(!P)return;const l=this._curveHelper.getSubCurve(r,0,n-o);if(!l)return;if(l.nextPath(),this._curveHelper.isEmpty(l))return;const p=e(l),a=this._constructOffset(p,c);if(!a)return;const f=this._constructOffset(p,-c);if(!f)return;const _=a[a.length-1],C=this._constructArrowBasePoint(a,c/2);if(!C)return;const m=f[f.length-1],w=this._constructArrowBasePoint(f,-c/2);if(!w)return;r.seekInPath(r.pathSize-1);const x=[r.x,r.y];t.pushPath(h),t.nextPath(),t.nextPoint(),t.setControlPoint(),t.pushPoint(m),t.nextPoint(),t.setControlPoint(),t.pushPoint(w),t.nextPoint(),t.setControlPoint(),t.pushPoint(x),t.nextPoint(),t.setControlPoint(),t.pushPoint(C),t.nextPoint(),t.setControlPoint(),t.pushPoint(_),t.nextPoint(),t.setControlPoint(),t.pushPoints(P.reverse()),t.nextPoint(),t.setControlPoint(),t.reset()}_constructOffset(t,e){return this._curveHelper.offset(t,e,"Rounded",4,this._offsetFlattenError)}_constructArrowBasePoint(t,e){if(!t||t.length<2)return null;const r=t[t.length-2],n=t[t.length-1],o=[n[0]-r[0],n[1]-r[1]];return this._curveHelper.normalize(o),[n[0]+o[1]*e,n[1]-o[0]*e]}}export{u as EffectArrow};
