"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatNumberFromChain = exports.formatNumberForChain = exports.stringToPaddedHex = exports.stringToPadded = exports.hexToUnpaddedString = exports.stringToUnpadded = exports.splitOnCapitalLetter = exports.toDateTimeString = exports.toDateString = exports.formatFileSize = exports.formatAmount = exports.trimString = exports.shortenString = void 0;
const moment_1 = __importDefault(require("moment"));
const util_1 = require("@polkadot/util");
exports.shortenString = (string, { size = 17 } = {}) => {
    const minSize = 5;
    if (size < minSize) {
        throw new Error('Cannot shorten string to less than 5 characters');
    }
    const portionSize = Math.floor((size - 3) / 2);
    const remainder = ((size - 3) / 2) % 1 !== 0 ? 1 : 0;
    if (string.length <= size)
        return string;
    return `${string.substring(0, portionSize + remainder)}...${string.slice(-portionSize)}`;
};
exports.trimString = (string, size = 17) => string.length > size ? `${string.slice(0, size).trim()}...` : string;
exports.formatAmount = (amount, minimumFractionDigits = 0) => {
    const formatter = new Intl.NumberFormat('en-US', {
        minimumFractionDigits,
    });
    return formatter.format(parseFloat(amount.toString()));
};
exports.formatFileSize = (bytes) => {
    let val = bytes;
    let suffix = '';
    if (val < 1000) {
        suffix = 'B';
    }
    else {
        val /= 1024;
        if (val < 1000) {
            suffix = 'KB';
        }
        else {
            val /= 1024;
            if (val < 1000) {
                suffix = 'MB';
            }
            else {
                val /= 1024;
                if (val < 1000) {
                    suffix = 'GB';
                }
                else {
                    val /= 1024;
                    suffix = 'TB';
                }
            }
        }
    }
    return Math.floor(val * 100) / 100 + suffix;
};
exports.toDateString = (date, expanded = false) => {
    const dateAsMoment = moment_1.default(date);
    return expanded
        ? dateAsMoment.format('DD MMMM, YYYY')
        : dateAsMoment.format('MM/DD/YYYY');
};
exports.toDateTimeString = (date, expanded = false) => {
    const dateAsMoment = moment_1.default(date);
    return expanded
        ? dateAsMoment.format('HH:MMa, D MMMM, YYYY')
        : dateAsMoment.format('MM/DD/YYYY LT');
};
exports.splitOnCapitalLetter = (input) => input.split(/(?=[A-Z])/).join(' ');
exports.stringToUnpadded = (input) => input.replace(/\0/g, '');
exports.hexToUnpaddedString = (hex) => exports.stringToUnpadded(util_1.isHex(hex) ? util_1.hexToString(hex) : hex);
exports.stringToPadded = (string, maxChars = 12) => `${string}${'\u0000'.repeat(maxChars - string.length)}`;
exports.stringToPaddedHex = (string) => util_1.stringToHex(exports.stringToPadded(string));
exports.formatNumberForChain = (number) => {
    return Number(number) * Math.pow(10, 6);
};
exports.formatNumberFromChain = (number) => {
    return Number(number) / Math.pow(10, 6);
};
//# sourceMappingURL=formatters.js.map