"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var igd_core_1 = require("@intelligentgraphics/igd.core");
exports.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 tslib_1.__awaiter(void 0, void 0, void 0, function () {
        var comId, articleType, subArticles;
        return tslib_1.__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*/, tslib_1.__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[igd_core_1.IGXA.EItemType.ArticleGfx] = "graphical",
    _a[igd_core_1.IGXA.EItemType.ArticleCom] = "commercial",
    _a[igd_core_1.IGXA.EItemType.Impression] = "impression",
    _a[igd_core_1.IGXA.EItemType.Set] = "set",
    _a[igd_core_1.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;
};
