/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{getEndpoint as e}from"../../../../geometry/support/curves/curveUtils.js";import{Vertex as t,createConnectingSegment as i,LineSegment as s}from"../EditGeometry.js";class r{constructor(e,t,i){this._editGeometry=e,this._part=t,this._posOrCurveDef=i,this._addedVertex=null,this._originalSegment=null,this._left=null,this._right=null,this._partAdded=!1}apply(){let r="redo";const{_part:d,_editGeometry:n,_posOrCurveDef:l}=this;if(null==this._addedVertex&&(r="apply",this._addedVertex=new t(d)),!n.parts.includes(d)){this._partAdded=!0;const e=n.parts.at(-1)?.index??-1;d.index=e+1,n.parts.push(d)}const h=d.getLastVertex(),a=n.coordinateHelper.arrayToVector(e(l));if(null==h)d.vertices.push(this._addedVertex),this._addedVertex.pos=a,this._addedVertex.index=0;else{let e=null;h.rightSegment&&(this._originalSegment=h.rightSegment,e=this._originalSegment.rightVertex,d.segments.splice(d.segments.indexOf(this._originalSegment),1)),d.vertices.push(this._addedVertex),this._addedVertex.pos=a,this._left??=i(d,h,this._addedVertex,l),d.segments.push(this._left),h.rightSegment=this._left,null!=this._originalSegment&&null!=e&&(this._right??=new s(this._part,this._addedVertex,e),d.segments.push(this._right),e.leftSegment=this._right),d.updateVertexIndex(this._addedVertex,h.index+1)}this._editGeometry.notifyChanges({operation:r,addedVertices:[this._addedVertex],addedParts:this._partAdded?[d]:void 0})}undo(){const{_addedVertex:e,_part:t,_left:i,_right:s,_originalSegment:r}=this;null!=e&&(t.vertices.splice(t.vertices.indexOf(e),1),null!=i&&(t.segments.splice(t.segments.indexOf(i),1),i.leftVertex.rightSegment=null),null!=s&&(t.segments.splice(t.segments.indexOf(s),1),s.rightVertex.leftSegment=null),null!=r&&(t.segments.push(r),r.leftVertex.rightSegment=r,r.rightVertex.leftSegment=r),null!=i?t.updateVertexIndex(i.leftVertex,i.leftVertex.index):t.updateVertexIndex(e,0),this._partAdded&&this._editGeometry.parts.splice(this._editGeometry.parts.indexOf(this._part)),this._editGeometry.notifyChanges({operation:"undo",removedVertices:[e],removedParts:this._partAdded?[t]:void 0}))}accumulate(){return!1}}export{r as AppendVertex};
