import { IGXA } from "@intelligentgraphics/igd.core";
import { executeCreateArticleOperation } from "./Creation";
export const executeRestoreOperation = async (article, articleQuery, productDataManager) => {
    const articleDoesExist = await productDataManager
        .getArticleID(article.comId)
        .catch(() => false);
    if (articleDoesExist) {
        const subArticles = await productDataManager
            .getSubArticleIDs(article.comId)
            .catch(() => []);
        return [
            {
                ...article,
                articleType: article.type,
                type: "create",
            },
            ...subArticles.map((childComId) => ({
                type: "create",
                comId: childComId,
                parent: article.comId,
            })),
        ];
    }
    return executeCreateArticleOperation({
        source: article.source,
        articleNumber: {
            ...article.articleNumber,
            itemType: resolveArticleType(article),
        },
        initialConfiguration: article.configuration,
        initialPlacement: article.placement,
        data: article.data,
        categories: article.categories,
    }, articleQuery, productDataManager);
};
export const executeRestoreSessionProviderOperation = async (data, sessionProvider) => {
    const comIds = await sessionProvider.restore(data);
    return comIds.map(comId => ({
        type: "create",
        comId,
    }));
};
const resolveArticleType = (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;
};
