import { __read } from "tslib";
import _ from "lodash";
import * as IG_GFX_MulitArticle from "@intelligentgraphics/ig.gfx.multiarticle";
import * as Associates from "./Associates";
import { StrongChildMatch } from "./Interactor/SnapMatches";
import SnapPoints from "./Interactor/SnapPoints";
import { cartesianProduct } from "./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({ 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 = _.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({ id: childId }, true).strongChildPoints.filter(function (point) { return point.type === "Origin"; });
        var matches = cartesianProduct(childPoints, this.parentPoints).map(function (_a) {
            var _b = __read(_a, 2), childPoint = _b[0], parentPoint = _b[1];
            return new StrongChildMatch(childPoint, parentPoint, childId, _this.parentId);
        });
        return _.first(_.orderBy(matches, function (m) { return m.distance; }, "asc"));
    };
    return ChildActionExecutor;
}());
export { ChildActionExecutor };
