import { __values } from "tslib";
/**
 * Filters input articles.
 *
 * - Removes duplicates
 * - Replaced comIds with their rule parent
 *
 * @param {Session} session
 * @param {string[]} comIds
 * @param {Rule} rule
 * @returns {string[]}
 */
export var execute = function (session, comIds, rule) {
    var e_1, _a;
    var result = new Set(comIds);
    try {
        for (var comIds_1 = __values(comIds), comIds_1_1 = comIds_1.next(); !comIds_1_1.done; comIds_1_1 = comIds_1.next()) {
            var comId = comIds_1_1.value;
            var article = session.getArticleByComId(comId);
            if (article === undefined ||
                article.type === "impression" ||
                article.parent === undefined ||
                !hasRule(article, rule)) {
                continue;
            }
            var ruleParent = session.getFirstArticleParent(article.comId, function (article) { return !hasRule(article, rule); });
            if (ruleParent === undefined) {
                continue;
            }
            result.add(ruleParent.comId);
            result.delete(comId);
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (comIds_1_1 && !comIds_1_1.done && (_a = comIds_1.return)) _a.call(comIds_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return Array.from(result);
};
var ruleCategoryMap = {
    configurable: "onlyConfigurableThroughParent",
    removeable: "onlyRemoveableThroughParent",
    manipulable: "onlyGroupManipulable",
};
export var hasRule = function (article, rule) {
    return article.categories[ruleCategoryMap[rule]] === true;
};
