"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const toPairs_1 = __importDefault(require("lodash/toPairs"));
const flatMap_1 = __importDefault(require("lodash/flatMap"));
const groupBy_1 = __importDefault(require("lodash/groupBy"));
const operators_1 = require("rxjs/operators");
require("./test-helpers/setup");
const bridge_1 = require("../bridge");
const live_env_1 = require("@ledgerhq/live-env");
const currencies_1 = require("../currencies");
const account_1 = require("../account");
const rxjs_1 = require("rxjs");
jest.setTimeout(120000);
const mockedCoins = [
    "bitcoin",
    "zcash",
    "ethereum",
    "ethereum_classic",
    "ripple",
    "tezos",
    "stellar",
    "cosmos",
];
mockedCoins.map(currencies_1.getCryptoCurrencyById).forEach(currency => {
    describe("mock " + currency.id, () => {
        (0, live_env_1.setEnv)("MOCK", "true");
        const bridge = (0, bridge_1.getCurrencyBridge)(currency);
        (0, live_env_1.setEnv)("MOCK", "false");
        test("scanAccounts", async () => {
            const accounts = await (0, rxjs_1.firstValueFrom)(bridge
                .scanAccounts({
                currency,
                deviceId: "",
                syncConfig: {
                    paginationConfig: {},
                },
            })
                .pipe((0, operators_1.filter)(e => e.type === "discovered"), (0, operators_1.map)(e => e.account), (0, operators_1.reduce)((all, a) => all.concat(a), [])));
            expect(accounts.length).toBeGreaterThan(0);
            const allOps = (0, flatMap_1.default)((0, account_1.flattenAccounts)(accounts), a => a.operations);
            const operationIdCollisions = (0, toPairs_1.default)((0, groupBy_1.default)(allOps, "id"))
                .filter(([_, coll]) => coll.length > 1)
                .map(([id]) => id);
            expect(operationIdCollisions).toEqual([]);
            const [first, second] = await Promise.all(accounts.map(async (a) => {
                const bridge = (0, bridge_1.getAccountBridge)(a, null);
                const synced = await (0, rxjs_1.firstValueFrom)(bridge
                    .sync(a, {
                    paginationConfig: {},
                })
                    .pipe((0, operators_1.reduce)((a, f) => f(a), a)));
                const m = (0, account_1.toAccountRaw)(a);
                delete m.lastSyncDate;
                delete m.blockHeight;
                expect((0, account_1.toAccountRaw)(synced)).toMatchObject(m);
                return synced;
            }));
            if (first && second) {
                const bridge = (0, bridge_1.getAccountBridge)(first, null);
                let t = bridge.createTransaction(first);
                t = await bridge.prepareTransaction(first, t);
                t = bridge.updateTransaction(t, {
                    recipient: second.freshAddress,
                    amount: second.balance.div(3),
                });
                t = await bridge.prepareTransaction(first, t);
                const s = await bridge.getTransactionStatus(first, t);
                expect(s).toMatchObject({
                    amount: t.amount,
                    errors: {},
                    warnings: {},
                });
                const signedOperation = await (0, rxjs_1.firstValueFrom)(bridge
                    .signOperation({
                    account: first,
                    transaction: t,
                    deviceId: "",
                })
                    .pipe((0, operators_1.filter)(e => e.type === "signed"), (0, operators_1.map)((e) => e.signedOperation)));
                expect(signedOperation.operation).toBeDefined();
                const operation = await bridge.broadcast({
                    account: first,
                    signedOperation,
                });
                expect(operation.hash).toBeTruthy();
                const firstResynced = await (0, rxjs_1.firstValueFrom)(bridge
                    .sync(first, {
                    paginationConfig: {},
                })
                    .pipe((0, operators_1.reduce)((a, f) => f(a), first)));
                expect(firstResynced.operations.length).toBeGreaterThan(first.operations.length);
            }
        });
    });
});
//# sourceMappingURL=mock-bridges.js.map