import { Obj, isValue } from "@intelligentgraphics/ig.utilities";
/**
 * Groups articles by their sourceId
 *
 * @param {GenericInstanceList} articles
 * @returns
 */
export const groupArticlesBySourceId = (articles) => {
    const groups = {};
    for (const entry of articles) {
        let targetGroup;
        if (!(targetGroup = groups[entry.source])) {
            targetGroup = groups[entry.source] = [];
        }
        targetGroup.push(entry);
    }
    return groups;
};
export const collectArticleData = (session, comIds) => {
    const result = {};
    for (const comId of comIds) {
        if (result[comId] === undefined) {
            result[comId] = getItemData(session, comId);
        }
    }
    return result;
};
const getItemData = (session, comId) => {
    const originalItem = session.getArticleByComId(comId);
    if (originalItem === undefined) {
        throw new Error(`Can not get article data of unknown article: ${comId}`);
    }
    const properties = originalItem.properties
        .map(property => {
        if (property.Visible === false ||
            property.ParentPropertyKey !== undefined) {
            return undefined;
        }
        const clone = Obj.deepClone(property);
        clone.Visible = true;
        clone.Values = clone.Values.filter(value => value.Visible !== false);
        return clone;
    })
        .filter(isValue);
    return {
        ...originalItem,
        properties,
    };
};
