"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    formatNumber: ()=>formatNumber,
    ReadPretty: ()=>ReadPretty,
    formatUnitConversion: ()=>formatUnitConversion,
    formatNumberWithSeparator: ()=>formatNumberWithSeparator,
    scientificNotation: ()=>scientificNotation
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const client_namespaceObject = require("@tachybase/utils/client");
const external_d3_format_namespaceObject = require("d3-format");
const external_mathjs_namespaceObject = require("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 external_mathjs_namespaceObject.round(result, 9);
}
function scientificNotation(number, decimalPlaces) {
    let separator = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : '.';
    const formatter = (0, external_d3_format_namespaceObject.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, schema_namespaceObject.isValid)(value)) return null;
    const unitData = formatUnitConversion(value, unitConversionType, unitConversion);
    const preciationData = (0, client_namespaceObject.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, external_react_namespaceObject.useMemo)(()=>formatNumber({
            step,
            formatStyle,
            value,
            unitConversion,
            unitConversionType,
            separator
        }), [
        step,
        formatStyle,
        value,
        unitConversion,
        unitConversionType,
        separator
    ]);
    if (!(0, schema_namespaceObject.isValid)(result)) return null;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
        className: 'tb-read-pretty-input-number',
        children: [
            addonBefore,
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                dangerouslySetInnerHTML: {
                    __html: result
                }
            }),
            addonAfter
        ]
    });
};
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
