import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE_d3_format__ from "d3-format";
import * as __WEBPACK_EXTERNAL_MODULE_mathjs__ from "mathjs";
function countDecimalPlaces(value) {
    const number = Number(value);
    if (!Number.isFinite(number)) return 0;
    const decimalPart = String(number).split('.')[1];
    return decimalPart ? decimalPart.length : 0;
}
const separators = {
    '0,0.00': {
        thousands: ',',
        decimal: '.'
    },
    '0.0,00': {
        thousands: '.',
        decimal: ','
    },
    '0 0,00': {
        thousands: ' ',
        decimal: '.'
    },
    '0.00': {
        thousands: '',
        decimal: '.'
    }
};
function formatNumberWithSeparator(number) {
    let format = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : '0.00', step = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : 1;
    let formattedNumber = '';
    if (separators[format]) {
        const { thousands, decimal } = separators[format];
        formattedNumber = number.toLocaleString('en-US', {
            style: 'decimal',
            minimumFractionDigits: step,
            maximumFractionDigits: step
        }).replace(/,/g, 'comma_placeholder').replace(/\./g, 'dot_placeholder').replace(/comma_placeholder/g, thousands).replace(/dot_placeholder/g, decimal);
    } else formattedNumber = number.toString();
    return formattedNumber;
}
function formatUnitConversion(value) {
    let operator = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : '*', multiplier = arguments.length > 2 ? arguments[2] : void 0;
    if (!multiplier) return value;
    let result;
    if ('*' === operator) result = value * multiplier;
    else if ('/' === operator) {
        if (0 !== multiplier) result = value / multiplier;
        else {
            console.error('Error: Division by zero.');
            return null;
        }
    } else {
        console.error("Error: Invalid operator. Use '*' for multiplication or '/' for division.");
        return null;
    }
    return __WEBPACK_EXTERNAL_MODULE_mathjs__.round(result, 9);
}
function scientificNotation(number, decimalPlaces) {
    let separator = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : '.';
    const formatter = (0, __WEBPACK_EXTERNAL_MODULE_d3_format__.format)(".".concat(decimalPlaces, "e"));
    const formattedNumber = formatter(number).replace('.', separator);
    const result = formattedNumber.replace(/e([+-]?\d+)/, (match, exponent)=>{
        if (exponent.startsWith('+')) return "\xd710<sup>".concat(exponent.slice(1), "</sup>");
        return "\xd710<sup>-".concat(exponent.slice(1), "</sup>");
    });
    return result;
}
function formatNumber(props) {
    const { step, formatStyle, value, unitConversion, unitConversionType, separator = '0.00' } = props;
    if (!(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.isValid)(value)) return null;
    const unitData = formatUnitConversion(value, unitConversionType, unitConversion);
    const preciationData = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.toFixedByStep)(unitData, step);
    let result;
    result = formatNumberWithSeparator(Number(preciationData), separator, countDecimalPlaces(step));
    if ('scientifix' === formatStyle) {
        var _separators_separator;
        result = scientificNotation(Number(unitData), countDecimalPlaces(step), null == separators ? void 0 : null === (_separators_separator = separators[separator]) || void 0 === _separators_separator ? void 0 : _separators_separator['decimal']);
    }
    return result;
}
const ReadPretty = (props)=>{
    const { step, formatStyle, value, addonBefore, addonAfter, unitConversion, unitConversionType, separator } = props;
    const result = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>formatNumber({
            step,
            formatStyle,
            value,
            unitConversion,
            unitConversionType,
            separator
        }), [
        step,
        formatStyle,
        value,
        unitConversion,
        unitConversionType,
        separator
    ]);
    if (!(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.isValid)(result)) return null;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
        className: 'tb-read-pretty-input-number',
        children: [
            addonBefore,
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                dangerouslySetInnerHTML: {
                    __html: result
                }
            }),
            addonAfter
        ]
    });
};
export { ReadPretty, formatNumber, formatNumberWithSeparator, formatUnitConversion, scientificNotation };
