import * as Core from "@intelligentgraphics/ig.gfx.core";
import { LinearAlgebra } from "@intelligentgraphics/ig.gfx.core";
import Environment from "../Environment";
import SnapCalculator from "../Interactor/SnapCalculator";
/** Some utilities for placing items */
var PlacementUtilities = /** @class */ (function () {
    function PlacementUtilities() {
    }
    /**
     * Checks if a position is viable (i.e. no collisions with objects or walls at that point).
     * @param componentId
     * @param transformation
     * @param selectedRoots
     * @returns {boolean}
     */
    PlacementUtilities.isPositionViable = function (componentId, transformation, selectedRoots) {
        var hasFloatingCategory = Core.Category.hasCategory(componentId, Core.Category.Floating);
        if (!hasFloatingCategory && transformation.position.y > 0.01) {
            return false;
        }
        // remove the environment root, we will take a look at walls separately
        var allRoots = Core.State.API.GetRoots();
        var environmentRoot = Environment.getInstance().getRoot();
        var otherRoots = allRoots.filter(function (root) { return root !== componentId && root !== environmentRoot; });
        var hasChildCategory = Core.Category.hasCategory(componentId, Core.Category.Child);
        if (hasChildCategory) {
            // for child objects, remove the parent component
            otherRoots = otherRoots.filter(function (root) {
                if (selectedRoots && selectedRoots.indexOf(root) === -1) {
                    return true;
                }
                var calc = new SnapCalculator({
                    id: componentId,
                    transformation: transformation,
                }, true);
                var rootTrafo = Core.getWorldTransformation(root);
                var childMatches = calc.getStrongChildMatches({
                    id: root,
                    transformation: rootTrafo,
                });
                return childMatches.length === 0;
            });
        }
        var isCollisionWithObject = otherRoots.some(function (root) {
            var otherRootTrafo = Core.getWorldTransformation(root);
            var collidingInfo = LinearAlgebra.wouldComponentsCollide(componentId, root, transformation, otherRootTrafo);
            return collidingInfo && collidingInfo.isColliding;
        });
        // Wall objects are allowed to collide with one wall
        var allowedCollisionsWithWalls = Core.Category.hasCategory(componentId, Core.Category.CuttingThroughWall)
            ? 1
            : 0;
        var allWalls = Environment.getInstance().getWalls();
        var collidingWalls = allWalls.filter(function (wallId) {
            var wallTrafo = Core.getWorldTransformation(wallId);
            var collidingInfo = LinearAlgebra.wouldComponentsCollide(componentId, wallId, transformation, wallTrafo);
            return collidingInfo && collidingInfo.isColliding;
        });
        var result = !isCollisionWithObject &&
            collidingWalls.length <= allowedCollisionsWithWalls;
        return result;
    };
    return PlacementUtilities;
}());
export { PlacementUtilities };
