import { isArrayEqualStrict } from "../../../../core/collection/array/isArrayEqualStrict.js";
import { is_typed_array_equals } from "../../../../core/collection/array/typed/is_typed_array_equals.js";


/**
 *
 * @param {THREE.BufferGeometry} a
 * @param {THREE.BufferGeometry} b
 * @returns {boolean}
 */
export function computeGeometryEquality(a, b) {
    if (a === b) {
        // identity shortcut
        return true;
    }

    const a_index = a.getIndex();
    const b_index = b.getIndex();

    if (a_index !== b_index) {
        if (a_index === null || b_index === null) {
            return false;
        }

        if (!is_typed_array_equals(a_index.array, b_index.array)) {
            return false;
        }
    }

    const a_attribute_keys = Object.keys(a.attributes);
    const b_attribute_keys = Object.keys(b.attributes);

    const attribute_count = a_attribute_keys.length;
    if (attribute_count !== b_attribute_keys.length) {
        return false;
    }

    // sorting keys ensures that order is not relevant
    a_attribute_keys.sort();
    b_attribute_keys.sort();


    if (!isArrayEqualStrict(a_attribute_keys, b_attribute_keys)) {
        return false;
    }

    // compare individual attributes
    for (let i = 0; i < attribute_count; i++) {
        const key = a_attribute_keys[i];

        const a_attribute = a.attributes[key];
        const b_attribute = b.attributes[key];

        if (!is_typed_array_equals(a_attribute.array, b_attribute.array)) {
            return false;
        }
    }

    return true;
}
