// SPDX-License-Identifier: Apache-2.0
import { createMock } from "@golevelup/ts-jest";
import { BurnCommandHandler } from "./BurnCommandHandler";
import { BurnCommandResponse } from "./BurnCommand";
import { BurnCommandFixture } from "../../../../../../../__tests__/fixtures/burn/BurnFixture";
import { EvmAddressPropsFixture, HederaIdPropsFixture, TransactionIdFixture } from "../../../../../../../__tests__/fixtures/shared/DataFixture";
import Account from "../../../../../../domain/context/account/Account";
import { _PARTITION_ID_1 } from "../../../../../../core/Constants";
import BigDecimal from "../../../../../../domain/context/shared/BigDecimal";
import { BurnCommandError } from "./error/BurnCommandError";
import EvmAddress from "../../../../../../domain/context/contract/EvmAddress";
import { SecurityRole } from "../../../../../../domain/context/security/SecurityRole";
describe("BurnCommandHandler", () => {
    let handler;
    let command;
    const transactionServiceMock = createMock();
    const accountServiceMock = createMock();
    const securityServiceMock = createMock();
    const validationServiceMock = createMock();
    const contractServiceMock = createMock();
    const securityEvmAddress = new EvmAddress(EvmAddressPropsFixture.create().value);
    const sourceEvmAddress = new EvmAddress(EvmAddressPropsFixture.create().value);
    const transactionId = TransactionIdFixture.create().id;
    const account = new Account({
        id: HederaIdPropsFixture.create().value,
        evmAddress: EvmAddressPropsFixture.create().value,
    });
    const security = {
        decimals: 6,
    };
    beforeEach(() => {
        handler = new BurnCommandHandler(securityServiceMock, accountServiceMock, transactionServiceMock, validationServiceMock, contractServiceMock);
        command = BurnCommandFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        it("should successfully burn tokens", async () => {
            const handlerMock = createMock();
            handlerMock.burn.mockResolvedValue({ id: transactionId });
            transactionServiceMock.getHandler.mockReturnValue(handlerMock);
            accountServiceMock.getCurrentAccount.mockReturnValue(account);
            contractServiceMock.getContractEvmAddress.mockResolvedValueOnce(securityEvmAddress);
            validationServiceMock.checkClearingDeactivated.mockResolvedValue(undefined);
            validationServiceMock.checkCanRedeem.mockResolvedValue(undefined);
            accountServiceMock.getAccountEvmAddress.mockResolvedValueOnce(sourceEvmAddress);
            securityServiceMock.get.mockResolvedValueOnce(security);
            validationServiceMock.checkDecimals.mockResolvedValue(undefined);
            const result = await handler.execute(command);
            expect(result).toBeInstanceOf(BurnCommandResponse);
            expect(result.payload).toBe(true);
            expect(result.transactionId).toBe(transactionId);
            expect(transactionServiceMock.getHandler).toHaveBeenCalledTimes(1);
            expect(accountServiceMock.getCurrentAccount).toHaveBeenCalledTimes(1);
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledWith(command.securityId);
            expect(validationServiceMock.checkClearingDeactivated).toHaveBeenCalledWith(command.securityId);
            expect(validationServiceMock.checkCanRedeem).toHaveBeenCalledWith(command.securityId, command.sourceId, command.amount, _PARTITION_ID_1);
            expect(accountServiceMock.getAccountEvmAddress).toHaveBeenCalledWith(command.sourceId);
            expect(securityServiceMock.get).toHaveBeenCalledWith(command.securityId);
            expect(validationServiceMock.checkDecimals).toHaveBeenCalledWith(security, command.amount);
            expect(validationServiceMock.checkAnyRole).toHaveBeenCalledWith([SecurityRole._CONTROLLER_ROLE, SecurityRole._AGENT_ROLE], account.id.toString(), command.securityId);
            const expectedAmountBd = BigDecimal.fromString(command.amount, security.decimals);
            expect(handlerMock.burn).toHaveBeenCalledWith(securityEvmAddress, sourceEvmAddress, expectedAmountBd, command.securityId);
        });
        it("should throw BurnCommandError if any error occurs", async () => {
            const error = new Error("Something went wrong");
            validationServiceMock.checkClearingDeactivated.mockRejectedValueOnce(error);
            await expect(handler.execute(command)).rejects.toThrow(BurnCommandError);
        });
    });
});
