import { __values } from "tslib";
import * as Core from "@intelligentgraphics/ig.gfx.core";
import { Rule } from "./Rule";
/** Class for handling niche related actions, e.g. finding niches, making them visible / invisible. */
var Niche = /** @class */ (function () {
    function Niche(nicheSubrootId, nicheIdentifier, rootId) {
        this.nicheSubrootId = nicheSubrootId;
        this.nicheIdentifier = nicheIdentifier;
        this.rootId = rootId;
    }
    Niche.prototype.actions = function (multiArticles) { };
    Niche.prototype.attachPoints = function (multiArticles, rootConfiguration) {
        var e_1, _a;
        if (!multiArticles) {
            return [];
        }
        var result = new Array();
        var multiArticleId = this.multiArticlecId();
        var attachPoints = multiArticles.getAttachPoints(multiArticleId);
        try {
            for (var attachPoints_1 = __values(attachPoints), attachPoints_1_1 = attachPoints_1.next(); !attachPoints_1_1.done; attachPoints_1_1 = attachPoints_1.next()) {
                var ap = attachPoints_1_1.value;
                var evaluatedAP = Rule.evalAttachPoint(ap, rootConfiguration);
                if (evaluatedAP) {
                    result.push(evaluatedAP);
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (attachPoints_1_1 && !attachPoints_1_1.done && (_a = attachPoints_1.return)) _a.call(attachPoints_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return result;
    };
    Niche.prototype.multiArticlecId = function () {
        return "@NICHE_" + this.nicheIdentifier;
    };
    Niche.prototype.getComponentId = function () {
        return this.nicheSubrootId;
    };
    Niche.prototype.makeVisible = function () {
        Core.State.API.SetVisibility(this.nicheSubrootId, true);
    };
    Niche.prototype.makeInvisible = function () {
        Core.State.API.SetVisibility(this.nicheSubrootId, false);
    };
    Niche.allNiches = function (componentId) {
        var component = Core.State.API.GetComponent(componentId);
        if (!component) {
            return [];
        }
        var niches = [];
        Core.State.API.TraverseComponent(componentId, function (subId) {
            var layer = Core.State.API.GetLayer(subId);
            if (layer === null) {
                return;
            }
            var match = layer.match(Niche.nicheRegex);
            if (match === null) {
                return;
            }
            niches.push(new Niche(subId, match[1], componentId));
        });
        return niches;
    };
    Niche.nicheRegex = /NICHE_([a-zA-Z0-9]*)_/;
    return Niche;
}());
export default Niche;
