"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
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 account_1 = require("../../../account");
const factory_1 = __importDefault(require("@ledgerhq/coin-bitcoin/wallet-btc/crypto/factory"));
const utils_1 = require("@ledgerhq/coin-bitcoin/wallet-btc/utils");
const api_1 = require("./api");
const validateAddress_1 = require("../../../bridge/validateAddress");
const receive = (0, mockHelpers_1.makeAccountBridgeReceive)();
const defaultGetFees = (a, t) => (t.feePerByte || new bignumber_js_1.BigNumber(0)).times(250);
const createTransaction = () => ({
    family: "bitcoin",
    amount: new bignumber_js_1.BigNumber(0),
    recipient: "",
    feePerByte: new bignumber_js_1.BigNumber(10),
    networkInfo: null,
    useAllAmount: false,
    rbf: false,
    utxoStrategy: {
        strategy: 0,
        excludeUTXOs: [],
    },
});
const estimateMaxSpendable = ({ account, parentAccount, transaction }) => {
    const mainAccount = (0, account_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, t) => {
    const errors = {};
    const warnings = {};
    const useAllAmount = !!t.useAllAmount;
    const estimatedFees = defaultGetFees(account, t);
    const totalSpent = useAllAmount ? account.balance : new bignumber_js_1.BigNumber(t.amount).plus(estimatedFees);
    const amount = useAllAmount ? account.balance.minus(estimatedFees) : new bignumber_js_1.BigNumber(t.amount);
    if (!errors.amount && !amount.gt(0)) {
        errors.amount = useAllAmount ? new errors_1.NotEnoughBalance() : new errors_1.AmountRequired();
    }
    if (amount.gt(0) && estimatedFees.times(10).gt(amount)) {
        warnings.feeTooHigh = new errors_1.FeeTooHigh();
    }
    // Fill up transaction errors...
    if (!errors.amount && totalSpent.gt(account.balance)) {
        errors.amount = new errors_1.NotEnoughBalance();
    }
    if (t.feePerByte) {
        const txSize = Math.ceil(estimatedFees.toNumber() / t.feePerByte.toNumber());
        const crypto = (0, factory_1.default)(account.currency.id);
        if (amount.gt(0) && amount.lt((0, utils_1.computeDustAmount)(crypto, txSize))) {
            errors.dustLimit = new errors_1.DustLimit();
        }
    }
    // 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("", {
            currencyName: account.currency.name,
        });
    }
    return Promise.resolve({
        errors,
        warnings,
        estimatedFees,
        amount,
        totalSpent,
    });
};
const prepareTransaction = async (account, transaction) => {
    // TODO it needs to set the fee if not in t as well
    if (!transaction.networkInfo) {
        const feeItems = await (0, api_1.getFeeItems)(account.currency);
        return {
            ...transaction,
            networkInfo: {
                family: "bitcoin",
                feeItems: feeItems,
            },
        };
    }
    return transaction;
};
const accountBridge = {
    estimateMaxSpendable,
    createTransaction,
    updateTransaction: jsHelpers_1.updateTransaction,
    getTransactionStatus,
    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 = {
    scanAccounts: mockHelpers_1.scanAccounts,
    preload: () => Promise.resolve({}),
    hydrate: () => { },
};
exports.default = {
    currencyBridge,
    accountBridge,
};
//# sourceMappingURL=mock.js.map