"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @jest-environment jsdom
 */
require("../__tests__/test-helpers/dom-polyfill");
const cryptoassets_1 = require("@ledgerhq/cryptoassets");
const react_1 = require("@testing-library/react");
const account_1 = require("../mock/account");
const _1 = require(".");
const useBridgeTransaction_1 = __importStar(require("./useBridgeTransaction"));
const currencies_1 = require("../currencies");
const LiveConfig_1 = require("@ledgerhq/live-config/lib/LiveConfig");
const BTC = (0, cryptoassets_1.getCryptoCurrencyById)("bitcoin");
(0, currencies_1.setSupportedCurrencies)(["bitcoin"]);
LiveConfig_1.LiveConfig.setConfig({
    config_currency_bitcoin: {
        type: "object",
        default: {
            checkSanctionedAddress: false,
        },
    },
});
describe("useBridgeTransaction", () => {
    test("initialize with a BTC account settles the transaction", async () => {
        const mainAccount = (0, account_1.genAccount)("mocked-account-1", { currency: BTC });
        const { result } = (0, react_1.renderHook)(() => (0, useBridgeTransaction_1.default)(() => ({ account: mainAccount })));
        await (0, react_1.waitFor)(() => {
            expect(result.current.bridgePending).toBeFalsy();
            expect(result.current.bridgeError).toBeFalsy();
            expect(result.current.transaction).not.toBeFalsy();
            expect(result.current.account).not.toBeFalsy();
        });
    });
    test("bridgeError go through", async () => {
        const mainAccount = (0, account_1.genAccount)("mocked-account-1", { currency: BTC });
        const { result } = (0, react_1.renderHook)(() => (0, useBridgeTransaction_1.default)(() => {
            const bridge = (0, _1.getAccountBridge)(mainAccount);
            const transaction = bridge.updateTransaction(bridge.createTransaction(mainAccount), {
                recipient: "criticalcrash",
            });
            return { account: mainAccount, transaction };
        }));
        await (0, react_1.waitFor)(() => expect(result.current.bridgeError).not.toBeFalsy());
    });
    test("bridgeError can be caught with globalOnBridgeError", async () => {
        const before = (0, useBridgeTransaction_1.getGlobalOnBridgeError)();
        try {
            const errors = [];
            (0, useBridgeTransaction_1.setGlobalOnBridgeError)(error => errors.push(error));
            const mainAccount = (0, account_1.genAccount)("mocked-account-1", { currency: BTC });
            (0, react_1.renderHook)(() => (0, useBridgeTransaction_1.default)(() => {
                const bridge = (0, _1.getAccountBridge)(mainAccount);
                const transaction = bridge.updateTransaction(bridge.createTransaction(mainAccount), {
                    recipient: "criticalcrash",
                });
                return { account: mainAccount, transaction };
            }));
            await (0, react_1.waitFor)(() => expect(errors.length).toBe(1));
            expect(errors[0]).toMatchObject(new Error("isInvalidRecipient_mock_criticalcrash"));
        }
        finally {
            (0, useBridgeTransaction_1.setGlobalOnBridgeError)(before);
        }
    });
});
//# sourceMappingURL=useBridgeTransaction.test.js.map