import { IGXB, convertIGXB } from "@intelligentgraphics/igd.core";
export const readIGXB = (igxb) => {
    igxb = convertIGXB(igxb);
    return processItems(igxb.Items);
};
const processItems = (items) => {
    const result = [];
    for (const item of items) {
        if (item.ItemType === IGXB.EItemType.Group) {
            result.push(...processItems(item.Items));
            continue;
        }
        const builder = itemBuilders[item.ItemType];
        if (builder !== undefined) {
            const builderResults = builder(item);
            if (builderResults === undefined) {
                continue;
            }
            if (Array.isArray(builderResults)) {
                result.push(...builderResults);
                continue;
            }
            result.push(builderResults);
        }
    }
    return result;
};
const createFromBaseArticleItem = (article) => ({
    comId: article.ComId,
    articleNumber: article.ArticleNumber !== undefined
        ? article.ArticleNumber
        : {
            baseArticleNumber: article.ArticleId,
            articlePackageId: article.ArticlePackageId,
        },
    source: article.Source,
    configuration: article.Configuration,
    categories: article.Categories,
    data: article.Data,
});
const createFromArticleItem = item => {
    if (item.Data && item.Data.dontRestore) {
        return;
    }
    const baseArticle = createFromBaseArticleItem(item);
    return {
        ...baseArticle,
        type: "graphical",
        data: item.Data,
        placement: item.Position === undefined
            ? undefined
            : {
                position: item.Position,
                rotation: item.Rotation || { x: 0, y: 0, z: 0 },
            },
    };
};
const createFromImpressionItem = item => ({
    ...createFromBaseArticleItem(item),
    type: "impression",
});
const createFromHeadItem = item => ({
    type: "headItem",
    source: item.Source,
    comId: item.ComId,
    articleNumber: {
        baseArticleNumber: item.ArticleId,
        articlePackageId: item.ArticlePackageId,
    },
    configuration: item.Configuration,
    categories: item.Categories,
});
const createFromSet = item => {
    const articles = [];
    const mainArticle = {
        comId: item.ComId,
        configuration: item.Configuration,
        type: "set",
        articleNumber: {
            baseArticleNumber: item.SetId,
        },
        source: item.Source,
        categories: item.Categories,
        children: [],
    };
    articles.push(mainArticle);
    for (const article of item.Items) {
        mainArticle.children.push(article.ComId);
        const subArticle = createItem(article);
        subArticle.parent = item.ComId;
        articles.push(subArticle);
    }
    return articles;
};
const createItem = (item) => {
    const builder = itemBuilders[item.ItemType];
    return builder(item);
};
const itemBuilders = {
    [IGXB.EItemType.Article]: createFromArticleItem,
    [IGXB.EItemType.Impression]: createFromImpressionItem,
    [IGXB.EItemType.HeadData]: createFromHeadItem,
    [IGXB.EItemType.Set]: createFromSet,
};
