"use strict";
// SPDX-License-Identifier: Apache-2.0
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const MetamaskService_1 = __importDefault(require("./MetamaskService"));
const WalletEvent_1 = require("../../event/WalletEvent");
const Wallet_1 = require("../../../../domain/context/network/Wallet");
const ethers_1 = require("ethers");
const ts_jest_1 = require("@golevelup/ts-jest");
const HederaId_1 = require("../../../../domain/context/shared/HederaId");
const Account_1 = __importDefault(require("../../../../domain/context/account/Account"));
const AccountFixture_1 = require("../../../../../__tests__/fixtures/account/AccountFixture");
const DataFixture_1 = require("../../../../../__tests__/fixtures/shared/DataFixture");
describe("MetamaskService", () => {
    let metamaskService;
    const mockEventService = (0, ts_jest_1.createMock)();
    const mockCommandBus = (0, ts_jest_1.createMock)();
    const mockNetworkService = (0, ts_jest_1.createMock)();
    const mockMirrorNodeAdapter = (0, ts_jest_1.createMock)();
    let mockEthereum;
    const account = new Account_1.default(AccountFixture_1.AccountPropsFixture.create());
    const id = new HederaId_1.HederaId(DataFixture_1.HederaIdPropsFixture.create().value);
    const evmAddress = DataFixture_1.EvmAddressPropsFixture.create().value;
    beforeEach(() => {
        metamaskService = new MetamaskService_1.default(mockEventService, mockCommandBus, mockNetworkService, mockMirrorNodeAdapter);
        mockEthereum = {
            isMetaMask: true,
            isConnected: jest.fn().mockReturnValue(true),
            request: jest.fn(),
            on: jest.fn(),
        };
        global.ethereum = mockEthereum;
        const mockSigner = { getAddress: jest.fn().mockResolvedValue(evmAddress) };
        jest.spyOn(ethers_1.ethers, "BrowserProvider").mockImplementation(() => ({
            getSigner: jest.fn().mockResolvedValue(mockSigner),
        }));
    });
    afterEach(() => {
        jest.clearAllMocks();
    });
    describe("init", () => {
        it("should not call connectMetamask when debug is true", async () => {
            await metamaskService.init(true);
            expect(mockEventService.emit).toHaveBeenCalledWith(WalletEvent_1.WalletEvents.walletInit, expect.any(Object));
        });
    });
    describe("register", () => {
        it("should register account and handler", async () => {
            const mockHandler = {};
            mockMirrorNodeAdapter.getAccountInfo.mockResolvedValue({
                id: id,
                evmAddress: evmAddress,
            });
            const result = await metamaskService.register(mockHandler, account, true);
            expect(result).toEqual({ account: account });
            expect(mockMirrorNodeAdapter.getAccountInfo).toHaveBeenCalledWith({
                value: account.id.value,
            });
        });
    });
    describe("stop", () => {
        it("should emit disconnect events and return true", async () => {
            const result = await metamaskService.stop();
            expect(result).toBe(true);
            expect(mockEventService.emit).toHaveBeenCalledWith(WalletEvent_1.WalletEvents.walletConnectionStatusChanged, {
                status: WalletEvent_1.ConnectionState.Disconnected,
                wallet: Wallet_1.SupportedWallets.METAMASK,
            });
            expect(mockEventService.emit).toHaveBeenCalledWith(WalletEvent_1.WalletEvents.walletDisconnect, {
                wallet: Wallet_1.SupportedWallets.METAMASK,
            });
        });
    });
    describe("setConfig", () => {
        it("should set configuration properties", () => {
            const config = {
                mirrorNodes: { nodes: [] },
                jsonRpcRelays: { nodes: [] },
                factories: { factories: [] },
                resolvers: { resolvers: [] },
            };
            metamaskService.setConfig(config);
            expect(metamaskService["mirrorNodes"]).toBe(config.mirrorNodes);
            expect(metamaskService["jsonRpcRelays"]).toBe(config.jsonRpcRelays);
            expect(metamaskService["factories"]).toBe(config.factories);
            expect(metamaskService["resolvers"]).toBe(config.resolvers);
        });
    });
});
