"use strict";
/* eslint-disable @typescript-eslint/no-var-requires */
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const banner_1 = require("./banner");
const preloadedData = __importStar(require("@ledgerhq/coin-cosmos/preloadedData"));
const preloadedData_mock_1 = __importDefault(require("@ledgerhq/coin-cosmos/preloadedData.mock"));
const bignumber_js_1 = require("bignumber.js");
const LiveConfig_1 = require("@ledgerhq/live-config/LiveConfig");
const sharedConfig_1 = require("../../config/sharedConfig");
const chain_1 = __importDefault(require("@ledgerhq/coin-cosmos/chain/chain"));
jest.mock("@ledgerhq/coin-cosmos/prepareTransaction", () => ({
    calculateFees: jest.fn(() => Promise.resolve({})),
}));
jest.mock("@ledgerhq/coin-cosmos/chain/chain");
jest.mock("@ledgerhq/coin-cosmos/logic", () => ({
    ...jest.requireActual("@ledgerhq/coin-cosmos/logic"),
    canDelegate: jest.fn(),
    canRedelegate: jest.fn(),
}));
LiveConfig_1.LiveConfig.setConfig(sharedConfig_1.liveConfig);
const LEDGER_VALIDATOR_ADDRESS = LiveConfig_1.LiveConfig.getValueByKey("config_currency_cosmos").ledgerValidator;
const ledgerValidator = preloadedData_mock_1.default.validators.find(x => x.validatorAddress === LEDGER_VALIDATOR_ADDRESS);
const expensiveValidator = preloadedData_mock_1.default.validators.find(x => x.validatorAddress === "cosmosvaloper1qs8tnw2t8l6amtzvdemnnsq9dzk0ag0z52uzay");
const cheapValidator = preloadedData_mock_1.default.validators.find(x => x.validatorAddress === "cosmosvaloper1qaa9zej9a0ge3ugpx3pxyx602lxh3ztqgfnp42");
const account = {
    type: "Account",
    id: "js:2:cosmos:cosmos1f9y7wdychcdhwvyrhff3zvs3gy3qxcu2th4g8u:",
    used: false,
    seedIdentifier: "02d4c121ce2bb160ebf39aa0be0050b4d553e18872985ac3a4e21904fd1442defe",
    derivationMode: "",
    index: 1,
    freshAddress: "cosmos1f9y7wdychcdhwvyrhff3zvs3gy3qxcu2th4g8u",
    freshAddressPath: "44'/118'/1'/0/0",
    blockHeight: 5417472,
    creationDate: new Date("2022-08-02T16:09:08.906Z"),
    balance: new bignumber_js_1.BigNumber("200250"),
    spendableBalance: new bignumber_js_1.BigNumber("200250"),
    operations: [],
    operationsCount: 1,
    pendingOperations: [],
    currency: {
        type: "CryptoCurrency",
        id: "cosmos",
        coinType: 118,
        name: "Cosmos",
        managerAppName: "Cosmos",
        ticker: "ATOM",
        scheme: "cosmos",
        color: "#16192f",
        family: "cosmos",
        units: [
            { name: "Atom", code: "ATOM", magnitude: 6 },
            { name: "microAtom", code: "uatom", magnitude: 0 },
        ],
        explorerViews: [
            {
                tx: "https://www.mintscan.io/cosmos/txs/$hash",
                address: "https://www.mintscan.io/cosmos/validators/$address",
            },
        ],
    },
    lastSyncDate: new Date("2022-08-02T16:11:47.343Z"),
    swapHistory: [],
    balanceHistoryCache: {
        HOUR: { balances: [0, 393248, 393248], latestDate: 1661162400000 },
        DAY: { balances: [0, 393248], latestDate: 1661122800000 },
        WEEK: { balances: [0, 393248], latestDate: 1661036400000 },
    },
    xpub: "cosmos1f9y7wdychcdhwvyrhff3zvs3gy3qxcu2th4g8u",
    cosmosResources: {
        delegations: [
            {
                amount: new bignumber_js_1.BigNumber("50000"),
                status: "bonded",
                pendingRewards: new bignumber_js_1.BigNumber("112"),
                validatorAddress: "cosmosvaloper1c4k24jzduc365kywrsvf5ujz4ya6mwympnc4en",
            },
        ],
        redelegations: [],
        unbondings: [],
        delegatedBalance: new bignumber_js_1.BigNumber("0"),
        pendingRewardsBalance: new bignumber_js_1.BigNumber("0"),
        unbondingBalance: new bignumber_js_1.BigNumber("0"),
        withdrawAddress: "",
        sequence: 0,
    },
};
const validators = [expensiveValidator, cheapValidator, ledgerValidator];
const validatorsMap = {
    cosmos: { validators },
};
describe("cosmos/banner", () => {
    describe("useCosmosFormattedDelegations", () => {
        beforeEach(() => {
            // @ts-expect-error Ledger value come from config
            chain_1.default.mockReturnValue({
                ledgerValidator: LEDGER_VALIDATOR_ADDRESS,
            });
        });
        afterEach(() => {
            jest.restoreAllMocks();
        });
        it("should not display the banner", async () => {
            jest.spyOn(preloadedData, "getCurrentCosmosPreloadData").mockReturnValue(validatorsMap);
            require("@ledgerhq/coin-cosmos/logic").canDelegate.mockReturnValue(false);
            require("@ledgerhq/coin-cosmos/logic").canRedelegate.mockReturnValue(false);
            const result = (0, banner_1.getAccountBannerState)(account);
            expect(result).toStrictEqual({
                display: false,
                redelegate: false,
                validatorSrcAddress: "",
                ledgerValidator,
            });
        });
        it("should return display delegate mode", async () => {
            jest.spyOn(preloadedData, "getCurrentCosmosPreloadData").mockReturnValue(validatorsMap);
            require("@ledgerhq/coin-cosmos/logic").canDelegate.mockReturnValue(true);
            require("@ledgerhq/coin-cosmos/logic").canRedelegate.mockReturnValue(false);
            const result = (0, banner_1.getAccountBannerState)(account);
            expect(result).toStrictEqual({
                display: true,
                redelegate: false,
                validatorSrcAddress: "",
                ledgerValidator,
            });
        });
        it("should return display redelegate mode", async () => {
            jest.spyOn(preloadedData, "getCurrentCosmosPreloadData").mockReturnValue(validatorsMap);
            require("@ledgerhq/coin-cosmos/logic").canDelegate.mockReturnValue(false);
            require("@ledgerhq/coin-cosmos/logic").canRedelegate.mockReturnValue(true);
            account.cosmosResources.redelegations.push({
                validatorSrcAddress: "xxxx",
                validatorDstAddress: expensiveValidator?.validatorAddress,
                amount: new bignumber_js_1.BigNumber(1000),
                completionDate: new Date(),
            });
            const accountWithSpendable5000 = {
                ...account,
                spendableBalance: new bignumber_js_1.BigNumber(5000),
            };
            const result = (0, banner_1.getAccountBannerState)(accountWithSpendable5000);
            expect(result).toStrictEqual({
                display: true,
                redelegate: true,
                validatorSrcAddress: expensiveValidator?.validatorAddress,
                ledgerValidator,
            });
        });
        it("should return not display redelegate mode", async () => {
            jest.spyOn(preloadedData, "getCurrentCosmosPreloadData").mockReturnValue(validatorsMap);
            require("@ledgerhq/coin-cosmos/logic").canDelegate.mockReturnValue(false);
            require("@ledgerhq/coin-cosmos/logic").canRedelegate.mockReturnValue(false);
            account.cosmosResources.redelegations.push({
                validatorSrcAddress: "xxxx",
                validatorDstAddress: expensiveValidator?.validatorAddress,
                amount: new bignumber_js_1.BigNumber(1000),
                completionDate: new Date(),
            });
            const result = (0, banner_1.getAccountBannerState)(account);
            expect(result).toStrictEqual({
                display: false,
                redelegate: false,
                validatorSrcAddress: "",
                ledgerValidator,
            });
        });
    });
});
//# sourceMappingURL=banner.test.js.map