"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 QueryBus_1 = require("../../../core/query/QueryBus");
const Account_1 = __importDefault(require("../../../domain/context/account/Account"));
const HederaId_1 = require("../../../domain/context/shared/HederaId");
const GetAccountInfoQuery_1 = require("../../usecase/query/account/info/GetAccountInfoQuery");
const AccountService_1 = __importDefault(require("./AccountService"));
const EvmAddress_1 = __importDefault(require("../../../domain/context/contract/EvmAddress"));
const Constants_1 = require("../../../core/Constants");
const Injectable_1 = __importDefault(require("../../../core/injectable/Injectable"));
const ts_jest_1 = require("@golevelup/ts-jest");
const DataFixture_1 = require("../../../../__tests__/fixtures/shared/DataFixture");
describe("AccountService", () => {
    let service;
    const queryBusMock = (0, ts_jest_1.createMock)();
    const mirrorNodeAdapterMock = (0, ts_jest_1.createMock)();
    const transactionServiceMock = (0, ts_jest_1.createMock)();
    const networkServiceMock = (0, ts_jest_1.createMock)();
    const account = new Account_1.default({
        id: new HederaId_1.HederaId(DataFixture_1.HederaIdPropsFixture.create().value).toString(),
    });
    const evmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value);
    const accountId = account.id.toString();
    beforeEach(() => {
        service = new AccountService_1.default(networkServiceMock, transactionServiceMock, mirrorNodeAdapterMock);
        jest.spyOn(Injectable_1.default, "resolve").mockReturnValue(queryBusMock);
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("getCurrentAccount", () => {
        it("should return the current account from TransactionService", () => {
            transactionServiceMock.getHandler().getAccount.mockReturnValue(account);
            const result = service.getCurrentAccount();
            expect(Injectable_1.default.resolve).toHaveBeenCalledWith(QueryBus_1.QueryBus);
            expect(transactionServiceMock.getHandler).toHaveBeenCalled();
            expect(transactionServiceMock.getHandler().getAccount).toHaveBeenCalled();
            expect(result).toBe(account);
        });
    });
    describe("getAccountInfo", () => {
        it("should return account info for a valid HederaId", async () => {
            queryBusMock.execute.mockResolvedValue({ account: account });
            const result = await service.getAccountInfo(HederaId_1.HederaId.from(accountId));
            expect(Injectable_1.default.resolve).toHaveBeenCalledWith(QueryBus_1.QueryBus);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetAccountInfoQuery_1.GetAccountInfoQuery(HederaId_1.HederaId.from(accountId)));
            expect(result).toBe(account);
        });
        it("should return account info for a valid string ID", async () => {
            queryBusMock.execute.mockResolvedValue({ account: account });
            const result = await service.getAccountInfo(accountId);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetAccountInfoQuery_1.GetAccountInfoQuery(accountId));
            expect(result).toBe(account);
        });
    });
    describe("getAccountEvmAddress", () => {
        it("should return EvmAddress for valid Hedera ID format", async () => {
            jest.spyOn(HederaId_1.HEDERA_FORMAT_ID_REGEX, "test");
            mirrorNodeAdapterMock.accountToEvmAddress.mockResolvedValue(evmAddress);
            const result = await service.getAccountEvmAddress(accountId);
            expect(HederaId_1.HEDERA_FORMAT_ID_REGEX.test).toHaveBeenCalledWith(accountId);
            expect(mirrorNodeAdapterMock.accountToEvmAddress).toHaveBeenCalledWith(accountId);
            expect(result).toBe(evmAddress);
        });
        it("should return EvmAddress for non-Hedera ID format", async () => {
            const accountId = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value).toString();
            const mockEvmAddress = new EvmAddress_1.default(accountId);
            jest.spyOn(HederaId_1.HEDERA_FORMAT_ID_REGEX, "test");
            const result = await service.getAccountEvmAddress(accountId);
            expect(HederaId_1.HEDERA_FORMAT_ID_REGEX.test).toHaveBeenCalledWith(accountId);
            expect(mirrorNodeAdapterMock.accountToEvmAddress).not.toHaveBeenCalled();
            expect(result).toEqual(mockEvmAddress);
        });
    });
    describe("getAccountEvmAddressOrNull", () => {
        it("should return zero address for accountId 0.0.0", async () => {
            const accountId = "0.0.0";
            const zeroAddress = new EvmAddress_1.default(Constants_1.EVM_ZERO_ADDRESS);
            const result = await service.getAccountEvmAddressOrNull(accountId);
            expect(result).toEqual(zeroAddress);
            expect(mirrorNodeAdapterMock.accountToEvmAddress).not.toHaveBeenCalled();
        });
        it("should call getAccountEvmAddress for non-zero accountId", async () => {
            mirrorNodeAdapterMock.accountToEvmAddress.mockResolvedValue(evmAddress);
            jest.spyOn(HederaId_1.HEDERA_FORMAT_ID_REGEX, "test");
            const result = await service.getAccountEvmAddressOrNull(accountId);
            expect(HederaId_1.HEDERA_FORMAT_ID_REGEX.test).toHaveBeenCalledWith(accountId);
            expect(mirrorNodeAdapterMock.accountToEvmAddress).toHaveBeenCalledWith(accountId);
            expect(result).toBe(evmAddress);
        });
    });
});
