var _a;
import { __assign, __read, __spread, __values } from "tslib";
import { IGXB, convertIGXB } from "@intelligentgraphics/igd.core";
export var readIGXB = function (igxb) {
    igxb = convertIGXB(igxb);
    return processItems(igxb.Items);
};
var processItems = function (items) {
    var e_1, _a;
    var result = [];
    try {
        for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
            var item = items_1_1.value;
            if (item.ItemType === IGXB.EItemType.Group) {
                result.push.apply(result, __spread(processItems(item.Items)));
                continue;
            }
            var builder = itemBuilders[item.ItemType];
            if (builder !== undefined) {
                var builderResults = builder(item);
                if (builderResults === undefined) {
                    continue;
                }
                if (Array.isArray(builderResults)) {
                    result.push.apply(result, __spread(builderResults));
                    continue;
                }
                result.push(builderResults);
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return result;
};
var createFromBaseArticleItem = function (article) { return ({
    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,
}); };
var createFromArticleItem = function (item) {
    if (item.Data && item.Data.dontRestore) {
        return;
    }
    var baseArticle = createFromBaseArticleItem(item);
    return __assign(__assign({}, baseArticle), { type: "graphical", data: item.Data, placement: item.Position === undefined
            ? undefined
            : {
                position: item.Position,
                rotation: item.Rotation || { x: 0, y: 0, z: 0 },
            } });
};
var createFromImpressionItem = function (item) { return (__assign(__assign({}, createFromBaseArticleItem(item)), { type: "impression" })); };
var createFromHeadItem = function (item) { return ({
    type: "headItem",
    source: item.Source,
    comId: item.ComId,
    articleNumber: {
        baseArticleNumber: item.ArticleId,
        articlePackageId: item.ArticlePackageId,
    },
    configuration: item.Configuration,
    categories: item.Categories,
}); };
var createFromSet = function (item) {
    var e_2, _a;
    var articles = [];
    var mainArticle = {
        comId: item.ComId,
        configuration: item.Configuration,
        type: "set",
        articleNumber: {
            baseArticleNumber: item.SetId,
        },
        source: item.Source,
        categories: item.Categories,
        children: [],
    };
    articles.push(mainArticle);
    try {
        for (var _b = __values(item.Items), _c = _b.next(); !_c.done; _c = _b.next()) {
            var article = _c.value;
            mainArticle.children.push(article.ComId);
            var subArticle = createItem(article);
            subArticle.parent = item.ComId;
            articles.push(subArticle);
        }
    }
    catch (e_2_1) { e_2 = { error: e_2_1 }; }
    finally {
        try {
            if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
        }
        finally { if (e_2) throw e_2.error; }
    }
    return articles;
};
var createItem = function (item) {
    var builder = itemBuilders[item.ItemType];
    return builder(item);
};
var itemBuilders = (_a = {},
    _a[IGXB.EItemType.Article] = createFromArticleItem,
    _a[IGXB.EItemType.Impression] = createFromImpressionItem,
    _a[IGXB.EItemType.HeadData] = createFromHeadItem,
    _a[IGXB.EItemType.Set] = createFromSet,
    _a);
