/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{LineSegment as e}from"../EditGeometry.js";class t{constructor(e,t,r=0){this._editGeometry=e,this._vertices=t,this._minNumberOfVertices=r,this.removedVertices=null}apply(){let e="redo";if(null==this.removedVertices){const t=this.removedVertices=[];this._vertices.forEach(e=>{const r=this._removeVertex(e);null!=r&&t.push(r)}),e="apply"}else this.removedVertices.forEach(e=>{this._removeVertex(e.removedVertex)});this._editGeometry.notifyChanges({operation:e,removedVertices:this._vertices})}undo(){this.removedVertices?.forEach(e=>{this._undoRemoveVertex(e)}),this._editGeometry.notifyChanges({operation:"undo",addedVertices:this._vertices})}accumulate(){return!1}_removeVertex(t){const r=t.part;if(r.vertices.length<=this._minNumberOfVertices)return null;const s={removedVertex:t,createdSegment:null},i=t.leftSegment,n=t.rightSegment;return r.vertices.splice(r.vertices.indexOf(t),1),i&&(r.segments.splice(r.segments.indexOf(i),1),i.leftVertex.rightSegment=null),n&&(r.segments.splice(r.segments.indexOf(n),1),n.rightVertex.leftSegment=null),0===t.index&&n&&this._vertices.length>0&&r.swapVertices(r.vertices.indexOf(n.rightVertex),0),i&&n&&(s.createdSegment=new e(r,i.leftVertex,n.rightVertex),r.segments.push(s.createdSegment)),n&&r.updateVertexIndex(n.rightVertex,n.rightVertex.index-1),s}_undoRemoveVertex(e){const t=e.removedVertex,r=e.removedVertex.part,s=t.leftSegment,i=t.rightSegment;e.createdSegment&&r.segments.splice(r.segments.indexOf(e.createdSegment),1),r.vertices.push(t),s&&(r.segments.push(s),s.leftVertex.rightSegment=s),i&&(r.segments.push(i),i.rightVertex.leftSegment=i),r.updateVertexIndex(t,t.index)}}export{t as RemoveVertices};
