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

/**
 *
 * @param {number[]|Float32Array} out
 * @param {number} out_offset
 * @param {number[]|Float32Array} mat4
 * @returns {number[]} `out` parameter
 */
export function m4_extract_scale(
    out, out_offset,
    mat4
) {
    assert.isArrayLike(out, 'out');
    assert.isNonNegativeInteger(out_offset, 'out_offset');
    assert.isArrayLike(mat4, 'mat4');

    const m11 = mat4[0];
    const m12 = mat4[1];
    const m13 = mat4[2];

    const scale_x = v3_length(m11, m12, m13);

    const m21 = mat4[4];
    const m22 = mat4[5];
    const m23 = mat4[6];

    const scale_y = v3_length(m21, m22, m23);

    const m31 = mat4[8];
    const m32 = mat4[9];
    const m33 = mat4[10];

    const scale_z = v3_length(m31, m32, m33);

    out[out_offset] = scale_x;
    out[out_offset + 1] = scale_y;
    out[out_offset + 2] = scale_z;

    return out;
}