"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConvertDecimals = exports.formatWei = exports.formatFeePct = exports.createFormatFunction = exports.formatWithMaxDecimals = void 0;
exports.createShortenedString = createShortenedString;
exports.utf8ToHex = utf8ToHex;
exports.hexToUtf8 = hexToUtf8;
exports.bnToHex = bnToHex;
exports.convertFromWei = convertFromWei;
exports.formatGwei = formatGwei;
exports.shortenHexStrings = shortenHexStrings;
exports.parseWinston = parseWinston;
exports.formatWinston = formatWinston;
const tslib_1 = require("tslib");
const ethers_1 = require("ethers");
const BigNumberUtils_1 = require("./BigNumberUtils");
const common_1 = require("./common");
const assert_1 = tslib_1.__importDefault(require("assert"));
const bignumber_js_1 = require("bignumber.js");
const formatWithMaxDecimals = (num, decimalPlaces, minPrecision, roundUp, showSign) => {
    if (roundUp) {
        bignumber_js_1.BigNumber.set({ ROUNDING_MODE: bignumber_js_1.BigNumber.ROUND_UP });
    }
    else {
        bignumber_js_1.BigNumber.set({ ROUNDING_MODE: bignumber_js_1.BigNumber.ROUND_DOWN });
    }
    const fullPrecisionFloat = new bignumber_js_1.BigNumber(num);
    const positiveSign = showSign && fullPrecisionFloat.gt(0) ? "+" : "";
    let fixedPrecisionFloat;
    if (fullPrecisionFloat.abs().gte(new bignumber_js_1.BigNumber(1))) {
        fixedPrecisionFloat = new bignumber_js_1.BigNumber(fullPrecisionFloat).toFixed(decimalPlaces).toString();
    }
    else {
        fixedPrecisionFloat = new bignumber_js_1.BigNumber(fullPrecisionFloat).toPrecision(minPrecision).toString();
    }
    const fixedPrecisionFloatParts = fixedPrecisionFloat.split(".");
    fixedPrecisionFloatParts[0] = fixedPrecisionFloatParts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    return positiveSign + fixedPrecisionFloatParts.join(".");
};
exports.formatWithMaxDecimals = formatWithMaxDecimals;
const createFormatFunction = (numDisplayedDecimals, minDisplayedPrecision, showSign = false, decimals = 18) => {
    return (valInWei) => (0, exports.formatWithMaxDecimals)((0, exports.formatWei)((0, exports.ConvertDecimals)(decimals, 18)(valInWei)), numDisplayedDecimals, minDisplayedPrecision, false, showSign);
};
exports.createFormatFunction = createFormatFunction;
const formatFeePct = (relayerFeePct) => {
    return (0, exports.createFormatFunction)(2, 4, false, 16)((0, BigNumberUtils_1.toBN)(relayerFeePct).toString());
};
exports.formatFeePct = formatFeePct;
function createShortenedString(hex, maxLength = 12, delimiter = "..") {
    if (hex.length <= maxLength) {
        return hex;
    }
    const maxAvailable = maxLength - delimiter.length;
    if (maxAvailable <= 0) {
        throw new Error("Invalid max length");
    }
    const leftCharacters = Math.floor(maxAvailable / 2 + 0.5);
    const rightCharacters = Math.floor(maxAvailable / 2);
    return `${hex.substring(0, leftCharacters)}${delimiter}${hex.substring(hex.length - rightCharacters)}`;
}
function utf8ToHex(input) {
    return ethers_1.utils.formatBytes32String(input);
}
function hexToUtf8(input) {
    return ethers_1.utils.toUtf8String(input);
}
function bnToHex(input) {
    return ethers_1.utils.hexZeroPad(ethers_1.utils.hexlify((0, BigNumberUtils_1.toBN)(input)), 32);
}
function convertFromWei(weiVal, decimals) {
    const formatFunction = (0, exports.createFormatFunction)(2, 4, false, decimals);
    return formatFunction(weiVal);
}
function formatGwei(weiVal) {
    return convertFromWei(weiVal, 9);
}
function shortenHexStrings(addresses) {
    return addresses.map((h) => createShortenedString(h));
}
const formatWei = (num) => {
    return (0, common_1.fromWei)(num.toString());
};
exports.formatWei = formatWei;
const ConvertDecimals = (fromDecimals, toDecimals) => {
    (0, assert_1.default)(fromDecimals >= 0, "requires fromDecimals as an integer >= 0");
    (0, assert_1.default)(toDecimals >= 0, "requires toDecimals as an integer >= 0");
    return (amountIn) => {
        const amount = (0, BigNumberUtils_1.toBN)(amountIn.toString());
        if (amount.isZero())
            return amount;
        const diff = fromDecimals - toDecimals;
        if (diff == 0)
            return amount;
        if (diff > 0)
            return amount.div((0, BigNumberUtils_1.toBN)("10").pow((0, BigNumberUtils_1.toBN)(diff.toString())));
        return amount.mul((0, BigNumberUtils_1.toBN)("10").pow((0, BigNumberUtils_1.toBN)((-1 * diff).toString())));
    };
};
exports.ConvertDecimals = ConvertDecimals;
function parseWinston(numericString) {
    return (0, BigNumberUtils_1.parseUnits)(numericString, 12);
}
function formatWinston(winstonValue) {
    return (0, BigNumberUtils_1.formatUnits)(winstonValue, 12);
}
//# sourceMappingURL=FormattingUtils.js.map