/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{PathTransformationCursor as t}from"../CIMCursor.js";import{lazyJsonConversion as s}from"../CIMEffects.js";import{GeometryWalker as i}from"../GeometryWalker.js";const e=2,h=.001;class r{static{this.instance=null}static local(){return null===r.instance&&(r.instance=new r),r.instance}execute(t,s,i){return new a(t,s,i)}}class a extends t{constructor(t,s,e){super(t),this._segmentIterator=null,this._segCount=0,this._firstSeg=!0,this._seg=null,this._ms=[],this._from=0,this._to=0,this._pt=null,this._walker=new i,this._walker.updateTolerance(e),this._placePerPart=s.placePerPart??!1,this._angleToLine=s.angleToLine??!1,this._keepUpright=s.keepUpright??!1,this._offset=(s.offset??0)*e,this._interval=(s.interval??0)*e,this._interval<=0&&(this._interval=10*h),this._skipRate=s.skipMarkerRate??0,this._skipRate<=1&&(this._skipRate=0),this._placeAtExtremities=s.placeAtExtremities??!1}processPath(t){if(!this.iteratePath){const{fromGeometry:i}=s.module,e=i(t.getCurrentPath().asJSON()).getGeometry(),h=e.getPathStart(0);this._segmentIterator=e.querySegmentIteratorAtVertex(h),this._segCount=e.getSegmentCountPath(0),this._firstSeg=!0,this.iteratePath=!0,this._pt=e.getXY(0).clone()}for(;0===this._ms.length&&this._segmentIterator?.hasNextSegment();){--this._segCount,this._seg=this._segmentIterator.nextSegment(),this._from=this._seg.getStartAttributeAsDbl(e,0),this._to=this._seg.getEndAttributeAsDbl(e,0);const s=!isNaN(this._from),i=!isNaN(this._to);s&&(this._from=Math.round(this._from/h)*h,Math.abs(this._from)<h&&(this._from=0)),i&&(this._to=Math.round(this._to/h)*h,Math.abs(this._to)<h&&(this._to=0));let r=s?Math.floor(this._from/this._interval):0,a=i?Math.floor(this._to/this._interval):0;if(s&&(this._placeAtExtremities||!this._firstSeg||!this._placePerPart&&t.pathIndex>0)&&Math.abs(r*this._interval-this._from)<h&&(!this._skipRate||r%this._skipRate)&&this._ms.push(this._from),this._firstSeg=!1,s&&i&&!(Math.abs(this._from-this._to)<h))if(this._from<=this._to){r*this._interval<this._from+h&&++r,a*this._interval>this._to-h&&--a;for(let t=r;t<=a;++t)(!this._skipRate||t%this._skipRate)&&this._ms.push(t*this._interval)}else{r*this._interval>this._from-h&&--r,a*this._interval<this._to+h&&++a;for(let t=a;t<=r;++t)(!this._skipRate||t%this._skipRate)&&this._ms.push(t*this._interval)}}let i=0;if(0===this._ms.length){if(this.iteratePath=!1,!this._segCount&&this._placeAtExtremities&&!isNaN(this._to)&&(this._placePerPart||t.pathIndex===t.totalSize-1)){const t=Math.floor(this._to/this._interval);Math.abs(t*this._interval-this._to)<h&&(!this._skipRate||t%this._skipRate)&&(this._ms.push(this._to),i=1)}if(0===this._ms.length)return null}const r=this._ms.pop(),a=isNaN(this._from)||isNaN(this._to)||Math.abs(this._from-this._to)<h?i:(r-this._from)/(this._to-this._from);this._seg.queryCoord2D(a,this._pt);const _=this._seg.getTangent(a),{x:o,y:n}=_.normalize();return this.internalPlacement.setTranslate(this._pt.x-this._offset*n,this._pt.y+this._offset*o),this._angleToLine&&this.internalPlacement.setRotateCS(o,n),this.internalPlacement.setMeasure(r),this.internalPlacement}}export{r as PlacementAtMeasuredUnits};
