"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 CosmosValidatorsManager_1 = require("@ledgerhq/coin-cosmos/CosmosValidatorsManager");
const chain_1 = __importDefault(require("@ledgerhq/coin-cosmos/chain/chain"));
const preloadedData_1 = require("@ledgerhq/coin-cosmos/preloadedData");
const preloadedData_mock_1 = __importDefault(require("@ledgerhq/coin-cosmos/preloadedData.mock"));
const serialization_1 = require("@ledgerhq/coin-cosmos/serialization");
const index_1 = require("@ledgerhq/coin-framework/account/index");
const jsHelpers_1 = require("@ledgerhq/coin-framework/bridge/jsHelpers");
const currencies_1 = require("@ledgerhq/cryptoassets/currencies");
const errors_1 = require("@ledgerhq/errors");
const mockHelpers_1 = require("../../../bridge/mockHelpers");
const config_1 = require("../../../config");
const validateAddress_1 = require("../../../bridge/validateAddress");
const receive = (0, mockHelpers_1.makeAccountBridgeReceive)();
const defaultGetFees = (a, t) => (t.fees || new bignumber_js_1.BigNumber(0)).times(t.gas || new bignumber_js_1.BigNumber(0));
const createTransaction = () => ({
    family: "cosmos",
    mode: "send",
    amount: new bignumber_js_1.BigNumber(0),
    recipient: "",
    fees: null,
    gas: null,
    memo: null,
    validators: [],
    sourceValidator: null,
    networkInfo: null,
    useAllAmount: false,
});
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, 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 (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 (t.mode === "send") {
        if (!t.recipient) {
            errors.recipient = new errors_1.RecipientRequired("");
        }
        else if ((0, mockHelpers_1.isInvalidRecipient)(t.recipient)) {
            errors.recipient = new errors_1.InvalidAddress("");
        }
    }
    if (amount.eq(0)) {
        errors.amount = new errors_1.AmountRequired();
    }
    return Promise.resolve({
        errors,
        warnings,
        estimatedFees,
        amount,
        totalSpent,
    });
};
const prepareTransaction = async (a, t) => {
    if (!t.networkInfo) {
        return {
            ...t,
            gas: new bignumber_js_1.BigNumber(1),
            fees: new bignumber_js_1.BigNumber(375),
            networkInfo: {
                family: "cosmos",
                fees: new bignumber_js_1.BigNumber(500),
            },
        };
    }
    return t;
};
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,
    assignFromAccountRaw: serialization_1.assignFromAccountRaw,
    assignToAccountRaw: serialization_1.assignToAccountRaw,
    getSerializedAddressParameters: jsHelpers_1.getSerializedAddressParameters,
    validateAddress: validateAddress_1.validateAddress,
};
const currencyBridge = {
    scanAccounts: mockHelpers_1.scanAccounts,
    preload: (currency) => {
        const config = (0, config_1.getCurrencyConfiguration)(currency);
        (0, preloadedData_1.setCosmosPreloadData)("cosmos", preloadedData_mock_1.default);
        return Promise.resolve({ validators: preloadedData_mock_1.default, config });
    },
    hydrate: (data, currency) => {
        if (!data || typeof data !== "object")
            return;
        const relatedImpl = (0, chain_1.default)(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_1.CosmosValidatorsManager((0, currencies_1.getCryptoCurrencyById)("cosmos"));
        cosmosValidatorsManager.hydrateValidators(validators);
        (0, preloadedData_1.setCosmosPreloadData)("cosmos", (0, preloadedData_1.asSafeCosmosPreloadData)(data));
    },
};
exports.default = {
    currencyBridge,
    accountBridge,
};
//# sourceMappingURL=mock.js.map