import { NULL_POINTER } from "../BinaryTopology.js";

/**
 * Removes a loop from an edge's radial cycle.
 * @param {BinaryTopology} mesh
 * @param {number} loop Loop ID
 * @param {number} edge Edge ID
 */
export function bt_radial_loop_remove(mesh, loop, edge) {
    const r_prev = mesh.loop_read_radial_prev(loop);
    const r_next = mesh.loop_read_radial_next(loop);

    // Bypass the loop in the doubly linked list
    mesh.loop_write_radial_next(r_prev, r_next);
    mesh.loop_write_radial_prev(r_next, r_prev);

    // If the edge's base pointer is looking at the loop being removed
    if (mesh.edge_read_loop(edge) === loop) {
        // If it was the only loop, set to NULL. Otherwise, advance to the next loop.
        mesh.edge_write_loop(edge, r_next === loop ? NULL_POINTER : r_next);
    }

    // Reset the removed loop's radial pointers to itself for safety
    mesh.loop_write_radial_next(loop, loop);
    mesh.loop_write_radial_prev(loop, loop);
}
