import * as Core from "@intelligentgraphics/ig.gfx.core";
import { LinearAlgebra, MathTypes, Vector, MathUtils, } from "@intelligentgraphics/ig.gfx.core";
import { WallUtilities } from "../WallUtilities";
/** Class for calculation of wall points in SnapMatches. */
var WallPointCalculator = /** @class */ (function () {
    /**
     * The constructor
     * @param wallId
     * @param componentId
     */
    function WallPointCalculator(wallId, componentId, desiredComponentTransformation) {
        this.wallId = wallId;
        this.componentId = componentId;
        this.init(desiredComponentTransformation);
    }
    /**
     * Get the point at the wall
     * @returns {Core.Point}
     */
    WallPointCalculator.prototype.getWallPoint = function () {
        return this.wallPoint;
    };
    /**
     * Get the point at the object.
     * @returns {Core.Point}
     */
    WallPointCalculator.prototype.getComponentPoint = function () {
        return this.componentPoint;
    };
    /**
     * Calculate the component and wall point.
     * The point on the object is chosen to be the middle of the x-Bounds,
     * so that the back of the object will be snapped to the wall.
     * The point on the wall is chosen to be the point that is closest to this, i.e. the projection to the wall plane.
     */
    WallPointCalculator.prototype.init = function (desiredComponentTransformation) {
        var wallTransformation = Core.getWorldTransformation(this.wallId);
        var bounds = Core.State.API.GetBounds(this.componentId, "Local", true);
        var halfLength = (bounds.max.x - bounds.min.x) / 2;
        var halfLengthVec = Vector.CreateVec3(halfLength, 0, 0);
        var zeroVec = Vector.CreateVec3();
        var closestPoint = new Core.Point(undefined, undefined, "Snap", halfLengthVec, zeroVec);
        var componentTrafo;
        if (desiredComponentTransformation) {
            componentTrafo = desiredComponentTransformation;
        }
        else {
            componentTrafo = Core.getWorldTransformation(this.componentId);
        }
        // get point into world space from local space
        closestPoint.ConvertForObject(componentTrafo.position, componentTrafo.rotation.y);
        this.componentPoint = closestPoint;
        var frontFace = WallUtilities.faces(this.wallId).front;
        var wallPlane = MathTypes.convertPlaneSegmentToPlane(frontFace);
        var wallPoint = LinearAlgebra.ProjectToPlane(closestPoint.transformedPoint, wallPlane);
        var wallPointRotation = {
            x: MathUtils.radToDeg(wallTransformation.rotation.x),
            y: MathUtils.radToDeg(wallTransformation.rotation.y),
            z: MathUtils.radToDeg(wallTransformation.rotation.z),
        };
        this.wallPoint = new Core.Point(undefined, undefined, "Snap", closestPoint.point, wallPointRotation, true);
        this.wallPoint.transformedPoint = wallPoint;
        this.wallPoint.enabled = true;
    };
    return WallPointCalculator;
}());
export default WallPointCalculator;
