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

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

    let x0 = Infinity;
    let y0 = Infinity;

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

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

        x0 = min2(x, x0);
        y0 = min2(y, y0);
        x1 = max2(x, x1);
        y1 = max2(y, y1);
    }

    // read out
    aabb2_array_set(result, 0, x0, y0, x1, y1);
}
