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') {
    let prepared = 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(`[${parts.find((d) => d.type === 'group')?.value}]`, 'g');
    const decimal = new RegExp(`[${parts.find((d) => d.type === 'decimal')?.value}]`);
    const numeral = new RegExp(`[${numerals.join('')}]`, 'g');
    const indexes = (decimalValue) => new Map(numerals.map((dx, i) => [dx, i])).get(decimalValue)?.toString() ??
        '';
    prepared = prepared?.trim().replace(group, '').replace(decimal, '.').replace(numeral, indexes);
    return checkValueIsFilled(prepared) ? Number(prepared) : NaN;
}

function numberFormat(value, options) {
    const fallback = options?.fallback ?? '';
    const locales = options?.locales ?? 'ru-RU';
    const numberValue = isNil(value) ? NaN : toNumber(value);
    return (isNaN(numberValue) ? fallback : numberValue.toLocaleString(locales, 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.mjs.map
