"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var lodash_1 = tslib_1.__importDefault(require("lodash"));
var IG_GFX_MulitArticle = tslib_1.__importStar(require("@intelligentgraphics/ig.gfx.multiarticle"));
var Associates = tslib_1.__importStar(require("./Associates"));
var SnapMatches_1 = require("./Interactor/SnapMatches");
var SnapPoints_1 = tslib_1.__importDefault(require("./Interactor/SnapPoints"));
var Util_1 = require("./Util");
/**
 * Class for executing actions on child elements.
 * Useful to execute actions of a child element if the parent element has changed.
 */
var ChildActionExecutor = /** @class */ (function () {
    function ChildActionExecutor(parentId) {
        this.parentId = parentId;
        this.childrenIds = Associates.getChildren([parentId], false);
        this.parentPoints = new SnapPoints_1.default({ id: this.parentId }, true).strongChildPoints.filter(function (point) { return point.type === "Child"; });
    }
    ChildActionExecutor.prototype.executeActions = function () {
        var _this = this;
        this.childrenIds
            .map(function (id) { return _this.matchForChild(id); })
            .filter(function (match) { return match; })
            .forEach(function (match) {
            var actions = lodash_1.default.flatMap(match.getActions(), function (a) { return a.Rules; });
            IG_GFX_MulitArticle.Rule.executeActions(actions, match.firstId, match.secondId);
        });
    };
    ChildActionExecutor.prototype.matchForChild = function (childId) {
        var _this = this;
        var childPoints = new SnapPoints_1.default({ id: childId }, true).strongChildPoints.filter(function (point) { return point.type === "Origin"; });
        var matches = Util_1.cartesianProduct(childPoints, this.parentPoints).map(function (_a) {
            var _b = tslib_1.__read(_a, 2), childPoint = _b[0], parentPoint = _b[1];
            return new SnapMatches_1.StrongChildMatch(childPoint, parentPoint, childId, _this.parentId);
        });
        return lodash_1.default.first(lodash_1.default.orderBy(matches, function (m) { return m.distance; }, "asc"));
    };
    return ChildActionExecutor;
}());
exports.ChildActionExecutor = ChildActionExecutor;
