"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const lodash_1 = require("lodash");
const luxon_1 = require("luxon");
/**
 * Wether a value is a BigNumber instance or not
 * NOTE @RafaelVidaurre: We cannot use instanceof here since we are handling
 * multiple versions of BigNumber
 */
function isBigNumber(value) {
    return typeof value === 'object' && (value.isBigNumber || value._isBigNumber);
}
/**
 * Converts a number to a string formatted representing dollars
 *
 * @param value number to convert
 * @param options.decimals amount of decimals to display
 */
exports.toUSD = (value, { decimals = 2 } = {}) => {
    const isValid = value !== null && (lodash_1.isNumber(value) || isBigNumber(value));
    if (!isValid) {
        return `- USD`;
    }
    const num = new bignumber_js_1.default(value);
    return `${num.toFormat(decimals)} USD`;
};
/**
 * Converts a number to a string formatted.
 * @param value number to convert
 * @param options.decimals amount of decimals to display
 */
exports.toCurrencyString = (value, { decimals = 2 } = {}) => {
    const isValid = value !== null && (lodash_1.isFinite(value) || isBigNumber(value));
    if (!isValid) {
        return '-';
    }
    const num = new bignumber_js_1.default(value);
    return num.toFormat(decimals);
};
/**
 * Converts a number into a percentage
 *
 * @param value number to convert
 * @param options.decimals amount of decimals to display
 */
exports.toPercent = (value, { decimals = 2 } = {}) => {
    const isValid = value !== null && (lodash_1.isNumber(value) || isBigNumber(value));
    if (!isValid) {
        return '- %';
    }
    const parsedValue = new bignumber_js_1.default(value);
    return parsedValue.toNumber().toLocaleString('en-US', {
        style: 'percent',
        maximumFractionDigits: decimals,
    });
};
/**
 * Converts a number into a string representing an amount of tokens
 *
 * @param value number to convert
 * @param options.decimals amount of decimals to display
 */
exports.toTokens = (value, { decimals = 1 } = {}) => {
    const isValid = value !== null && (lodash_1.isNumber(value) || isBigNumber(value));
    if (!isValid) {
        return `-`;
    }
    let num;
    if (isBigNumber(value)) {
        num = new bignumber_js_1.default(value);
    }
    else {
        num = new bignumber_js_1.default(`${value}`);
    }
    return num
        .precision(18)
        .decimalPlaces(decimals)
        .toFormat();
};
/**
 * Shortens an address for display purposes
 */
exports.toShortAddress = (address, { size = 17 } = {}) => {
    const minSize = 5;
    if (size < minSize) {
        throw new Error('Cannot shortify an address to less than 5 characters');
    }
    const portionSize = Math.floor((size - 3) / 2);
    const remainder = ((size - 3) / 2) % 1 !== 0 ? 1 : 0;
    return `${address.substring(0, portionSize + remainder)}...${address.slice(-portionSize)}`;
};
/**
 * Shortens an address for display purposes
 */
exports.toShortAddress2 = (address, { size = 15 } = {}) => {
    const minSize = 5;
    if (size < minSize) {
        throw new Error('Cannot shortify an address to less than 5 characters');
    }
    const portionSize = Math.floor((size - 3) / 2);
    const remainder = ((size - 3) / 2) % 1 !== 0 ? 1 : 0;
    return `${address.substring(0, portionSize + remainder)}...${address.slice(-portionSize)}`;
};
exports.date = luxon_1.DateTime;
exports.toDateFormat = (inputDate, { format = luxon_1.DateTime.DATE_FULL } = {}) => luxon_1.DateTime.fromJSDate(inputDate).toLocaleString(format);
exports.toTimeFormat = (inputDate, { format = luxon_1.DateTime.TIME_SIMPLE } = {}) => luxon_1.DateTime.fromJSDate(inputDate).toLocaleString(format);
exports.toFileSizeFormat = (bytes, { decimals = 2 } = {}) => {
    if (bytes === 0)
        return '0 Bytes';
    const k = 1024;
    const dm = decimals < 0 ? 0 : decimals;
    const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
};
//# sourceMappingURL=index.js.map