import { toPercent } from "@equilab/utils/lib/format";
import BigNumber from "bignumber.js";
export const numberWithSeparator = (num, separator = ",") => {
    const number = num.toString().split(".");
    const integer = number[0];
    const fraction = number[1];
    return `${integer.replace(/\B(?=(\d{3})+(?!\d))/g, separator)}${fraction ? `.${fraction}` : ""}`;
};
export const numberWithCommas = (num) => numberWithSeparator(num);
export const numberWithSpaces = (num) => numberWithSeparator(num, " ");
export const safeToFixed = (value, precision, withSeparator, separator) => {
    if (typeof value === "undefined") {
        return "0.00";
    }
    else {
        const amount = parseFloat(value
            .toFixed(precision)
            .replace(/([0-9]+(\.[0-9]+[1-9])?)(\.?0+$)/, "$1")) * 1;
        if (withSeparator) {
            return numberWithSeparator(amount, separator);
        }
        return amount.toString();
    }
};
const COMPACT_GROUP = [
    {
        min: 1e12,
        symbol: "B",
    },
    {
        min: 1e9,
        symbol: "B",
    },
    {
        min: 1e6,
        symbol: "M",
    },
    {
        min: 1e3,
        symbol: "K",
    },
];
export const safeToCompact = (value, precision = 2, withSeparator = false, separator) => {
    if (!value) {
        return "0.00";
    }
    const amnt = typeof value === "number" ? new BigNumber(value) : value;
    const group = COMPACT_GROUP.find(({ min }) => amnt.gte(min));
    if (!group) {
        return safeToFixed(value, precision, withSeparator, separator);
    }
    return (parseFloat(amnt
        .div(group.min)
        .toFixed(2)
        .replace(/([0-9]+(\.[0-9]+[1-9])?)(\.?0+$)/, "$1")) + group.symbol);
};
export const trimDp = (val, limit = 9) => {
    let i = 0;
    const removeDots = val.replace(/[\.\%]/g, function (match) {
        return match === "." ? (i++ === 0 ? "." : "") : "";
    });
    const [ip, dp] = removeDots.split(".");
    if (dp && dp.length > 9) {
        return `${ip}.${dp.substring(0, limit)}`;
    }
    else {
        return removeDots;
    }
};
export const trimNumber = (val, limitInteger = 12, limitDecimals = 9) => {
    let i = 0;
    const removeDots = val.replace(/[\.\%]/g, function (match) {
        return match === "." ? (i++ === 0 ? "." : "") : "";
    });
    let [ip, dp] = removeDots.split(".");
    if (dp && dp.length > limitDecimals) {
        dp = dp.substring(0, limitDecimals);
        return `${ip}.${dp}`;
    }
    if (ip && ip.length > limitInteger) {
        ip = ip.substring(0, limitInteger);
        return `${ip}${val.includes(".") ? `.${dp ?? ""}` : ""}`;
    }
    return val;
};
export const safeToPercent = (value, precision) => {
    if (typeof value === "undefined") {
        return "-";
    }
    else if (value instanceof BigNumber) {
        return value.isNaN() || !value.isFinite()
            ? "-"
            : toPercent(value, precision);
    }
    else {
        return toPercent(new BigNumber(value), precision);
    }
};
const newLineRegex = /\n|\t/g;
export const toSvgLink = (svgText) => {
    let result = svgText.replace(newLineRegex, "");
    return encodeURI(`data:image/svg+xml;base64,${Buffer.from(result).toString("base64")}`);
};
//# sourceMappingURL=format.js.map