import { assert } from "../../../assert.js";
import { max2 } from "../../../math/max2.js";
import { min2 } from "../../../math/min2.js";
import { aabb3_array_set } from "./aabb3_array_set.js";

/**
 *
 * @param {ArrayLike<number>|number[]|Float32Array|AABB3} result
 * @param {number[]|ArrayLike<number>|Float32Array} input
 * @param {number} input_length length of the input array
 */
export function aabb3_from_v3_array(result, input, input_length) {
    assert.equal(input_length % 3, 0, `Input must be divisible by 3, instead was ${input_length}`)

    let x0 = Infinity;
    let y0 = Infinity;
    let z0 = Infinity;

    let x1 = -Infinity;
    let y1 = -Infinity;
    let z1 = -Infinity;

    for (let i = 0; i < input_length; i += 3) {
        const x = input[i];
        const y = input[i + 1];
        const z = input[i + 2];

        x0 = min2(x, x0);
        y0 = min2(y, y0);
        z0 = min2(z, z0);

        x1 = max2(x, x1);
        y1 = max2(y, y1);
        z1 = max2(z, z1);
    }

    // read out
    aabb3_array_set(result, 0, x0, y0, z0, x1, y1, z1);
}
