/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{splitBezierCurve as e}from"../../../../geometry/support/curves/bezierCurveUtils.js";import{deriveCircleFromCircularArc as t,splitCircularArc as r}from"../../../../geometry/support/curves/circularArcUtils.js";import{getEndpoint as s}from"../../../../geometry/support/curves/curveUtils.js";import{deriveCircleFromEllipticArc4 as i,splitEllipticArc4 as c}from"../../../../geometry/support/curves/ellipticArc4Utils.js";import{deriveEllipse as o,splitEllipticArc7 as d}from"../../../../geometry/support/curves/ellipticArc7Utils.js";import{Vertex as a,createConnectingSegment as h}from"../EditGeometry.js";class n{constructor(e,t,r){this._editGeometry=e,this._segment=t,this._t=r,this.createdVertex=null,this._createdLeftEdge=null,this._createdRightEdge=null}apply(){let n="redo";const g=this._editGeometry.coordinateHelper,l=this._segment,p=l.part,u=l.leftVertex,m=l.rightVertex;p.segments.splice(p.segments.indexOf(l),1);const f=[];switch(null==this.createdVertex&&(n="apply",this.createdVertex=new a(p)),l.type){case"line":f.push(g.lerp(u.pos,m.pos,this._t,g.createVector()));break;case"bezier":{const t=l.toCurveOrCoordinate();f.push(...e(u.pos,t,this._t));break}case"circular-arc":{const e=l.toCurveOrCoordinate(),s=t(u.pos,e);f.push(...r(s,e,this._t));break}case"elliptic-arc-4":{const e=l.toCurveOrCoordinate(),t=i(u.pos,e);f.push(...c(t,e,this._t));break}case"elliptic-arc-7":{const e=l.toCurveOrCoordinate(),t=o(u.pos,e);f.push(...d(t,e,this._t));break}}p.vertices.push(this.createdVertex),this.createdVertex.pos=g.arrayToVector(s(f[0])),f.length>1&&(m.pos=g.arrayToVector(s(f[1]))),null==this._createdLeftEdge&&(this._createdLeftEdge=h(p,u,this.createdVertex,f[0])),this._createdLeftEdge.leftVertex.leftSegment?p.segments.push(this._createdLeftEdge):p.segments.unshift(this._createdLeftEdge),u.rightSegment=this._createdLeftEdge,null==this._createdRightEdge&&(this._createdRightEdge=h(p,this.createdVertex,m,f.at(1)??m.pos)),p.segments.push(this._createdRightEdge),m.leftSegment=this._createdRightEdge,p.updateVertexIndex(this.createdVertex,u.index+1),this._editGeometry.notifyChanges({operation:n,addedVertices:[this.createdVertex]})}undo(){if(null==this.createdVertex||null==this._createdLeftEdge||null==this._createdRightEdge)return null;const e=this._segment,t=e.part,{leftSegment:r,rightSegment:s}=this.createdVertex,i=r?.leftVertex,c=s?.rightVertex;t.vertices.splice(t.vertices.indexOf(this.createdVertex),1),t.segments.splice(t.segments.indexOf(this._createdLeftEdge),1),t.segments.splice(t.segments.indexOf(this._createdRightEdge),1),e.leftVertex.leftSegment?t.segments.push(e):t.segments.unshift(e),i&&(i.rightSegment=e),c&&(c.leftSegment=e),i&&t.updateVertexIndex(i,i.index),this._editGeometry.notifyChanges({operation:"undo",removedVertices:[this.createdVertex]})}accumulate(){return!1}}export{n as SplitSegment};
