"use strict";
// TODO: update path by moving mockHelpers to coin-framework
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = require("bignumber.js");
const errors_1 = require("@ledgerhq/errors");
const mockHelpers_1 = require("../../../bridge/mockHelpers");
const jsHelpers_1 = require("@ledgerhq/coin-framework/bridge/jsHelpers");
const index_1 = require("@ledgerhq/coin-framework/account/index");
const serialization_1 = require("@ledgerhq/coin-multiversx/serialization");
const validateAddress_1 = require("../../../bridge/validateAddress");
const receive = (0, mockHelpers_1.makeAccountBridgeReceive)();
const defaultGetFees = (a, t) => t.fees || new bignumber_js_1.BigNumber(0);
const createTransaction = () => ({
    family: "multiversx",
    mode: "send",
    amount: new bignumber_js_1.BigNumber(0),
    recipient: "",
    fees: null,
    useAllAmount: false,
    gasLimit: 0,
});
const estimateMaxSpendable = ({ account, parentAccount, transaction }) => {
    const mainAccount = (0, index_1.getMainAccount)(account, parentAccount);
    const estimatedFees = transaction
        ? defaultGetFees(mainAccount, transaction)
        : new bignumber_js_1.BigNumber(5000);
    return Promise.resolve(bignumber_js_1.BigNumber.max(0, account.balance.minus(estimatedFees)));
};
const getTransactionStatus = (account, transaction) => {
    const errors = {};
    const warnings = {};
    const useAllAmount = !!transaction.useAllAmount;
    const estimatedFees = defaultGetFees(account, transaction);
    const totalSpent = useAllAmount
        ? account.balance
        : new bignumber_js_1.BigNumber(transaction.amount).plus(estimatedFees);
    const amount = useAllAmount
        ? account.balance.minus(estimatedFees)
        : new bignumber_js_1.BigNumber(transaction.amount);
    if (amount.gt(0) && estimatedFees.times(10).gt(amount)) {
        warnings.feeTooHigh = new errors_1.FeeTooHigh();
    }
    // Fill up transaction errors
    if (totalSpent.gt(account.balance)) {
        errors.amount = new errors_1.NotEnoughBalance();
    }
    // Fill up recipient errors
    if (!transaction.recipient) {
        errors.recipient = new errors_1.RecipientRequired("");
    }
    else if ((0, mockHelpers_1.isInvalidRecipient)(transaction.recipient)) {
        errors.recipient = new errors_1.InvalidAddress("", {
            currencyName: account.currency.name,
        });
    }
    return Promise.resolve({
        errors,
        warnings,
        estimatedFees,
        amount,
        totalSpent,
    });
};
const prepareTransaction = async (a, t) => {
    if (!t.fees) {
        return { ...t, fees: new bignumber_js_1.BigNumber(500) };
    }
    return t;
};
const accountBridge = {
    estimateMaxSpendable,
    createTransaction,
    updateTransaction: jsHelpers_1.updateTransaction,
    getTransactionStatus,
    prepareTransaction,
    sync: mockHelpers_1.sync,
    receive,
    assignToAccountRaw: serialization_1.assignToAccountRaw,
    assignFromAccountRaw: serialization_1.assignFromAccountRaw,
    signOperation: mockHelpers_1.signOperation,
    signRawOperation: mockHelpers_1.signRawOperation,
    broadcast: mockHelpers_1.broadcast,
    getSerializedAddressParameters: jsHelpers_1.getSerializedAddressParameters,
    validateAddress: validateAddress_1.validateAddress,
};
const currencyBridge = {
    scanAccounts: mockHelpers_1.scanAccounts,
    preload: () => {
        return Promise.resolve({});
    },
    hydrate: () => { },
};
exports.default = {
    currencyBridge,
    accountBridge,
};
//# sourceMappingURL=mock.js.map