import { assert } from "../../../../assert.js";

/**
 * Represents cavity or "ball" formed when carving out "triangles" to accommodate inserted point
 */
export class Cavity {
    /**
     *
     * @type {number[]}
     */
    __deleted = [];

    /**
     *
     * @type {number}
     */
    __deleted_size = 0;

    /**
     * flat array of 5-tuples
     * @type {number[]}
     * @private
     */
    __boundary = [];

    /**
     *
     * @type {number}
     * @private
     */
    __boundary_size = 0;

    /**
     * Add boundary information
     * @param {number} vta
     * @param {number} node1
     * @param {number} node2
     * @param {number} node3
     * @param {number} bnd
     */
    push_boundary(
        vta,
        node1,
        node2,
        node3,
        bnd
    ) {

        const n = this.__boundary_size++;

        const n5 = n * 5;

        const boundary = this.__boundary;

        boundary[n5] = vta;
        boundary[n5 + 1] = node1;
        boundary[n5 + 2] = node2;
        boundary[n5 + 3] = node3;
        boundary[n5 + 4] = bnd;

    }

    /**
     *
     * @param {number} i
     */
    push_deleted(i) {
        assert.equal(this.includes(i), false, `ball element ${i} already exists`);

        this.__deleted[this.__deleted_size++] = i;
    }

    /**
     * Primarily for debug
     * @param {number} i
     * @returns {boolean}
     */
    includes(i) {
        const n = this.__deleted_size;

        for (let j = 0; j < n; j++) {
            if (this.__deleted[j] === i) {
                return true;
            }
        }

        return false;
    }

    reset() {
        this.__deleted_size = 0;
        this.__boundary_size = 0;
    }
}
