import { query_vertex_in_edge } from "./query/query_vertex_in_edge.js";
import { assert } from "../../../assert.js";
import { tm_edge_kill } from "./tm_edge_kill.js";

/**
 * Splice Edge
 * Splice two unique edges which share the same two vertices into one edge
 * source onto destination, removing source
 *
 * PRECONDITION: Edges must share vertices
 * @param {TopoMesh} mesh
 * @param {TopoEdge} destination
 * @param {TopoEdge} source
 * @returns {boolean}
 */
export function tm_edge_splice(mesh, destination, source) {
    if (!query_vertex_in_edge(source, destination.v0) || !query_vertex_in_edge(source, destination.v1)) {
        // do not share vertices, can't splice

        // the caller must make sure that this never happens
        assert.ok(false);

        return false;
    }

    // move faces
    const source_faces = source.faces;
    const source_face_count = source_faces.length;
    for (let i = 0; i < source_face_count; i++) {
        const a = source_faces.pop();

        a.removeEdge(source);

        a.addUniqueEdge(destination);
        destination.addUniqueFace(a);
    }


    tm_edge_kill(mesh, source);

    return true;
}
