/**
 * Filters input articles.
 *
 * - Removes duplicates
 * - Replaced comIds with their rule parent
 *
 * @param {Session} session
 * @param {string[]} comIds
 * @param {Rule} rule
 * @returns {string[]}
 */
export const execute = (session, comIds, rule) => {
    const result = new Set(comIds);
    for (const comId of comIds) {
        const article = session.getArticleByComId(comId);
        if (article === undefined ||
            article.type === "impression" ||
            article.parent === undefined ||
            !hasRule(article, rule)) {
            continue;
        }
        const ruleParent = session.getFirstArticleParent(article.comId, article => !hasRule(article, rule));
        if (ruleParent === undefined) {
            continue;
        }
        result.add(ruleParent.comId);
        result.delete(comId);
    }
    return Array.from(result);
};
const ruleCategoryMap = {
    configurable: "onlyConfigurableThroughParent",
    removeable: "onlyRemoveableThroughParent",
    manipulable: "onlyGroupManipulable",
};
export const hasRule = (article, rule) => article.categories[ruleCategoryMap[rule]] === true;
