"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.bulkInputToArray = exports.formatAmount = exports.formatPercentage = exports.formatDollars = exports.formatAsset = exports.formatPercentageBN = exports.formatAmountBN = exports.formatDollarsBN = exports.formatAssetBN = exports.maxDecimals = exports.MAX_INT = exports.MAXINT = exports.N_A = void 0;
var bn_1 = require("./bn");
var numbro_1 = __importDefault(require("numbro"));
exports.N_A = 'N/A';
exports.MAXINT = 57896044618658100000000000000000000000000000000000000000000000000000000;
exports.MAX_INT = 'MAX_INT';
var maxDecimals = function (x, dec) {
    return x.indexOf('.') >= 0
        ? x.substring(0, x.indexOf('.')) +
            x.substring(x.indexOf('.'), dec + 1 + x.indexOf('.'))
        : x;
};
exports.maxDecimals = maxDecimals;
var formatAssetBN = function (symbol, n, d, round, digits, trimMantissa) {
    if (round === void 0) { round = true; }
    if (digits === void 0) { digits = 2; }
    if (trimMantissa === void 0) { trimMantissa = false; }
    if (n == null || d == null)
        return exports.N_A;
    return (0, exports.formatAsset)(symbol, (0, bn_1.bnToNumber)(n, d), round, digits, trimMantissa);
};
exports.formatAssetBN = formatAssetBN;
var formatDollarsBN = function (n, d, round, digits, trimMantissa) {
    if (round === void 0) { round = true; }
    if (digits === void 0) { digits = 2; }
    if (trimMantissa === void 0) { trimMantissa = false; }
    if (n == null || d == null)
        return exports.N_A;
    return (0, exports.formatDollars)((0, bn_1.bnToNumber)(n, d), round, digits, trimMantissa);
};
exports.formatDollarsBN = formatDollarsBN;
var formatAmountBN = function (n, d, round, digits, trimMantissa) {
    if (round === void 0) { round = false; }
    if (digits === void 0) { digits = 2; }
    if (trimMantissa === void 0) { trimMantissa = true; }
    if (!n || d == null)
        return exports.N_A;
    return (0, exports.formatAmount)((0, bn_1.bnToNumber)(n, d), round, digits, trimMantissa);
};
exports.formatAmountBN = formatAmountBN;
var formatPercentageBN = function (n, d, round, digits, trimMantissa) {
    if (round === void 0) { round = false; }
    if (digits === void 0) { digits = 2; }
    if (trimMantissa === void 0) { trimMantissa = true; }
    if (n == null || d == null)
        return exports.N_A;
    return (0, exports.formatPercentage)((0, bn_1.bnToNumber)(n, d), round, digits, trimMantissa);
};
exports.formatPercentageBN = formatPercentageBN;
var formatAsset = function (symbol, num, round, digits, trimMantissa) {
    if (round === void 0) { round = true; }
    if (digits === void 0) { digits = 2; }
    if (trimMantissa === void 0) { trimMantissa = false; }
    if ((symbol === null || symbol === void 0 ? void 0 : symbol.toLowerCase()) === 'usdc')
        return (0, exports.formatDollars)(num, round, digits, trimMantissa);
    else {
        var amount = (0, exports.formatAmount)(num, round, digits, trimMantissa);
        if (amount === exports.N_A)
            return exports.N_A;
        if (!symbol)
            return amount;
        else
            return "".concat(amount, " ").concat(symbol);
    }
};
exports.formatAsset = formatAsset;
var formatDollars = function (num, round, digits, trimMantissa) {
    var _a;
    if (round === void 0) { round = true; }
    if (digits === void 0) { digits = 2; }
    if (trimMantissa === void 0) { trimMantissa = false; }
    if (num === 0)
        return '$0.00';
    if (num == null)
        return exports.N_A;
    if (num === exports.MAXINT)
        return exports.MAX_INT;
    if (digits < 3 && num < 0.01 && num > 0)
        return '<$0.01';
    if (digits < 3 && num > -0.01 && num < 0)
        return '<-$0.01';
    return (_a = (0, numbro_1.default)(num)
        .formatCurrency({
        average: round,
        mantissa: digits,
        trimMantissa: num > 1000 ? true : trimMantissa,
        thousandSeparated: true,
        abbreviations: {
            thousand: 'K',
            million: 'M',
            billion: 'B',
            trillion: 'T',
        },
    })) === null || _a === void 0 ? void 0 : _a.toUpperCase();
};
exports.formatDollars = formatDollars;
var formatPercentage = function (num, round, digits, trimMantissa) {
    var _a;
    if (round === void 0) { round = false; }
    if (digits === void 0) { digits = 2; }
    if (trimMantissa === void 0) { trimMantissa = true; }
    if (num === 0)
        return '0%';
    if (num == null)
        return exports.N_A;
    if (num === exports.MAXINT)
        return exports.MAX_INT;
    if (digits < 5 /* sic: 3 + 5 */ && num < 0.0001 && num > 0)
        return '<0.01%';
    if (digits < 5 /* sic: 3 + 5 */ && num > -0.0001 && num < 0)
        return '<-0.01%';
    return (_a = (0, numbro_1.default)(num)
        .format({
        output: 'percent',
        average: round,
        mantissa: digits,
        trimMantissa: trimMantissa,
        thousandSeparated: true,
        abbreviations: {
            million: 'M',
            billion: 'B',
        },
    })) === null || _a === void 0 ? void 0 : _a.toUpperCase();
};
exports.formatPercentage = formatPercentage;
var formatAmount = function (num, round, digits, trimMantissa) {
    var _a;
    if (round === void 0) { round = false; }
    if (digits === void 0) { digits = 2; }
    if (trimMantissa === void 0) { trimMantissa = true; }
    if (num === 0)
        return '0';
    if (num == null)
        return exports.N_A;
    if (num === exports.MAXINT)
        return exports.MAX_INT;
    if (digits < 3 && num < 0.01 && num > 0)
        return '<0.01';
    if (digits < 3 && num > 0.01 && num < 0)
        return '<-0.01';
    return (_a = (0, numbro_1.default)(num)
        .format({
        average: round,
        mantissa: digits,
        trimMantissa: trimMantissa,
        thousandSeparated: true,
        abbreviations: {
            million: 'M',
            billion: 'B',
        },
    })) === null || _a === void 0 ? void 0 : _a.toUpperCase();
};
exports.formatAmount = formatAmount;
function bulkInputToArray(value) {
    // separate on all non-alphanumeric characters
    return value.split(/[\W_]+/g).filter(function (x) { return x.length; });
}
exports.bulkInputToArray = bulkInputToArray;
//# sourceMappingURL=format.js.map