import { __assign, __awaiter, __generator, __read, __spread } from "tslib";
import { IGXA } from "@intelligentgraphics/igd.core";
import { executeCreateArticleOperation } from "./Creation";
export var executeRestoreOperation = function (article, articleQuery, productDataManager) { return __awaiter(void 0, void 0, void 0, function () {
    var articleDoesExist, subArticles;
    return __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*/, __spread([
                        __assign(__assign({}, article), { articleType: article.type, type: "create" })
                    ], subArticles.map(function (childComId) { return ({
                        type: "create",
                        comId: childComId,
                        parent: article.comId,
                    }); }))];
            case 3: return [2 /*return*/, executeCreateArticleOperation({
                    source: article.source,
                    articleNumber: __assign(__assign({}, article.articleNumber), { itemType: resolveArticleType(article) }),
                    initialConfiguration: article.configuration,
                    initialPlacement: article.placement,
                    data: article.data,
                    categories: article.categories,
                }, articleQuery, productDataManager)];
        }
    });
}); };
export var executeRestoreSessionProviderOperation = function (data, sessionProvider) { return __awaiter(void 0, void 0, void 0, function () {
    var comIds;
    return __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 IGXA.EItemType.Impression;
        case "set":
            return IGXA.EItemType.Set;
        case "headItem":
            return IGXA.EItemType.HeadData;
    }
    return IGXA.EItemType.ArticleGfx;
};
