"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = require("bignumber.js");
const errors_1 = require("@ledgerhq/errors");
const jsHelpers_1 = require("@ledgerhq/coin-framework/bridge/jsHelpers");
const mockHelpers_1 = require("../../../bridge/mockHelpers");
const mockHelpers_2 = require("../../../bridge/mockHelpers");
const validateAddress_1 = require("../../../bridge/validateAddress");
const receive = (0, mockHelpers_2.makeAccountBridgeReceive)();
const createTransaction = () => ({
    family: "tron",
    amount: new bignumber_js_1.BigNumber(0),
    useAllAmount: false,
    mode: "send",
    duration: 3,
    recipient: "",
    networkInfo: null,
    resource: null,
    votes: [],
});
const updateTransaction = (t, patch) => ({ ...t, ...patch });
const estimateMaxSpendable = ({ account }) => {
    return account.balance;
};
const getTransactionStatus = (a, t) => {
    const errors = {};
    const warnings = {};
    const tokenAccount = !t.subAccountId
        ? null
        : a.subAccounts && a.subAccounts.find(ta => ta.id === t.subAccountId);
    const account = tokenAccount || a;
    const useAllAmount = !!t.useAllAmount;
    const estimatedFees = (0, bignumber_js_1.BigNumber)(0);
    const totalSpent = useAllAmount
        ? account.balance
        : tokenAccount
            ? new bignumber_js_1.BigNumber(t.amount)
            : new bignumber_js_1.BigNumber(t.amount).plus(estimatedFees);
    const amount = useAllAmount
        ? tokenAccount
            ? new bignumber_js_1.BigNumber(t.amount)
            : account.balance.minus(estimatedFees)
        : new bignumber_js_1.BigNumber(t.amount);
    // Fill up transaction errors...
    if (totalSpent.gt(account.balance)) {
        errors.amount = new errors_1.NotEnoughBalance();
    }
    // Fill up recipient errors...
    if (!t.recipient) {
        errors.recipient = new errors_1.RecipientRequired("");
    }
    else if ((0, mockHelpers_1.isInvalidRecipient)(t.recipient)) {
        errors.recipient = new errors_1.InvalidAddress("");
    }
    return Promise.resolve({
        errors,
        warnings,
        estimatedFees,
        amount,
        totalSpent,
    });
};
const prepareTransaction = async (a, t) => {
    let res = t;
    if (!res.networkInfo) {
        const freeNetUsed = 0, freeNetLimit = 0, NetUsed = 0, NetLimit = 0, EnergyUsed = 0, EnergyLimit = 0;
        res = {
            ...res,
            networkInfo: {
                family: "tron",
                freeNetUsed: new bignumber_js_1.BigNumber(freeNetUsed),
                freeNetLimit: new bignumber_js_1.BigNumber(freeNetLimit),
                netUsed: new bignumber_js_1.BigNumber(NetUsed),
                netLimit: new bignumber_js_1.BigNumber(NetLimit),
                energyUsed: new bignumber_js_1.BigNumber(EnergyUsed),
                energyLimit: new bignumber_js_1.BigNumber(EnergyLimit),
            },
        };
    }
    return res;
};
const accountBridge = {
    createTransaction,
    updateTransaction,
    getTransactionStatus,
    estimateMaxSpendable,
    prepareTransaction,
    sync: mockHelpers_1.sync,
    receive,
    signOperation: mockHelpers_1.signOperation,
    signRawOperation: mockHelpers_1.signRawOperation,
    broadcast: mockHelpers_1.broadcast,
    getSerializedAddressParameters: jsHelpers_1.getSerializedAddressParameters,
    validateAddress: validateAddress_1.validateAddress,
};
const currencyBridge = {
    preload: () => Promise.resolve({}),
    hydrate: () => { },
    scanAccounts: mockHelpers_1.scanAccounts,
};
exports.default = {
    currencyBridge,
    accountBridge,
};
//# sourceMappingURL=mock.js.map