"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 ContractService_1 = __importDefault(require("./ContractService")); // Adjust path as needed
const EvmAddress_1 = __importDefault(require("../../../domain/context/contract/EvmAddress"));
const ts_jest_1 = require("@golevelup/ts-jest");
const Injectable_1 = __importDefault(require("../../../core/injectable/Injectable"));
const DataFixture_1 = require("../../../../__tests__/fixtures/shared/DataFixture");
const InvalidEvmAddress_1 = require("../../../domain/context/contract/error/InvalidEvmAddress");
describe("ContractService", () => {
    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 evmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value).toString();
    const evmAddress2 = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value).toString();
    const id = DataFixture_1.HederaIdPropsFixture.create().value;
    const id2 = DataFixture_1.HederaIdPropsFixture.create().value;
    beforeEach(() => {
        service = new ContractService_1.default(networkServiceMock, transactionServiceMock, mirrorNodeAdapterMock);
        jest.spyOn(Injectable_1.default, "resolve").mockReturnValue(queryBusMock);
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("getContractEvmAddress", () => {
        it("should return EvmAddress for a valid Hedera contract ID", async () => {
            mirrorNodeAdapterMock.getContractInfo.mockResolvedValue({
                id,
                evmAddress,
            });
            const result = await service.getContractEvmAddress(id);
            expect(mirrorNodeAdapterMock.getContractInfo).toHaveBeenCalledWith(id);
            expect(result).toBeInstanceOf(EvmAddress_1.default);
            expect(result.value).toBe(evmAddress);
        });
        it("should return EvmAddress for a valid EVM address", async () => {
            const result = await service.getContractEvmAddress(evmAddress);
            expect(mirrorNodeAdapterMock.getContractInfo).not.toHaveBeenCalled();
            expect(result).toBeInstanceOf(EvmAddress_1.default);
            expect(result.value).toBe(evmAddress);
        });
        it("should throw an error for an invalid contract ID format", async () => {
            const invalidContractId = "invalid-id";
            await expect(service.getContractEvmAddress(invalidContractId)).rejects.toBeInstanceOf(InvalidEvmAddress_1.InvalidEvmAddress);
            expect(mirrorNodeAdapterMock.getContractInfo).not.toHaveBeenCalled();
        });
    });
    describe("getEvmAddressesFromHederaIds", () => {
        it("should return an empty array when addresses is undefined", async () => {
            const result = await service.getEvmAddressesFromHederaIds(undefined);
            expect(result).toEqual([]);
            expect(mirrorNodeAdapterMock.getContractInfo).not.toHaveBeenCalled();
        });
        it("should return EvmAddress array for a list of valid contract IDs", async () => {
            const contractIds = [id, id2];
            const evmAddresses = [evmAddress, evmAddress2];
            mirrorNodeAdapterMock.getContractInfo
                .mockResolvedValueOnce({
                id: contractIds[0],
                evmAddress: evmAddresses[0],
            })
                .mockResolvedValueOnce({
                id: contractIds[1],
                evmAddress: evmAddresses[1],
            });
            const result = await service.getEvmAddressesFromHederaIds(contractIds);
            expect(mirrorNodeAdapterMock.getContractInfo).toHaveBeenCalledTimes(2);
            expect(mirrorNodeAdapterMock.getContractInfo).toHaveBeenCalledWith(contractIds[0]);
            expect(mirrorNodeAdapterMock.getContractInfo).toHaveBeenCalledWith(contractIds[1]);
            expect(result).toHaveLength(2);
            expect(result[0]).toBeInstanceOf(EvmAddress_1.default);
            expect(result[0].value).toBe(evmAddresses[0]);
            expect(result[1]).toBeInstanceOf(EvmAddress_1.default);
            expect(result[1].value).toBe(evmAddresses[1]);
        });
        it("should handle mixed input of Hedera IDs and EVM addresses", async () => {
            const addresses = [id, evmAddress];
            mirrorNodeAdapterMock.getContractInfo.mockResolvedValueOnce({
                id,
                evmAddress,
            });
            const result = await service.getEvmAddressesFromHederaIds(addresses);
            expect(mirrorNodeAdapterMock.getContractInfo).toHaveBeenCalledTimes(1);
            expect(mirrorNodeAdapterMock.getContractInfo).toHaveBeenCalledWith(addresses[0]);
            expect(result).toHaveLength(2);
            expect(result[0]).toBeInstanceOf(EvmAddress_1.default);
            expect(result[0].value).toBe(evmAddress);
            expect(result[1]).toBeInstanceOf(EvmAddress_1.default);
            expect(result[1].value).toBe(addresses[1]);
        });
        it("should handle empty address array", async () => {
            const result = await service.getEvmAddressesFromHederaIds([]);
            expect(result).toEqual([]);
            expect(mirrorNodeAdapterMock.getContractInfo).not.toHaveBeenCalled();
        });
    });
});
