import { IGXA } from "@intelligentgraphics/igd.core";
export const executeCreateArticleOperation = async ({ source, articleNumber, initialConfiguration, initialPlacement, data, categories, gfxId, }, articleQuery, productDataManager) => {
    const comId = await productDataManager.addArticle(articleNumber, initialConfiguration, source);
    const articleType = resolveArticleType(articleNumber);
    const subArticles = await productDataManager
        .getSubArticleIDs(comId)
        .catch(() => []);
    return [
        {
            type: "create",
            comId,
            articleNumber,
            categories,
            placement: initialPlacement,
            data,
            articleType,
            children: subArticles,
            gfxId,
        },
        ...subArticles.map((childComId) => ({
            type: "create",
            comId: childComId,
            parent: comId,
        })),
    ];
};
const articleTypeMap = {
    [IGXA.EItemType.ArticleGfx]: "graphical",
    [IGXA.EItemType.ArticleCom]: "commercial",
    [IGXA.EItemType.Impression]: "impression",
    [IGXA.EItemType.Set]: "set",
    [IGXA.EItemType.HeadData]: "headItem",
};
const resolveArticleType = (articleNumber) => {
    if (articleNumber.itemType === undefined) {
        return "graphical";
    }
    const type = articleTypeMap[articleNumber.itemType];
    if (type === undefined) {
        throw new Error(`Can not create an article for unknown item type "${articleNumber.itemType}"`);
    }
    return type;
};
