import { checkValueIsFilled, isNil } from '@angular-ru/cdk/utils';

const mantisaDigits = 10;
const fractionDigits = 8;
const epsilon = 1e-8;
const bias = 0.5;
const gaussPointer = 2;
function gaussRound(num, decimalPlaces = 0) {
    const mantisa = Math.pow(mantisaDigits, decimalPlaces);
    const numberValue = Number((decimalPlaces ? num * mantisa : num).toFixed(fractionDigits));
    const integerValue = Math.floor(numberValue);
    const decimalPointValue = numberValue - integerValue;
    const 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) {
    const 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';
}

const formatToPartsValue = 12345.6;
const intlFormatOrigin = 9876543210;
function toNumber(value, locale = 'ru-RU') {
    var _a, _b;
    let prepared = value === null || value === void 0 ? void 0 : value.toString().replace(/\s/g, '');
    const parts = new Intl.NumberFormat(locale).formatToParts(formatToPartsValue);
    const numerals = Array.from(new Intl.NumberFormat(locale, { useGrouping: false }).format(intlFormatOrigin)).reverse();
    const group = new RegExp(`[${(_a = parts.find((d) => d.type === 'group')) === null || _a === void 0 ? void 0 : _a.value}]`, 'g');
    const decimal = new RegExp(`[${(_b = parts.find((d) => d.type === 'decimal')) === null || _b === void 0 ? void 0 : _b.value}]`);
    const numeral = new RegExp(`[${numerals.join('')}]`, 'g');
    const indexes = (decimalVal) => { var _a, _b; return (_b = (_a = new Map(numerals.map((dx, i) => [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 checkValueIsFilled(prepared) ? Number(prepared) : NaN;
}

function numberFormat(value, options) {
    var _a, _b;
    const fallback = (_a = options === null || options === void 0 ? void 0 : options.fallback) !== null && _a !== void 0 ? _a : '';
    const locales = (_b = options === null || options === void 0 ? void 0 : options.locales) !== null && _b !== void 0 ? _b : 'ru-RU';
    const numberValue = 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) {
    const mantisa = 10;
    const numPowerConverter = Math.pow(mantisa, decimalPlaces);
    return ~~(value * numPowerConverter) / numPowerConverter;
}

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

export { gaussRound, getFractionSeparator, half, isNumber, numberFormat, toNumber, truncated };
//# sourceMappingURL=angular-ru-cdk-number.js.map
