import { IGXE, IGXA } from "@intelligentgraphics/igd.productdata";
import { Http } from "@intelligentgraphics/ig.utilities";
export var parse = function (data, _a) {
    var imageBasePath = _a.imageBasePath;
    var articles = [];
    var Models = data.Models, Options = data.Options, productsData = data.Products;
    for (var modelName in Models) {
        var Products = Models[modelName].Products;
        var _loop_1 = function (productId) {
            var product = Products[productId];
            var propertyList = {};
            var defaultConfiguration = {};
            var createProperty = function (propertyKey, values) {
                var data = Options[propertyKey] || { Values: {} };
                var value = product.Options.Defaults[propertyKey] ||
                    (values === undefined ? undefined : values[0]);
                if (value !== undefined) {
                    defaultConfiguration[propertyKey] = value;
                }
                // if (data === undefined) {
                // 	throw new Error(
                // 		`Can not find property informations for "${propertyKey}"`,
                // 	);
                // }
                return {
                    Key: propertyKey,
                    Value: value,
                    Values: values === undefined
                        ? []
                        : values.map(function (valueName) {
                            var valueData = data.Values[valueName] ||
                                {};
                            return {
                                Value: valueName,
                                Text: valueData.Text !== undefined
                                    ? [valueData.Text]
                                    : [valueName],
                                Image: valueData.Image !== undefined
                                    ? Http.joinPaths(imageBasePath, valueData.Image)
                                    : undefined,
                            };
                        }),
                    Text: [data.Text || propertyKey],
                    ValueType: IGXE.EPropertyType.String,
                    Type: values !== undefined ? "choiceList" : "readonly",
                    SubProperties: [],
                };
            };
            for (var propertyKey in product.Options.Dynamic) {
                var options = product.Options.Dynamic[propertyKey];
                var property = createProperty(propertyKey, options);
                propertyList[propertyKey] = property;
            }
            for (var propertyKey in product.Options.Static) {
                var options = product.Options.Static[propertyKey];
                var property = createProperty(propertyKey, undefined);
                propertyList[propertyKey] = property;
            }
            var productData = productsData[productId];
            articles.push({
                properties: propertyList,
                defaultConfiguration: defaultConfiguration,
                catalogItem: {
                    Type: IGXA.EItemType.ArticleGfx,
                    Caption: productData.Text || productId,
                    Description: productData.Description,
                    ID: productId,
                    Image: Http.joinPaths(imageBasePath, productData.Image),
                },
                id: productId,
            });
        };
        for (var productId in Products) {
            _loop_1(productId);
        }
    }
    return articles;
};
