"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_core_1 = require("@intelligentgraphics/ig.gfx.core");
var Types_1 = require("../../Types");
var WallUtilities_1 = require("../../WallUtilities");
var WeakSnapMatch_1 = require("./WeakSnapMatch");
var WallMatch = /** @class */ (function (_super) {
    tslib_1.__extends(WallMatch, _super);
    function WallMatch(first, second, firstId, secondId, firstPoints) {
        var _this = _super.call(this, first, second, firstId, secondId) || this;
        _this.snapMode = Types_1.SnapMode.Wall;
        _this.calculateDistance(secondId, firstPoints);
        _this.rotationDifference = Core.MathUtils.degToRad(second.rotation.y - first.rotation.y);
        _this.isPointCloseToWall = WallUtilities_1.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 ig_gfx_core_1.LinearAlgebra.DistanceToPlaneSegment(WallUtilities_1.WallUtilities.faces(wallId).front, pt.transformedPoint);
        });
        this.distance = Math.min.apply(Math, tslib_1.__spread(distances));
    };
    WallMatch.MaxDistanceCuttingThroughWall = 0.8;
    WallMatch.MaxDistanceNotCuttingThroughWall = 0.4;
    return WallMatch;
}(WeakSnapMatch_1.WeakSnapMatch));
exports.WallMatch = WallMatch;
