import { assert } from "../../../../../../../assert.js";
import { NULL_POINTER } from "../../BinaryTopology.js";
import { bt_disk_edge_remove } from "../bt_disk_edge_remove.js";
import { bt_face_kill } from "../face/bt_face_kill.js";
import { bt_kill_only_edge } from "./bt_kill_only_edge.js";

/**
 * @param {BinaryTopology} mesh
 * @param {number} e
 */
export function bt_edge_kill(mesh, e) {
    assert.isNonNegativeInteger(e, 'e');

    let l = NULL_POINTER;

    while ((l = mesh.edge_read_loop(e)) !== NULL_POINTER) {
        const face = mesh.loop_read_face(l);

        bt_face_kill(mesh, face);
    }

    const v1 = mesh.edge_read_vertex1(e);
    const v2 = mesh.edge_read_vertex2(e);

    bt_disk_edge_remove(mesh, e, v1);
    if (v2 !== v1) {
        // For self-loops the edge occupies a single slot in v's disk cycle
        // (the v1 slot, per convention); the v2 slot is unused. Calling
        // bt_disk_edge_remove again with the same vertex would be idempotent
        // here, but the explicit guard documents the intent and saves a call.
        bt_disk_edge_remove(mesh, e, v2);
    }

    bt_kill_only_edge(mesh, e);
}