var _a;
import { __awaiter, __generator, __read, __spread } from "tslib";
import { IGXA } from "@intelligentgraphics/igd.core";
export var executeCreateArticleOperation = function (_a, articleQuery, productDataManager) {
    var source = _a.source, articleNumber = _a.articleNumber, initialConfiguration = _a.initialConfiguration, initialPlacement = _a.initialPlacement, data = _a.data, categories = _a.categories, gfxId = _a.gfxId;
    return __awaiter(void 0, void 0, void 0, function () {
        var comId, articleType, subArticles;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0: return [4 /*yield*/, productDataManager.addArticle(articleNumber, initialConfiguration, source)];
                case 1:
                    comId = _b.sent();
                    articleType = resolveArticleType(articleNumber);
                    return [4 /*yield*/, productDataManager
                            .getSubArticleIDs(comId)
                            .catch(function () { return []; })];
                case 2:
                    subArticles = _b.sent();
                    return [2 /*return*/, __spread([
                            {
                                type: "create",
                                comId: comId,
                                articleNumber: articleNumber,
                                categories: categories,
                                placement: initialPlacement,
                                data: data,
                                articleType: articleType,
                                children: subArticles,
                                gfxId: gfxId,
                            }
                        ], subArticles.map(function (childComId) { return ({
                            type: "create",
                            comId: childComId,
                            parent: comId,
                        }); }))];
            }
        });
    });
};
var articleTypeMap = (_a = {},
    _a[IGXA.EItemType.ArticleGfx] = "graphical",
    _a[IGXA.EItemType.ArticleCom] = "commercial",
    _a[IGXA.EItemType.Impression] = "impression",
    _a[IGXA.EItemType.Set] = "set",
    _a[IGXA.EItemType.HeadData] = "headItem",
    _a);
var resolveArticleType = function (articleNumber) {
    if (articleNumber.itemType === undefined) {
        return "graphical";
    }
    var type = articleTypeMap[articleNumber.itemType];
    if (type === undefined) {
        throw new Error("Can not create an article for unknown item type \"" + articleNumber.itemType + "\"");
    }
    return type;
};
