"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 ts_jest_1 = require("@golevelup/ts-jest");
const ForcedTransferCommandHandler_1 = require("./ForcedTransferCommandHandler");
const ForcedTransferCommand_1 = require("./ForcedTransferCommand");
const DataFixture_1 = require("../../../../../../../__tests__/fixtures/shared/DataFixture");
const Account_1 = __importDefault(require("../../../../../../domain/context/account/Account"));
const EvmAddress_1 = __importDefault(require("../../../../../../domain/context/contract/EvmAddress"));
const BigDecimal_1 = __importDefault(require("../../../../../../domain/context/shared/BigDecimal"));
const TransferFixture_1 = require("../../../../../../../__tests__/fixtures/transfer/TransferFixture");
const ForcedTransferCommandError_1 = require("./error/ForcedTransferCommandError");
describe("ForcedTransferCommandHandler", () => {
    let handler;
    let command;
    const accountServiceMock = (0, ts_jest_1.createMock)();
    const securityServiceMock = (0, ts_jest_1.createMock)();
    const transactionServiceMock = (0, ts_jest_1.createMock)();
    const validationServiceMock = (0, ts_jest_1.createMock)();
    const contractServiceMock = (0, ts_jest_1.createMock)();
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    const security = { decimals: 6 };
    const account = new Account_1.default({
        id: DataFixture_1.HederaIdPropsFixture.create().value,
        evmAddress: DataFixture_1.EvmAddressPropsFixture.create().value,
    });
    const securityEvmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value);
    const sourceEvmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value);
    const targetEvmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value);
    beforeEach(() => {
        handler = new ForcedTransferCommandHandler_1.ForcedTransferCommandHandler(securityServiceMock, accountServiceMock, transactionServiceMock, validationServiceMock, contractServiceMock);
        command = TransferFixture_1.FocedTransferCommandFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        it("should successfully execute forced transfer", async () => {
            const handlerMock = (0, ts_jest_1.createMock)();
            handlerMock.forcedTransfer.mockResolvedValue({ id: transactionId });
            transactionServiceMock.getHandler.mockReturnValue(handlerMock);
            accountServiceMock.getCurrentAccount.mockReturnValue(account);
            securityServiceMock.get.mockResolvedValue(security);
            contractServiceMock.getContractEvmAddress.mockResolvedValueOnce(securityEvmAddress);
            accountServiceMock.getAccountEvmAddress.mockResolvedValueOnce(sourceEvmAddress);
            accountServiceMock.getAccountEvmAddress.mockResolvedValueOnce(targetEvmAddress);
            const result = await handler.execute(command);
            expect(result).toBeInstanceOf(ForcedTransferCommand_1.ForcedTransferCommandResponse);
            expect(result.payload).toBe(true);
            expect(result.transactionId).toBe(transactionId);
            const amountBd = BigDecimal_1.default.fromString(command.amount, security.decimals);
            expect(securityServiceMock.get).toHaveBeenCalledWith(command.securityId);
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledWith(command.securityId);
            expect(accountServiceMock.getAccountEvmAddress).toHaveBeenNthCalledWith(1, command.sourceId);
            expect(accountServiceMock.getAccountEvmAddress).toHaveBeenNthCalledWith(2, command.targetId);
            expect(validationServiceMock.checkCanTransfer).toHaveBeenCalledWith(command.securityId, command.targetId, command.amount, account.id.toString(), command.sourceId);
            expect(validationServiceMock.checkDecimals).toHaveBeenCalledWith(security, command.amount);
            expect(handlerMock.forcedTransfer).toHaveBeenCalledWith(securityEvmAddress, sourceEvmAddress, targetEvmAddress, amountBd, command.securityId);
        });
        it("should throw ForcedTransferCommandError on failure", async () => {
            securityServiceMock.get.mockRejectedValue(new Error("Failed"));
            await expect(handler.execute(command)).rejects.toThrow(ForcedTransferCommandError_1.ForcedTransferCommandError);
        });
    });
});
