"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Core = tslib_1.__importStar(require("@intelligentgraphics/ig.gfx.core"));
var Environment_1 = tslib_1.__importDefault(require("../Environment"));
/** Class for placing objects to the right of the scene. Also align to the element that is placed frontal. */
var RightOfScenePlacement = /** @class */ (function () {
    /**
     * The constructor
     * @param id
     */
    function RightOfScenePlacement(id) {
        this.componentId = id;
    }
    RightOfScenePlacement.prototype.getTransformation = function () {
        var e_1, _a, e_2, _b;
        var tAllRoots = Core.State.API.GetRoots();
        var tEnvironmentInstanceRoot = Environment_1.default.getInstance().getRoot();
        var tOtherRoots = new Array();
        try {
            for (var tAllRoots_1 = tslib_1.__values(tAllRoots), tAllRoots_1_1 = tAllRoots_1.next(); !tAllRoots_1_1.done; tAllRoots_1_1 = tAllRoots_1.next()) {
                var tRoot = tAllRoots_1_1.value;
                if (tRoot !== this.componentId &&
                    tRoot !== tEnvironmentInstanceRoot) {
                    tOtherRoots.push(tRoot);
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (tAllRoots_1_1 && !tAllRoots_1_1.done && (_a = tAllRoots_1.return)) _a.call(tAllRoots_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        var tWallIds = Environment_1.default.getInstance().getWalls();
        tOtherRoots = tOtherRoots.concat(tWallIds);
        if (tOtherRoots.length === 0) {
            // If there are no other roots, place at the origin.
            var zeroVectorPosition = { x: 0, y: 0, z: 0 };
            var zeroVectorRotation = { x: 0, y: 0, z: 0 };
            return {
                position: zeroVectorPosition,
                rotation: zeroVectorRotation,
            };
        }
        // search for the most right element and the most front element
        var tRightXCoordinate = -Infinity;
        var tFrontZCoordinate = -Infinity;
        try {
            for (var tOtherRoots_1 = tslib_1.__values(tOtherRoots), tOtherRoots_1_1 = tOtherRoots_1.next(); !tOtherRoots_1_1.done; tOtherRoots_1_1 = tOtherRoots_1.next()) {
                var tRoot = tOtherRoots_1_1.value;
                var tWorldBounds = Core.State.API.GetBounds(tRoot, "World");
                if (tWorldBounds.max.x > tRightXCoordinate) {
                    tRightXCoordinate = tWorldBounds.max.x;
                }
                if (tWorldBounds.min.z > tFrontZCoordinate) {
                    tFrontZCoordinate = tWorldBounds.min.z;
                }
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (tOtherRoots_1_1 && !tOtherRoots_1_1.done && (_b = tOtherRoots_1.return)) _b.call(tOtherRoots_1);
            }
            finally { if (e_2) throw e_2.error; }
        }
        return {
            position: {
                x: tRightXCoordinate,
                y: 0,
                z: tFrontZCoordinate,
            },
            rotation: {
                x: 0,
                y: 0,
                z: 0,
            },
        };
    };
    return RightOfScenePlacement;
}());
exports.default = RightOfScenePlacement;
