"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDecimalsCoefficient = exports.getDecimals = exports.DEFAULT_DECIMALS = exports.u64FromCurrency = exports.currencyFromU64 = void 0;
const bn_1 = require("@polkadot/util/bn/bn");
const capitalize = (str) => str.slice(0, 1).toUpperCase() + str.slice(1);
const currencyFromU64 = (u64) => {
    const bytes = [];
    let num = typeof u64 === "number" ? new bn_1.BN(u64) : u64;
    do {
        bytes.unshift(num.modn(256));
        num = num.divn(256);
    } while (num.gtn(0));
    return capitalize(Buffer.from(bytes).toString("utf8"));
};
exports.currencyFromU64 = currencyFromU64;
const u64FromCurrency = (currency) => {
    const buf = Buffer.from(currency.toLowerCase());
    const size = buf.length;
    return buf.reduce((val, digit, i) => val + Math.pow(256, size - 1 - i) * digit, 0);
};
exports.u64FromCurrency = u64FromCurrency;
exports.DEFAULT_DECIMALS = 9;
/** @deprecated */
const getDecimals = (api, currency) => {
    const tokenId = api.registry.chainTokens.findIndex(
    // All tokens in chainTokens are uppercased
    (curr) => curr === currency || curr === currency.toUpperCase());
    const decimals = api.registry.chainDecimals[tokenId];
    return decimals || exports.DEFAULT_DECIMALS;
};
exports.getDecimals = getDecimals;
/** @deprecated */
const getDecimalsCoefficient = (api, currency) => {
    const decimals = (0, exports.getDecimals)(api, currency);
    return Math.pow(10, decimals);
};
exports.getDecimalsCoefficient = getDecimalsCoefficient;
