"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var igd_core_1 = require("@intelligentgraphics/igd.core");
var Creation_1 = require("./Creation");
exports.executeRestoreOperation = function (article, articleQuery, productDataManager) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
    var articleDoesExist, subArticles;
    return tslib_1.__generator(this, function (_a) {
        switch (_a.label) {
            case 0: return [4 /*yield*/, productDataManager
                    .getArticleID(article.comId)
                    .catch(function () { return false; })];
            case 1:
                articleDoesExist = _a.sent();
                if (!articleDoesExist) return [3 /*break*/, 3];
                return [4 /*yield*/, productDataManager
                        .getSubArticleIDs(article.comId)
                        .catch(function () { return []; })];
            case 2:
                subArticles = _a.sent();
                return [2 /*return*/, tslib_1.__spread([
                        tslib_1.__assign(tslib_1.__assign({}, article), { articleType: article.type, type: "create" })
                    ], subArticles.map(function (childComId) { return ({
                        type: "create",
                        comId: childComId,
                        parent: article.comId,
                    }); }))];
            case 3: return [2 /*return*/, Creation_1.executeCreateArticleOperation({
                    source: article.source,
                    articleNumber: tslib_1.__assign(tslib_1.__assign({}, article.articleNumber), { itemType: resolveArticleType(article) }),
                    initialConfiguration: article.configuration,
                    initialPlacement: article.placement,
                    data: article.data,
                    categories: article.categories,
                }, articleQuery, productDataManager)];
        }
    });
}); };
exports.executeRestoreSessionProviderOperation = function (data, sessionProvider) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
    var comIds;
    return tslib_1.__generator(this, function (_a) {
        switch (_a.label) {
            case 0: return [4 /*yield*/, sessionProvider.restore(data)];
            case 1:
                comIds = _a.sent();
                return [2 /*return*/, comIds.map(function (comId) { return ({
                        type: "create",
                        comId: comId,
                    }); })];
        }
    });
}); };
var resolveArticleType = function (article) {
    switch (article.type) {
        case "impression":
            return igd_core_1.IGXA.EItemType.Impression;
        case "set":
            return igd_core_1.IGXA.EItemType.Set;
        case "headItem":
            return igd_core_1.IGXA.EItemType.HeadData;
    }
    return igd_core_1.IGXA.EItemType.ArticleGfx;
};
