import { v3_dot_array_array } from "../../vec3/v3_dot_array_array.js";
import { v3_matrix4_multiply } from "../../vec3/v3_matrix4_multiply.js";
import { v3_matrix4_rotate_normal } from "../../vec3/v3_matrix4_rotate_normal.js";
import { v3_multiply_scalar } from "../../vec3/v3_multiply_scalar.js";

const scratch_v3_0 = new Float32Array(3);

/**
 *
 * @param {number[]|Float64Array|Float32Array} destination
 * @param {number} destination_offset
 * @param {number[]|Float32Array|Float64Array} matrix
 * @param {number} x
 * @param {number} y
 * @param {number} z
 * @param {number} w
 */
export function plane3_matrix4_project(
    destination, destination_offset,
    matrix,
    x, y, z, w,
) {

    // build reference point in the new pane
    v3_multiply_scalar(scratch_v3_0, 0, x, y, z, -w);
    v3_matrix4_multiply(scratch_v3_0, 0, scratch_v3_0, 0, matrix);

    // rotate normal vector
    v3_matrix4_rotate_normal(destination, destination_offset, x, y, z, matrix);

    // figure out the new planar offset using new orientation and a reference point
    destination[destination_offset + 3] = -v3_dot_array_array(scratch_v3_0, 0, destination, destination_offset);
}