"use strict";
var __assign = (this && this.__assign) || Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
        s = arguments[i];
        for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
            t[p] = s[p];
    }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var computeMatrix_1 = require("./layout/computeMatrix");
var computeOrientationFlexibly_1 = require("./layout/computeOrientationFlexibly");
var computeSize_1 = require("./layout/computeSize");
var ELEMENTS_2D = {
    circle: true,
    ellipse: true,
    foreignObject: true,
    g: true,
    image: true,
    line: true,
    mesh: true,
    path: true,
    polygon: true,
    polyline: true,
    rect: true,
    switch: true,
    symbol: true,
    text: true,
    textPath: true,
    tspan: true,
    unknown: true,
    use: true,
};
var SIZE_PROPORTIONAL = 0;
var SIZE_ABSOLUTE = 1;
var DEFAULT_DEPTH = 0;
var IDENTITY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
var FORMATS = {
    THREE: 3,
    TWO: 2,
};
function initializeNodeAttributes(element) {
    if (!element.attributes) {
        element.attributes = {};
    }
    if (!element.attributes.style) {
        element.attributes.style = {};
    }
    if (!element.layout) {
        element.layout = createLayoutSpec(null, null, null);
        element.layout.matrix = createMatrix();
        element.layout.format = ELEMENTS_2D[element.elementName]
            ? FORMATS.TWO
            : FORMATS.THREE;
    }
    return element;
}
function initializeTreeAttributes(tree, container) {
    if (!tree || typeof tree === 'string') {
        return;
    }
    initializeNodeAttributes(tree);
    tree.__parent = container;
    if (!tree.children || tree.children.length < 1) {
        return;
    }
    for (var i = 0; i < tree.children.length; i++) {
        initializeTreeAttributes(tree.children[i], tree);
    }
}
function createLayoutSpec(ax, ay, az) {
    return {
        shown: true,
        opacity: 1.0,
        mount: { x: ax || 0, y: ay || 0, z: az || 0 },
        align: { x: ax || 0, y: ay || 0, z: az || 0 },
        origin: { x: ax || 0, y: ay || 0, z: az || 0 },
        translation: { x: 0, y: 0, z: 0 },
        rotation: { x: 0, y: 0, z: 0, w: 0 },
        orientation: { x: 0, y: 0, z: 0, w: 0 },
        scale: { x: 1, y: 1, z: 1 },
        sizeMode: {
            x: SIZE_PROPORTIONAL,
            y: SIZE_PROPORTIONAL,
            z: SIZE_PROPORTIONAL,
        },
        sizeProportional: { x: 1, y: 1, z: 1 },
        sizeDifferential: { x: 0, y: 0, z: 0 },
        sizeAbsolute: { x: 0, y: 0, z: 0 },
    };
}
function createMatrix() {
    return copyMatrix(IDENTITY);
}
function copyMatrix(m) {
    return m.slice();
}
function multiplyMatrices(a, b) {
    return [
        a[0] * b[0] + a[1] * b[4] + a[2] * b[8] + a[3] * b[12],
        a[0] * b[1] + a[1] * b[5] + a[2] * b[9] + a[3] * b[13],
        a[0] * b[2] + a[1] * b[6] + a[2] * b[10] + a[3] * b[14],
        a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3] * b[15],
        a[4] * b[0] + a[5] * b[4] + a[6] * b[8] + a[7] * b[12],
        a[4] * b[1] + a[5] * b[5] + a[6] * b[9] + a[7] * b[13],
        a[4] * b[2] + a[5] * b[6] + a[6] * b[10] + a[7] * b[14],
        a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7] * b[15],
        a[8] * b[0] + a[9] * b[4] + a[10] * b[8] + a[11] * b[12],
        a[8] * b[1] + a[9] * b[5] + a[10] * b[9] + a[11] * b[13],
        a[8] * b[2] + a[9] * b[6] + a[10] * b[10] + a[11] * b[14],
        a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11] * b[15],
        a[12] * b[0] + a[13] * b[4] + a[14] * b[8] + a[15] * b[12],
        a[12] * b[1] + a[13] * b[5] + a[14] * b[9] + a[15] * b[13],
        a[12] * b[2] + a[13] * b[6] + a[14] * b[10] + a[15] * b[14],
        a[12] * b[3] + a[13] * b[7] + a[14] * b[11] + a[15] * b[15],
    ];
}
function multiplyArrayOfMatrices(arrayOfMatrices) {
    var product = createMatrix();
    for (var i = 0; i < arrayOfMatrices.length; i++) {
        product = multiplyMatrices(product, arrayOfMatrices[i]);
    }
    return product;
}
function computeLayout(layoutSpec, currentMatrix, parentsizeAbsoluteIn) {
    delete layoutSpec.computed;
    var parentsizeAbsolute = parentsizeAbsoluteIn || { x: 0, y: 0, z: 0 };
    if (parentsizeAbsolute.z === undefined || parentsizeAbsolute.z === null) {
        parentsizeAbsolute.z = DEFAULT_DEPTH;
    }
    var size = computeSize_1.default(layoutSpec, layoutSpec.sizeMode, parentsizeAbsolute);
    return __assign({}, layoutSpec, { size: size, matrix: computeMatrix_1.default(layoutSpec, currentMatrix, size, parentsizeAbsolute) });
}
exports.default = {
    multiplyArrayOfMatrices: multiplyArrayOfMatrices,
    computeLayout: computeLayout,
    createLayoutSpec: createLayoutSpec,
    computeOrientationFlexibly: computeOrientationFlexibly_1.default,
    createMatrix: createMatrix,
    multiplyMatrices: multiplyMatrices,
    copyMatrix: copyMatrix,
    initializeTreeAttributes: initializeTreeAttributes,
    FORMATS: FORMATS,
    SIZE_ABSOLUTE: SIZE_ABSOLUTE,
    SIZE_PROPORTIONAL: SIZE_PROPORTIONAL,
    ATTRIBUTES: createLayoutSpec(null, null, null),
};
//# sourceMappingURL=Layout3D.js.map