"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var ig_utilities_1 = require("@intelligentgraphics/ig.utilities");
/**
 * Groups articles by their sourceId
 *
 * @param {GenericInstanceList} articles
 * @returns
 */
exports.groupArticlesBySourceId = function (articles) {
    var e_1, _a;
    var groups = {};
    try {
        for (var articles_1 = tslib_1.__values(articles), articles_1_1 = articles_1.next(); !articles_1_1.done; articles_1_1 = articles_1.next()) {
            var entry = articles_1_1.value;
            var targetGroup = void 0;
            if (!(targetGroup = groups[entry.source])) {
                targetGroup = groups[entry.source] = [];
            }
            targetGroup.push(entry);
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (articles_1_1 && !articles_1_1.done && (_a = articles_1.return)) _a.call(articles_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return groups;
};
exports.collectArticleData = function (session, comIds) {
    var e_2, _a;
    var result = {};
    try {
        for (var comIds_1 = tslib_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;
            if (result[comId] === undefined) {
                result[comId] = getItemData(session, comId);
            }
        }
    }
    catch (e_2_1) { e_2 = { error: e_2_1 }; }
    finally {
        try {
            if (comIds_1_1 && !comIds_1_1.done && (_a = comIds_1.return)) _a.call(comIds_1);
        }
        finally { if (e_2) throw e_2.error; }
    }
    return result;
};
var getItemData = function (session, comId) {
    var originalItem = session.getArticleByComId(comId);
    if (originalItem === undefined) {
        throw new Error("Can not get article data of unknown article: " + comId);
    }
    var properties = originalItem.properties
        .map(function (property) {
        if (property.Visible === false ||
            property.ParentPropertyKey !== undefined) {
            return undefined;
        }
        var clone = ig_utilities_1.Obj.deepClone(property);
        clone.Visible = true;
        clone.Values = clone.Values.filter(function (value) { return value.Visible !== false; });
        return clone;
    })
        .filter(ig_utilities_1.isValue);
    return tslib_1.__assign(tslib_1.__assign({}, originalItem), { properties: properties });
};
