/**
 * DevExtreme (esm/localization/globalize/currency.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import openXmlCurrencyFormat from "../open_xml_currency_format";
import "./core";
import "./number";
import "../currency";
import "globalize/currency";
import Globalize from "globalize";
import config from "../../core/config";
import numberLocalization from "../number";
var CURRENCY_STYLES = ["symbol", "accounting"];
if (Globalize && Globalize.formatCurrency) {
    if ("en" === Globalize.locale().locale) {
        Globalize.locale("en")
    }
    var formattersCache = {};
    var getFormatter = (currency, format) => {
        var formatter;
        var formatCacheKey;
        if ("object" === typeof format) {
            formatCacheKey = Globalize.locale().locale + ":" + currency + ":" + JSON.stringify(format)
        } else {
            formatCacheKey = Globalize.locale().locale + ":" + currency + ":" + format
        }
        formatter = formattersCache[formatCacheKey];
        if (!formatter) {
            formatter = formattersCache[formatCacheKey] = Globalize.currencyFormatter(currency, format)
        }
        return formatter
    };
    var globalizeCurrencyLocalization = {
        _formatNumberCore: function(value, format, formatConfig) {
            if ("currency" === format) {
                var currency = formatConfig && formatConfig.currency || config().defaultCurrency;
                return getFormatter(currency, this._normalizeFormatConfig(format, formatConfig, value))(value)
            }
            return this.callBase.apply(this, arguments)
        },
        _normalizeFormatConfig: function(format, formatConfig, value) {
            var normalizedConfig = this.callBase(format, formatConfig, value);
            if ("currency" === format) {
                var _formatConfig$useCurr;
                var useAccountingStyle = null !== (_formatConfig$useCurr = formatConfig.useCurrencyAccountingStyle) && void 0 !== _formatConfig$useCurr ? _formatConfig$useCurr : config().defaultUseCurrencyAccountingStyle;
                normalizedConfig.style = CURRENCY_STYLES[+useAccountingStyle]
            }
            return normalizedConfig
        },
        format: function(value, _format) {
            if ("number" !== typeof value) {
                return value
            }
            _format = this._normalizeFormat(_format);
            if (_format) {
                if ("default" === _format.currency) {
                    _format.currency = config().defaultCurrency
                }
                if ("currency" === _format.type) {
                    return this._formatNumber(value, this._parseNumberFormatString("currency"), _format)
                } else if (!_format.type && _format.currency) {
                    return getFormatter(_format.currency, _format)(value)
                }
            }
            return this.callBase.apply(this, arguments)
        },
        getCurrencySymbol: function(currency) {
            if (!currency) {
                currency = config().defaultCurrency
            }
            return Globalize.cldr.main("numbers/currencies/" + currency)
        },
        getOpenXmlCurrencyFormat: function(currency) {
            var currencySymbol = this.getCurrencySymbol(currency).symbol;
            var accountingFormat = Globalize.cldr.main("numbers/currencyFormats-numberSystem-latn").accounting;
            return openXmlCurrencyFormat(currencySymbol, accountingFormat)
        }
    };
    numberLocalization.inject(globalizeCurrencyLocalization)
}
