import { __extends, __read, __spread } from "tslib";
import * as Core from "@intelligentgraphics/ig.gfx.core";
import { LinearAlgebra } from "@intelligentgraphics/ig.gfx.core";
import { SnapMode } from "../../Types";
import { WallUtilities } from "../../WallUtilities";
import { WeakSnapMatch } from "./WeakSnapMatch";
var WallMatch = /** @class */ (function (_super) {
    __extends(WallMatch, _super);
    function WallMatch(first, second, firstId, secondId, firstPoints) {
        var _this = _super.call(this, first, second, firstId, secondId) || this;
        _this.snapMode = SnapMode.Wall;
        _this.calculateDistance(secondId, firstPoints);
        _this.rotationDifference = Core.MathUtils.degToRad(second.rotation.y - first.rotation.y);
        _this.isPointCloseToWall = WallUtilities.isPointCloseToWall(second.transformedPoint, secondId);
        if (_this.rotationDifference < -1.5 * Math.PI) {
            _this.rotationDifference += 2 * Math.PI;
        }
        else if (_this.rotationDifference > 1.5 * Math.PI) {
            _this.rotationDifference -= 2 * Math.PI;
        }
        _this.canIntersectSnappedTo = Core.Category.hasCategory(firstId, Core.Category.CuttingThroughWall);
        _this.isWallComponent = _this.canIntersectSnappedTo;
        return _this;
    }
    WallMatch.prototype.isValid = function () {
        var isCloseEnough = this.distance <= this.maxDistance();
        return this.isCoreMatch && this.isPointCloseToWall && isCloseEnough;
    };
    /**
     * Get the maximum distance for a wall match to be active.
     * For wall components, this is larger than for normal objects.
     * @returns {number}
     */
    WallMatch.prototype.maxDistance = function () {
        return this.isWallComponent
            ? WallMatch.MaxDistanceCuttingThroughWall
            : WallMatch.MaxDistanceNotCuttingThroughWall;
    };
    WallMatch.prototype.calculateDistance = function (wallId, firstPoints) {
        // only calculate distance to front
        var distances = firstPoints.map(function (pt) {
            return LinearAlgebra.DistanceToPlaneSegment(WallUtilities.faces(wallId).front, pt.transformedPoint);
        });
        this.distance = Math.min.apply(Math, __spread(distances));
    };
    WallMatch.MaxDistanceCuttingThroughWall = 0.8;
    WallMatch.MaxDistanceNotCuttingThroughWall = 0.4;
    return WallMatch;
}(WeakSnapMatch));
export { WallMatch };
