import { BigNumber } from "bignumber.js";
import { CosmosValidatorsManager } from "@ledgerhq/coin-cosmos/CosmosValidatorsManager";
import cryptoFactory from "@ledgerhq/coin-cosmos/chain/chain";
import { asSafeCosmosPreloadData, setCosmosPreloadData } from "@ledgerhq/coin-cosmos/preloadedData";
import mockPreloadedData from "@ledgerhq/coin-cosmos/preloadedData.mock";
import { assignFromAccountRaw, assignToAccountRaw } from "@ledgerhq/coin-cosmos/serialization";
import { getMainAccount } from "@ledgerhq/coin-framework/account/index";
import { getSerializedAddressParameters, updateTransaction, } from "@ledgerhq/coin-framework/bridge/jsHelpers";
import { getCryptoCurrencyById } from "@ledgerhq/cryptoassets/currencies";
import { AmountRequired, FeeTooHigh, InvalidAddress, NotEnoughBalance, RecipientRequired, } from "@ledgerhq/errors";
import { broadcast, isInvalidRecipient, makeAccountBridgeReceive, scanAccounts, signOperation, signRawOperation, sync, } from "../../../bridge/mockHelpers";
import { getCurrencyConfiguration } from "../../../config";
import { validateAddress } from "../../../bridge/validateAddress";
const receive = makeAccountBridgeReceive();
const defaultGetFees = (a, t) => (t.fees || new BigNumber(0)).times(t.gas || new BigNumber(0));
const createTransaction = () => ({
    family: "cosmos",
    mode: "send",
    amount: new BigNumber(0),
    recipient: "",
    fees: null,
    gas: null,
    memo: null,
    validators: [],
    sourceValidator: null,
    networkInfo: null,
    useAllAmount: false,
});
const estimateMaxSpendable = ({ account, parentAccount, transaction }) => {
    const mainAccount = getMainAccount(account, parentAccount);
    const estimatedFees = transaction
        ? defaultGetFees(mainAccount, transaction)
        : new BigNumber(5000);
    return Promise.resolve(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(t.amount).plus(estimatedFees);
    const amount = useAllAmount ? account.balance.minus(estimatedFees) : new BigNumber(t.amount);
    if (amount.gt(0) && estimatedFees.times(10).gt(amount)) {
        warnings.feeTooHigh = new FeeTooHigh();
    }
    // Fill up transaction errors...
    if (totalSpent.gt(account.balance)) {
        errors.amount = new NotEnoughBalance();
    }
    // Fill up recipient errors...
    if (t.mode === "send") {
        if (!t.recipient) {
            errors.recipient = new RecipientRequired("");
        }
        else if (isInvalidRecipient(t.recipient)) {
            errors.recipient = new InvalidAddress("");
        }
    }
    if (amount.eq(0)) {
        errors.amount = new AmountRequired();
    }
    return Promise.resolve({
        errors,
        warnings,
        estimatedFees,
        amount,
        totalSpent,
    });
};
const prepareTransaction = async (a, t) => {
    if (!t.networkInfo) {
        return {
            ...t,
            gas: new BigNumber(1),
            fees: new BigNumber(375),
            networkInfo: {
                family: "cosmos",
                fees: new BigNumber(500),
            },
        };
    }
    return t;
};
const accountBridge = {
    estimateMaxSpendable,
    createTransaction,
    updateTransaction,
    getTransactionStatus,
    prepareTransaction,
    sync,
    receive,
    signOperation,
    signRawOperation,
    broadcast,
    assignFromAccountRaw,
    assignToAccountRaw,
    getSerializedAddressParameters,
    validateAddress,
};
const currencyBridge = {
    scanAccounts,
    preload: (currency) => {
        const config = getCurrencyConfiguration(currency);
        setCosmosPreloadData("cosmos", mockPreloadedData);
        return Promise.resolve({ validators: mockPreloadedData, config });
    },
    hydrate: (data, currency) => {
        if (!data || typeof data !== "object")
            return;
        const relatedImpl = cryptoFactory(currency.id);
        relatedImpl.lcd = data.config.lcd;
        relatedImpl.minGasPrice = data.config.minGasPrice;
        relatedImpl.ledgerValidator = data.config.ledgerValidator;
        const { validators } = data;
        if (!validators || typeof validators !== "object" || !Array.isArray(validators))
            return;
        const cosmosValidatorsManager = new CosmosValidatorsManager(getCryptoCurrencyById("cosmos"));
        cosmosValidatorsManager.hydrateValidators(validators);
        setCosmosPreloadData("cosmos", asSafeCosmosPreloadData(data));
    },
};
export default {
    currencyBridge,
    accountBridge,
};
//# sourceMappingURL=mock.js.map