(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular-ru/cdk/utils')) :
    typeof define === 'function' && define.amd ? define('@angular-ru/cdk/number', ['exports', '@angular-ru/cdk/utils'], factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global['angular-ru'] = global['angular-ru'] || {}, global['angular-ru'].cdk = global['angular-ru'].cdk || {}, global['angular-ru'].cdk.number = {}), global['angular-ru'].cdk.utils));
}(this, (function (exports, utils) { 'use strict';

    var mantisaDigits = 10;
    var fractionDigits = 8;
    var epsilon = 1e-8;
    var bias = 0.5;
    var gaussPointer = 2;
    function gaussRound(num, decimalPlaces) {
        if (decimalPlaces === void 0) { decimalPlaces = 0; }
        var mantisa = Math.pow(mantisaDigits, decimalPlaces);
        var numberValue = Number((decimalPlaces ? num * mantisa : num).toFixed(fractionDigits));
        var integerValue = Math.floor(numberValue);
        var decimalPointValue = numberValue - integerValue;
        var gaussPointCondition = decimalPointValue > bias - epsilon && decimalPointValue < bias + epsilon;
        if (gaussPointCondition) {
            return (integerValue % gaussPointer === 0 ? integerValue : integerValue + 1) / mantisa;
        }
        else {
            return Math.round(numberValue) / mantisa;
        }
    }

    function getFractionSeparator(lang) {
        var fractionSample = { expectedValue: 1000000.65, lastValuePositionBeforeFraction: -3 };
        return fractionSample.expectedValue
            .toLocaleString(lang)
            .slice(fractionSample.lastValuePositionBeforeFraction)
            .slice(0, 1);
    }

    function half(dividend) {
        if (typeof dividend === 'number') {
            // eslint-disable-next-line @typescript-eslint/no-magic-numbers
            return dividend / 2;
        }
        else {
            return NaN;
        }
    }

    function isNumber(value) {
        return typeof value === 'number';
    }

    var formatToPartsValue = 12345.6;
    var intlFormatOrigin = 9876543210;
    function toNumber(value, locale) {
        if (locale === void 0) { locale = 'ru-RU'; }
        var _a, _b;
        var prepared = value === null || value === void 0 ? void 0 : value.toString().replace(/\s/g, '');
        var parts = new Intl.NumberFormat(locale).formatToParts(formatToPartsValue);
        var numerals = Array.from(new Intl.NumberFormat(locale, { useGrouping: false }).format(intlFormatOrigin)).reverse();
        var group = new RegExp("[" + ((_a = parts.find(function (d) { return d.type === 'group'; })) === null || _a === void 0 ? void 0 : _a.value) + "]", 'g');
        var decimal = new RegExp("[" + ((_b = parts.find(function (d) { return d.type === 'decimal'; })) === null || _b === void 0 ? void 0 : _b.value) + "]");
        var numeral = new RegExp("[" + numerals.join('') + "]", 'g');
        var indexes = function (decimalVal) { var _a, _b; return (_b = (_a = new Map(numerals.map(function (dx, i) { return [dx, i]; })).get(decimalVal)) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : ''; };
        prepared = prepared === null || prepared === void 0 ? void 0 : prepared.trim().replace(group, '').replace(decimal, '.').replace(numeral, indexes);
        return utils.checkValueIsFilled(prepared) ? Number(prepared) : NaN;
    }

    function numberFormat(value, options) {
        var _a, _b;
        var fallback = (_a = options === null || options === void 0 ? void 0 : options.fallback) !== null && _a !== void 0 ? _a : '';
        var locales = (_b = options === null || options === void 0 ? void 0 : options.locales) !== null && _b !== void 0 ? _b : 'ru-RU';
        var numberValue = utils.isNil(value) ? NaN : toNumber(value);
        return (isNaN(numberValue) ? fallback : numberValue.toLocaleString(locales, options === null || options === void 0 ? void 0 : options.formatOptions)).replace(/\s/g, ' ');
    }

    function truncated(value, decimalPlaces) {
        var mantisa = 10;
        var numPowerConverter = Math.pow(mantisa, decimalPlaces);
        return ~~(value * numPowerConverter) / numPowerConverter;
    }

    /**
     * Generated bundle index. Do not edit.
     */

    exports.gaussRound = gaussRound;
    exports.getFractionSeparator = getFractionSeparator;
    exports.half = half;
    exports.isNumber = isNumber;
    exports.numberFormat = numberFormat;
    exports.toNumber = toNumber;
    exports.truncated = truncated;

    Object.defineProperty(exports, '__esModule', { value: true });

})));
//# sourceMappingURL=angular-ru-cdk-number.umd.js.map
