"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Types_1 = require("../../Types");
var SnapMatch_1 = require("./SnapMatch");
var ig_gfx_core_1 = require("@intelligentgraphics/ig.gfx.core");
/** Class representing a strong snap match, i.e. a match of two given snap points. */
var StrongNeighborMatch = /** @class */ (function (_super) {
    tslib_1.__extends(StrongNeighborMatch, _super);
    /**
     * The constructor of the StrongNeighborMatch class. Calculates if the two points given are a core match
     * @param first the first point
     * @param second the second point
     * @param firstId the first id
     * @param secondId the second id
     */
    function StrongNeighborMatch(first, second, firstId, secondId) {
        var _this = _super.call(this, first, second, firstId, secondId) || this;
        _this.snapMode = Types_1.SnapMode.Strong;
        _this.isCoreMatch = StrongNeighborMatch.isCoreMatch(first, second);
        _this.isTopBottom = first.type === "Bottom" && second.type === "Top";
        return _this;
    }
    /**
     * Check if the two points are a core match, e.g. left <-> right, top <-> bottom. Also checks if the regexes match.
     * @param first the first point
     * @param second the second point
     */
    StrongNeighborMatch.isCoreMatch = function (first, second) {
        if ((first.type === "Left" && second.type === "Right") ||
            (first.type === "Right" && second.type === "Left") ||
            (first.type === "Top" && second.type === "Bottom") ||
            (first.type === "Bottom" && second.type === "Top") ||
            (first.type === "Front" && second.type === "Back") ||
            (first.type === "Back" && second.type === "Front") ||
            (first.type === "Snap" && second.type === "Snap")) {
            var tRegExp = ig_gfx_core_1.CoreUtils.createSaneRegExp(first.foreign || "");
            if (tRegExp.test(second.key) === false) {
                tRegExp = ig_gfx_core_1.CoreUtils.createSaneRegExp(second.foreign || "");
                if (tRegExp.test(first.key) === false) {
                    return false;
                }
            }
            return true;
        }
        return false;
    };
    /**
     * Checks if the points are a core match and if the distance and rotation differences are small enough for the match to be valid
     */
    StrongNeighborMatch.prototype.isValid = function () {
        if (!this.isCoreMatch) {
            return false;
        }
        if (!this.isTopBottom && this.distance > SnapMatch_1.SnapMatch.maxDistance) {
            return false;
        }
        if (this.isTopBottom) {
            if (this.xzDistance > SnapMatch_1.SnapMatch.maxDistance) {
                return false;
            }
        }
        if (Math.abs(this.rotationDifference) > SnapMatch_1.SnapMatch.maxRotation) {
            return false;
        }
        return true;
    };
    return StrongNeighborMatch;
}(SnapMatch_1.SnapMatch));
exports.StrongNeighborMatch = StrongNeighborMatch;
