import { v3_dot } from "../../vec3/v3_dot.js";

/**
 * Compose an inverse 4x4 transformation matrix from 3x3 rotation matrix and a translation vector
 * @param {number[]} output 4x4 matrix
 * @param {number[]} rotation_matrix 3x3 mat
 * @param {number[]} translation 3d vector
 * @returns {number[]} `output` parameter
 * @see m4_rotation_translation
 */
export function m4_inverse_rotation_translation(
    output,
    rotation_matrix,
    translation
) {

    const r = rotation_matrix;


    output[0] = r[0];
    output[1] = r[3];
    output[2] = r[6];
    output[3] = 0;

    output[4] = r[1];
    output[5] = r[4];
    output[6] = r[7];
    output[7] = 0;

    output[8] = r[2];
    output[9] = r[5];
    output[10] = r[8];
    output[11] = 0;

    const t = translation;

    output[12] = -v3_dot(t[0], t[1], t[2], r[0], r[1], r[2]);
    output[13] = -v3_dot(t[0], t[1], t[2], r[3], r[4], r[5]);
    output[14] = -v3_dot(t[0], t[1], t[2], r[6], r[7], r[8]);
    output[15] = 1;

    return output;
}