"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 CreateTrexSuiteBondCommandHandler_1 = require("./CreateTrexSuiteBondCommandHandler");
const ts_jest_1 = require("@golevelup/ts-jest");
const BondFixture_1 = require("../../../../../../__tests__/fixtures/bond/BondFixture");
const CreateTrexSuiteBondCommand_1 = require("./CreateTrexSuiteBondCommand");
const BigDecimal_1 = __importDefault(require("../../../../../domain/context/shared/BigDecimal"));
const DataFixture_1 = require("../../../../../../__tests__/fixtures/shared/DataFixture");
const EvmAddress_1 = __importDefault(require("../../../../../domain/context/contract/EvmAddress"));
const BaseError_1 = require("../../../../../core/error/BaseError");
const CreateTrexSuiteBondError_1 = require("./error/CreateTrexSuiteBondError");
describe("CreateTrexSuiteBondCommandHandler", () => {
    let handler;
    let command;
    const transactionServiceMock = (0, ts_jest_1.createMock)();
    const mirrorNodeAdapterMock = (0, ts_jest_1.createMock)();
    const accountServiceMock = (0, ts_jest_1.createMock)();
    const contractServiceMock = (0, ts_jest_1.createMock)();
    const validationServiceMock = (0, ts_jest_1.createMock)();
    const evmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value);
    const externalPauseEvmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value);
    const externalControlEvmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value);
    const externalKycEvmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value);
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    const hederaId = DataFixture_1.HederaIdPropsFixture.create();
    const hederaIdZeroAddress = DataFixture_1.HederaIdZeroAddressFixture.create().address;
    const errorMsg = DataFixture_1.ErrorMsgFixture.create().msg;
    beforeEach(() => {
        handler = new CreateTrexSuiteBondCommandHandler_1.CreateTrexSuiteBondCommandHandler(accountServiceMock, transactionServiceMock, mirrorNodeAdapterMock, contractServiceMock, validationServiceMock);
        command = BondFixture_1.CreateTrexSuiteBondCommandFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        describe("error cases", () => {
            it("throws CreateTrexSuiteBondCommandError when command fails with uncaught error", async () => {
                const fakeError = new Error(errorMsg);
                accountServiceMock.getAccountEvmAddress.mockRejectedValue(fakeError);
                const resultPromise = handler.execute(command);
                await expect(resultPromise).rejects.toBeInstanceOf(CreateTrexSuiteBondError_1.CreateTrexSuiteBondCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`An error occurred while creating the trex suite bond: ${errorMsg}`),
                    errorCode: BaseError_1.ErrorCode.UncaughtCommandError,
                });
            });
        });
        describe("success cases", () => {
            it("should successfully create a bond with bondAddress in response", async () => {
                contractServiceMock.getContractEvmAddress
                    .mockResolvedValueOnce(evmAddress)
                    .mockResolvedValueOnce(evmAddress)
                    .mockResolvedValueOnce(evmAddress)
                    .mockResolvedValueOnce(evmAddress);
                contractServiceMock.getEvmAddressesFromHederaIds
                    .mockResolvedValueOnce([externalPauseEvmAddress])
                    .mockResolvedValueOnce([externalControlEvmAddress])
                    .mockResolvedValueOnce([externalKycEvmAddress])
                    .mockResolvedValueOnce([evmAddress]); // proceedRecipients
                accountServiceMock.getAccountEvmAddress.mockResolvedValue(evmAddress);
                transactionServiceMock.getHandler().createTrexSuiteBond.mockResolvedValue({
                    id: transactionId,
                    response: { _token: evmAddress.value },
                });
                validationServiceMock.checkTrexTokenSaltExists.mockResolvedValue();
                mirrorNodeAdapterMock.getHederaIdfromContractAddress.mockResolvedValue(transactionId);
                transactionServiceMock.getTransactionResult.mockResolvedValue(evmAddress.value);
                const result = await handler.execute(command);
                expect(result).toBeInstanceOf(CreateTrexSuiteBondCommand_1.CreateTrexSuiteBondCommandResponse);
                expect(result.securityId.value).toBe(transactionId);
                expect(result.transactionId).toBe(transactionId);
                expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledTimes(4);
                expect(contractServiceMock.getEvmAddressesFromHederaIds).toHaveBeenCalledTimes(3);
                expect(accountServiceMock.getAccountEvmAddress).toHaveBeenCalledTimes(2);
                expect(transactionServiceMock.getHandler().createTrexSuiteBond).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getTransactionResult).toHaveBeenCalledTimes(1);
                expect(mirrorNodeAdapterMock.getHederaIdfromContractAddress).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getHandler().createTrexSuiteBond).toHaveBeenCalledWith(command.salt, command.owner, command.irs, command.onchainId, command.irAgents, command.tokenAgents, command.compliancesModules, command.complianceSettings, command.claimTopics, command.issuers, command.issuerClaims, expect.objectContaining(command.security), expect.objectContaining({
                    currency: command.currency,
                    nominalValue: BigDecimal_1.default.fromString(command.nominalValue),
                    startingDate: parseInt(command.startingDate),
                    maturityDate: parseInt(command.maturityDate),
                }), evmAddress, evmAddress, command.configId, command.configVersion, evmAddress, evmAddress, evmAddress, [evmAddress], command.proceedRecipientsData, [externalPauseEvmAddress], [externalControlEvmAddress], [externalKycEvmAddress], command.factory?.toString());
                expect(transactionServiceMock.getTransactionResult).toHaveBeenCalledWith(expect.objectContaining({
                    res: {
                        id: transactionId,
                        response: { _token: evmAddress.value },
                    },
                    result: evmAddress.value,
                    className: CreateTrexSuiteBondCommandHandler_1.CreateTrexSuiteBondCommandHandler.name,
                    position: 0,
                    numberOfResultsItems: 1,
                }));
            });
            it("should handle error and return fallback response if response code is 1", async () => {
                contractServiceMock.getContractEvmAddress.mockResolvedValue(evmAddress);
                mirrorNodeAdapterMock.getContractInfo.mockResolvedValue({
                    id: hederaId.value,
                    evmAddress: evmAddress.value,
                });
                transactionServiceMock.getHandler().createTrexSuiteBond.mockResolvedValue({
                    id: transactionId,
                    response: 1,
                });
                const result = await handler.execute(command);
                expect(result).toBeInstanceOf(CreateTrexSuiteBondCommand_1.CreateTrexSuiteBondCommandResponse);
                expect(result.securityId.toString()).toBe(hederaIdZeroAddress);
                expect(result.transactionId.toString()).toBe(transactionId);
            });
        });
    });
});
