"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = require("bignumber.js");
const errors_1 = require("@ledgerhq/errors");
const account_1 = require("../../../account");
const mockHelpers_1 = require("../../../bridge/mockHelpers");
const jsHelpers_1 = require("@ledgerhq/coin-framework/bridge/jsHelpers");
const utils_1 = require("@ledgerhq/coin-evm/utils");
const transaction_1 = require("@ledgerhq/coin-evm/transaction");
const config_1 = require("../../../config");
const config_2 = require("@ledgerhq/coin-evm/config");
const validateAddress_1 = require("../../../bridge/validateAddress");
const receive = (0, mockHelpers_1.makeAccountBridgeReceive)();
const defaultGetFees = (_a, t) => (t.gasPrice || new bignumber_js_1.BigNumber(0)).times((0, utils_1.getGasLimit)(t));
const createTransaction = () => ({
    family: "evm",
    mode: "send",
    amount: new bignumber_js_1.BigNumber(10000000000),
    nonce: 0,
    recipient: "",
    gasPrice: new bignumber_js_1.BigNumber(10000000000),
    gasLimit: new bignumber_js_1.BigNumber(21000),
    chainId: 2222,
    useAllAmount: false,
    subAccountId: null,
});
const estimateMaxSpendable = ({ account, parentAccount, transaction }) => {
    if (parentAccount)
        return Promise.resolve(account.balance);
    const mainAccount = (0, account_1.getMainAccount)(account, parentAccount);
    let estimatedFees = new bignumber_js_1.BigNumber(1000000000000);
    if (transaction) {
        estimatedFees = defaultGetFees(mainAccount, transaction);
    }
    return Promise.resolve(bignumber_js_1.BigNumber.max(0, account.balance.minus(estimatedFees)));
};
const getTransactionStatus = (account, transaction) => {
    const errors = {};
    const warnings = {};
    let tokenAccount = null;
    if (transaction.subAccountId) {
        tokenAccount = account.subAccounts?.find(ta => ta.id === transaction.subAccountId);
    }
    const currentAccount = tokenAccount || account;
    const useAllAmount = Boolean(transaction.useAllAmount);
    const estimatedFees = defaultGetFees(account, transaction);
    let totalSpent;
    if (useAllAmount) {
        totalSpent = currentAccount.balance;
    }
    else if (tokenAccount) {
        totalSpent = new bignumber_js_1.BigNumber(transaction.amount);
    }
    else {
        totalSpent = new bignumber_js_1.BigNumber(transaction.amount).plus(estimatedFees);
    }
    let amount;
    if (useAllAmount) {
        if (tokenAccount) {
            amount = currentAccount.balance;
        }
        else {
            amount = currentAccount.balance.minus(estimatedFees);
        }
    }
    else {
        amount = new bignumber_js_1.BigNumber(transaction.amount);
    }
    // Fill up transaction errors...
    if (totalSpent.gt(currentAccount.balance)) {
        errors.amount = new errors_1.NotEnoughBalance();
    }
    if (!transaction.recipient) {
        errors.recipient = new errors_1.RecipientRequired("");
    }
    return Promise.resolve({
        errors,
        warnings,
        estimatedFees,
        amount,
        totalSpent,
    });
};
const prepareTransaction = async (_a, t) => {
    const typedTransaction = (0, transaction_1.getTypedTransaction)(t, {
        gasPrice: new bignumber_js_1.BigNumber(50),
        maxFeePerGas: new bignumber_js_1.BigNumber(50),
        maxPriorityFeePerGas: new bignumber_js_1.BigNumber(50),
        nextBaseFee: new bignumber_js_1.BigNumber(50),
    });
    return typedTransaction;
};
let isConfigLoaded = false;
const loadCoinConfig = () => {
    if (!isConfigLoaded) {
        (0, config_2.setCoinConfig)((currency) => {
            isConfigLoaded = true;
            return { info: (0, config_1.getCurrencyConfiguration)(currency) };
        });
    }
};
const accountBridge = {
    createTransaction,
    updateTransaction: jsHelpers_1.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,
    loadCoinConfig,
};
//# sourceMappingURL=mock.js.map