"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var igd_core_1 = require("@intelligentgraphics/igd.core");
var defaultPriceFormatter = function (_a) {
    var currency = _a.currency, rangeMin = _a.rangeMin, rangeMax = _a.rangeMax, value = _a.value, locale = _a.locale;
    var price;
    if (rangeMin !== undefined && rangeMax !== undefined) {
        price = {
            min: rangeMin,
            max: rangeMax,
        };
    }
    else if (value !== undefined) {
        price = value;
    }
    else {
        throw new Error("Detected invalid price without any value");
    }
    return igd_core_1.PriceHelper.getLocalizedDecimal(price, locale, true, 2, currency);
};
exports.createBasket = function (session, _a) {
    var _b = (_a === void 0 ? {} : _a).priceFormatter, priceFormatter = _b === void 0 ? defaultPriceFormatter : _b;
    var positionStrings = new Map();
    var update = function () {
        buildPositionStrings(session.getFilteredArticles(function (article) { return article.parent === undefined; }), []);
    };
    var buildPositionStrings = function (articles, parentParts) {
        var skipped = 0;
        for (var index = 0; index < articles.length; index++) {
            var article = articles[index];
            if (article.data.hidden) {
                skipped++;
                continue;
            }
            var articleIndex = index - skipped;
            var articleParts = tslib_1.__spread(parentParts, [articleIndex + 1]);
            positionStrings.set(article.comId, articleParts.join("."));
            var children = session.getArticlesByComIds(article.children);
            buildPositionStrings(children, articleParts);
        }
    };
    var getPositionString = function (comId) { return positionStrings.get(comId); };
    var getBomTotals = function () {
        var e_1, _a;
        var articles = session.getAllArticles();
        var min = 0;
        var max = 0;
        var currency;
        try {
            for (var articles_1 = tslib_1.__values(articles), articles_1_1 = articles_1.next(); !articles_1_1.done; articles_1_1 = articles_1.next()) {
                var item = articles_1_1.value;
                var singlePrice = getSinglePrice(item.comId);
                if (singlePrice === undefined) {
                    continue;
                }
                if (singlePrice.Value !== undefined) {
                    min += singlePrice.Value;
                    max += singlePrice.Value;
                }
                else if (singlePrice.RangeMin !== undefined &&
                    singlePrice.RangeMax !== undefined) {
                    min += singlePrice.RangeMin;
                    max += singlePrice.RangeMax;
                }
                currency = singlePrice.Currency;
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (articles_1_1 && !articles_1_1.done && (_a = articles_1.return)) _a.call(articles_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        var value = {
            rangeMin: min === max ? undefined : min,
            rangeMax: min === max ? undefined : max,
            value: min === max ? min : undefined,
            currency: currency,
            locale: session.getLanguage(),
        };
        return [
            {
                caption: "@subtotal",
                rangeMin: min === max ? undefined : min,
                rangeMax: min === max ? undefined : max,
                value: min === max ? min : undefined,
                currency: currency,
                type: "subtotal",
                text: priceFormatter(value) || "",
            },
            {
                caption: "@total",
                rangeMin: min === max ? undefined : min,
                rangeMax: min === max ? undefined : max,
                value: min === max ? min : undefined,
                currency: currency,
                type: "total",
                text: priceFormatter(value) || "",
            },
        ];
    };
    var getSinglePrice = function (comId) {
        var prices = session.getArticleInfo(comId).prices;
        var singlePrice = prices.find(function (price) { return price.Level === igd_core_1.IGXE.EPriceLevel.Base; });
        return singlePrice;
    };
    var getSinglePriceText = function (comId) {
        var singlePrice = getSinglePrice(comId);
        if (singlePrice === undefined) {
            return undefined;
        }
        var article = session.getArticleByComId(comId);
        if (article === undefined) {
            throw new Error("Can not find article for comId \"" + comId + "\"");
        }
        return priceFormatter({
            value: singlePrice.Value,
            rangeMax: singlePrice.RangeMax,
            rangeMin: singlePrice.RangeMin,
            currency: singlePrice.Currency,
            locale: session.getLanguage(),
        });
    };
    var subscription = session.subscribe({
        ItemCreated: update,
        ItemRemoved: update,
    });
    var destroy = function () { return subscription.destroy(); };
    update();
    return {
        destroy: destroy,
        getPositionString: getPositionString,
        getBomTotals: getBomTotals,
        getSinglePrice: getSinglePrice,
        getSinglePriceText: getSinglePriceText,
    };
};
