"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Core = tslib_1.__importStar(require("@intelligentgraphics/ig.gfx.core"));
var ig_gfx_threejs_1 = require("@intelligentgraphics/ig.gfx.threejs");
var Associates = tslib_1.__importStar(require("./Associates"));
/**
 * Cartesian Product of two arrays, e.g. all possible tuples with first element from a and second element from b.
 */
function cartesianProduct(a, b) {
    return [].concat.apply([], tslib_1.__spread(a.map(function (a2) { return b.map(function (b2) { return [].concat(a2, b2); }); })));
}
exports.cartesianProduct = cartesianProduct;
/**
 * Calculate the middle point as the weighted average of all moving components
 * @param componentIds The IDs of all relevant components
 */
function calculateMiddlePointFromAllBoundingBoxes(componentIds) {
    var e_1, _a;
    // remove all children of which parents are selected
    var necessaryIds = componentIds.filter(function (id, index, array) { return array.indexOf(Associates.parent(id)) === -1; });
    var worldBounds = necessaryIds.map(function (id) {
        var abb = new ig_gfx_threejs_1.AbstractBoundingBox();
        abb.setFromComponent(id);
        return abb.getAxisAlignedBoundingBox();
    });
    var globalBounds = Core.createBounds();
    try {
        for (var worldBounds_1 = tslib_1.__values(worldBounds), worldBounds_1_1 = worldBounds_1.next(); !worldBounds_1_1.done; worldBounds_1_1 = worldBounds_1.next()) {
            var bound = worldBounds_1_1.value;
            globalBounds = Core.mergeBounds(globalBounds, bound);
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (worldBounds_1_1 && !worldBounds_1_1.done && (_a = worldBounds_1.return)) _a.call(worldBounds_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    var result = Core.getBoundsCenter(globalBounds);
    return result;
}
exports.calculateMiddlePointFromAllBoundingBoxes = calculateMiddlePointFromAllBoundingBoxes;
