"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Matrix4_1 = require("./Matrix4");
var MathUtils_1 = require("./MathUtils");
exports.createEuler = function (x, y, z, order) {
    if (x === void 0) { x = 0; }
    if (y === void 0) { y = 0; }
    if (z === void 0) { z = 0; }
    if (order === void 0) { order = "xyz"; }
    return ({
        x: x,
        y: y,
        z: z,
        order: order,
    });
};
exports.createEulerFromQuaternion = function (quaternion, order) {
    if (order === void 0) { order = "xyz"; }
    var matrix = Matrix4_1.createMatrix4FromQuaternion(quaternion);
    return exports.createEulerFromRotationMatrix(matrix, order);
};
exports.createEulerFromRotationMatrix = function (matrix, order) {
    if (order === void 0) { order = "xyz"; }
    var m11 = matrix.elements[0], m12 = matrix.elements[4], m13 = matrix.elements[8];
    var m21 = matrix.elements[1], m22 = matrix.elements[5], m23 = matrix.elements[9];
    var m31 = matrix.elements[2], m32 = matrix.elements[6], m33 = matrix.elements[10];
    var result = exports.createEuler(undefined, undefined, undefined, order);
    if (order === "xyz") {
        result.y = Math.asin(MathUtils_1.clamp(m13, -1, 1));
        if (Math.abs(m13) < 0.99999) {
            result.x = Math.atan2(-m23, m33);
            result.z = Math.atan2(-m12, m11);
        }
        else {
            result.x = Math.atan2(m32, m22);
            result.z = 0;
        }
    }
    else {
        throw new Error("Unknown order");
    }
    return result;
};
