"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseEscrowLockedBalance = exports.parseOrmlTokensAccountData = exports.ChainBalance = exports.parseEscrowPoint = exports.tickerToMonetaryCurrency = exports.currencyIdToLiteral = exports.currencyIdLiteralToMonetaryCurrency = exports.currencyIdToMonetaryCurrency = exports.tickerToCurrencyIdLiteral = exports.VoteUnit = exports.VotingCurrency = exports.GovernanceUnit = exports.GovernanceCurrency = exports.WrappedAmount = exports.WrappedCurrency = exports.CurrencyUnit = exports.CollateralUnit = exports.CollateralCurrency = exports.CollateralAmount = exports.CurrencyIdLiteral = void 0;
const monetary_js_1 = require("@interlay/monetary-js");
const utils_1 = require("../utils");
var CurrencyIdLiteral;
(function (CurrencyIdLiteral) {
    CurrencyIdLiteral["DOT"] = "DOT";
    CurrencyIdLiteral["INTERBTC"] = "IBTC";
    CurrencyIdLiteral["INTR"] = "INTR";
    CurrencyIdLiteral["KSM"] = "KSM";
    CurrencyIdLiteral["KBTC"] = "KBTC";
    CurrencyIdLiteral["KINT"] = "KINT";
})(CurrencyIdLiteral = exports.CurrencyIdLiteral || (exports.CurrencyIdLiteral = {}));
exports.CollateralAmount = [monetary_js_1.PolkadotAmount, monetary_js_1.KusamaAmount, monetary_js_1.InterlayAmount, monetary_js_1.KintsugiAmount];
exports.CollateralCurrency = [monetary_js_1.Polkadot, monetary_js_1.Kusama, monetary_js_1.Interlay, monetary_js_1.Kintsugi];
exports.CollateralUnit = [monetary_js_1.PolkadotUnit, monetary_js_1.KusamaUnit, monetary_js_1.InterlayUnit, monetary_js_1.KintsugiUnit];
exports.CurrencyUnit = [monetary_js_1.BitcoinUnit, monetary_js_1.PolkadotUnit, monetary_js_1.KusamaUnit, monetary_js_1.KintsugiUnit, monetary_js_1.InterlayUnit];
exports.WrappedCurrency = [monetary_js_1.InterBtc, monetary_js_1.KBtc];
exports.WrappedAmount = [monetary_js_1.InterBtcAmount, monetary_js_1.KBtcAmount];
exports.GovernanceCurrency = [monetary_js_1.Interlay, monetary_js_1.Kintsugi];
exports.GovernanceUnit = [monetary_js_1.InterlayUnit, monetary_js_1.KintsugiUnit];
exports.VotingCurrency = [monetary_js_1.VoteInterlay, monetary_js_1.VoteKintsugi];
exports.VoteUnit = exports.GovernanceUnit;
function tickerToCurrencyIdLiteral(ticker) {
    switch (ticker) {
        case monetary_js_1.Polkadot.ticker: {
            return CurrencyIdLiteral.DOT;
        }
        case monetary_js_1.Kusama.ticker: {
            return CurrencyIdLiteral.KSM;
        }
        case monetary_js_1.KBtc.ticker: {
            return CurrencyIdLiteral.KBTC;
        }
        case monetary_js_1.InterBtc.ticker: {
            return CurrencyIdLiteral.INTERBTC;
        }
        case monetary_js_1.Kintsugi.ticker: {
            return CurrencyIdLiteral.KINT;
        }
        case monetary_js_1.Interlay.ticker: {
            return CurrencyIdLiteral.INTR;
        }
    }
    throw new Error("No CurrencyId entry for provided ticker");
}
exports.tickerToCurrencyIdLiteral = tickerToCurrencyIdLiteral;
function currencyIdToMonetaryCurrency(currencyId) {
    // The currencyId is always a token, since it is just a tuple struct
    if (!currencyId.isToken) {
        throw new Error("The currency ID must be a token");
    }
    const token = currencyId.asToken;
    if (token.isIbtc) {
        return monetary_js_1.InterBtc;
    }
    else if (token.isDot) {
        return monetary_js_1.Polkadot;
    }
    else if (token.isKsm) {
        return monetary_js_1.Kusama;
    }
    else if (token.isKbtc) {
        return monetary_js_1.KBtc;
    }
    else if (token.isKint) {
        return monetary_js_1.Kintsugi;
    }
    else if (token.isIntr) {
        return monetary_js_1.Interlay;
    }
    throw new Error("No CurrencyId entry for provided ticker");
}
exports.currencyIdToMonetaryCurrency = currencyIdToMonetaryCurrency;
function currencyIdLiteralToMonetaryCurrency(api, currencyIdLiteral) {
    return currencyIdToMonetaryCurrency(utils_1.newCurrencyId(api, currencyIdLiteral));
}
exports.currencyIdLiteralToMonetaryCurrency = currencyIdLiteralToMonetaryCurrency;
function currencyIdToLiteral(currencyId) {
    const monetaryCurrency = currencyIdToMonetaryCurrency(currencyId);
    return tickerToCurrencyIdLiteral(monetaryCurrency.ticker);
}
exports.currencyIdToLiteral = currencyIdToLiteral;
function tickerToMonetaryCurrency(api, ticker) {
    const currencyIdLiteral = tickerToCurrencyIdLiteral(ticker);
    return currencyIdToMonetaryCurrency(utils_1.newCurrencyId(api, currencyIdLiteral));
}
exports.tickerToMonetaryCurrency = tickerToMonetaryCurrency;
function parseEscrowPoint(e) {
    return {
        bias: e.bias.toBn(),
        slope: e.slope.toBn(),
        ts: e.ts.toBn(),
    };
}
exports.parseEscrowPoint = parseEscrowPoint;
class ChainBalance {
    constructor(currency, free, transferable, reserved) {
        this.currency = currency;
        this.free = utils_1.newMonetaryAmount(free || 0, currency);
        this.transferable = utils_1.newMonetaryAmount(transferable || 0, currency);
        this.reserved = utils_1.newMonetaryAmount(reserved || 0, currency);
    }
    /*
        First stringifies the `MonetaryAmount`s, then the entire object.
        Allows for simple comparison in tests.
    */
    toString(base) {
        const stringifiedChainBalance = Object.fromEntries(Object.entries(this).map(([k, v]) => [k, v.toString(base || this.currency.base)]));
        return JSON.stringify(stringifiedChainBalance);
    }
}
exports.ChainBalance = ChainBalance;
function parseOrmlTokensAccountData(data, currency) {
    return new ChainBalance(currency, data.free.toString(), data.free.sub(data.frozen).toString(), data.reserved.toString());
}
exports.parseOrmlTokensAccountData = parseOrmlTokensAccountData;
function parseEscrowLockedBalance(governanceCurrency, escrowLockedBalance) {
    return {
        amount: utils_1.newMonetaryAmount(escrowLockedBalance.amount.toString(), governanceCurrency),
        endBlock: escrowLockedBalance.end.toNumber(),
    };
}
exports.parseEscrowLockedBalance = parseEscrowLockedBalance;
//# sourceMappingURL=currency.js.map