// SPDX-License-Identifier: Apache-2.0
import { createMock } from "@golevelup/ts-jest";
import { ErrorMsgFixture, EvmAddressPropsFixture, HederaIdPropsFixture, TransactionIdFixture, } from "../../../../../../../../__tests__/fixtures/shared/DataFixture";
import EvmAddress from "../../../../../../../domain/context/contract/EvmAddress";
import Account from "../../../../../../../domain/context/account/Account";
import { ErrorCode } from "../../../../../../../core/error/BaseError";
import { BatchUnfreezePartialTokensCommandError } from "./error/BatchUnfreezePartialTokensCommandError";
import { BatchUnfreezePartialTokensResponse, } from "./BatchUnfreezePartialTokensCommand";
import { BatchUnfreezePartialTokensCommandHandler } from "./BatchUnfreezePartialTokensCommandHandler";
import BigDecimal from "../../../../../../../domain/context/shared/BigDecimal";
import { SecurityPropsFixture } from "../../../../../../../../__tests__/fixtures/shared/SecurityFixture";
import { Security } from "../../../../../../../domain/context/security/Security";
import { BatchUnfreezePartialTokensCommandFixture } from "../../../../../../../../__tests__/fixtures/batch/BatchFixture";
describe("BatchUnfreezePartialTokensCommandHandler", () => {
    let handler;
    let command;
    const transactionServiceMock = createMock();
    const validationServiceMock = createMock();
    const accountServiceMock = createMock();
    const contractServiceMock = createMock();
    const securityServiceMock = createMock();
    const evmAddress = new EvmAddress(EvmAddressPropsFixture.create().value);
    const account = new Account({
        id: HederaIdPropsFixture.create().value,
        evmAddress: EvmAddressPropsFixture.create().value,
    });
    const transactionId = TransactionIdFixture.create().id;
    const errorMsg = ErrorMsgFixture.create().msg;
    const security = new Security(SecurityPropsFixture.create());
    beforeEach(() => {
        handler = new BatchUnfreezePartialTokensCommandHandler(securityServiceMock, accountServiceMock, transactionServiceMock, validationServiceMock, contractServiceMock);
        command = BatchUnfreezePartialTokensCommandFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        it("throws BatchUnfreezePartialTokensCommandError when command fails with uncaught error", async () => {
            const fakeError = new Error(errorMsg);
            contractServiceMock.getContractEvmAddress.mockRejectedValue(fakeError);
            const resultPromise = handler.execute(command);
            await expect(resultPromise).rejects.toBeInstanceOf(BatchUnfreezePartialTokensCommandError);
            await expect(resultPromise).rejects.toMatchObject({
                message: expect.stringContaining(`An error occurred while batch unfreeze partial tokens: ${errorMsg}`),
                errorCode: ErrorCode.UncaughtCommandError,
            });
        });
        it("should successfully batch freeze partial tokens", async () => {
            contractServiceMock.getContractEvmAddress.mockResolvedValueOnce(evmAddress);
            accountServiceMock.getAccountEvmAddress.mockResolvedValue(evmAddress);
            accountServiceMock.getCurrentAccount.mockReturnValue(account);
            validationServiceMock.checkPause.mockResolvedValue(undefined);
            validationServiceMock.checkClearingDeactivated.mockResolvedValue(undefined);
            validationServiceMock.checkDecimals.mockResolvedValue(undefined);
            securityServiceMock.get.mockResolvedValue(security);
            transactionServiceMock.getHandler().batchUnfreezePartialTokens.mockResolvedValue({
                id: transactionId,
            });
            const result = await handler.execute(command);
            expect(result).toBeInstanceOf(BatchUnfreezePartialTokensResponse);
            expect(result.payload).toBe(true);
            expect(result.transactionId).toBe(transactionId);
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledTimes(1);
            expect(validationServiceMock.checkPause).toHaveBeenCalledTimes(1);
            expect(validationServiceMock.checkClearingDeactivated).toHaveBeenCalledTimes(1);
            expect(accountServiceMock.getCurrentAccount).toHaveBeenCalledTimes(1);
            expect(transactionServiceMock.getHandler().batchUnfreezePartialTokens).toHaveBeenCalledTimes(1);
            expect(validationServiceMock.checkPause).toHaveBeenCalledWith(command.securityId);
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledWith(command.securityId);
            expect(transactionServiceMock.getHandler().batchUnfreezePartialTokens).toHaveBeenCalledWith(evmAddress, [BigDecimal.fromString(command.amountList[0], security.decimals)], [evmAddress], command.securityId);
        });
    });
});
