"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 CreateTrexSuiteEquityCommandHandler_1 = require("./CreateTrexSuiteEquityCommandHandler");
const CreateTrexSuiteEquityCommand_1 = require("./CreateTrexSuiteEquityCommand");
const EvmAddress_1 = __importDefault(require("../../../../../domain/context/contract/EvmAddress"));
const DataFixture_1 = require("../../../../../../__tests__/fixtures/shared/DataFixture");
const BigDecimal_1 = __importDefault(require("../../../../../domain/context/shared/BigDecimal"));
const EquityFixture_1 = require("../../../../../../__tests__/fixtures/equity/EquityFixture");
const BaseError_1 = require("../../../../../core/error/BaseError");
const CreateTrexSuiteEquityError_1 = require("./error/CreateTrexSuiteEquityError");
describe("CreateTrexSuiteEquityCommandHandler", () => {
    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().value;
    const hederaIdZeroAddress = DataFixture_1.HederaIdZeroAddressFixture.create().address;
    const errorMsg = DataFixture_1.ErrorMsgFixture.create().msg;
    beforeEach(() => {
        handler = new CreateTrexSuiteEquityCommandHandler_1.CreateTrexSuiteEquityCommandHandler(accountServiceMock, transactionServiceMock, mirrorNodeAdapterMock, contractServiceMock, validationServiceMock);
        command = EquityFixture_1.CreateTrexSuiteEquityCommandFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        describe("error cases", () => {
            it("throws CreateTrexSuiteEquityCommandError 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(CreateTrexSuiteEquityError_1.CreateTrexSuiteEquityCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`An error occurred while creating the trex suite equity: ${errorMsg}`),
                    errorCode: BaseError_1.ErrorCode.UncaughtCommandError,
                });
            });
        });
        describe("success cases", () => {
            it("should successfully create an equity with equityAddress in response", async () => {
                contractServiceMock.getContractEvmAddress
                    .mockResolvedValueOnce(evmAddress)
                    .mockResolvedValueOnce(evmAddress)
                    .mockResolvedValueOnce(evmAddress)
                    .mockResolvedValueOnce(evmAddress);
                contractServiceMock.getEvmAddressesFromHederaIds
                    .mockResolvedValueOnce([externalPauseEvmAddress])
                    .mockResolvedValueOnce([externalControlEvmAddress])
                    .mockResolvedValueOnce([externalKycEvmAddress]);
                accountServiceMock.getAccountEvmAddress.mockResolvedValue(evmAddress);
                transactionServiceMock.getHandler().createTrexSuiteEquity.mockResolvedValue({
                    id: transactionId,
                    response: { _token: evmAddress.value },
                });
                validationServiceMock.checkTrexTokenSaltExists.mockResolvedValue();
                mirrorNodeAdapterMock.getHederaIdfromContractAddress.mockResolvedValue(hederaId);
                transactionServiceMock.getTransactionResult.mockResolvedValue(evmAddress.value);
                const result = await handler.execute(command);
                expect(result).toBeInstanceOf(CreateTrexSuiteEquityCommand_1.CreateTrexSuiteEquityCommandResponse);
                expect(result.securityId.value).toBe(hederaId);
                expect(result.transactionId).toBe(transactionId);
                expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledTimes(4);
                expect(contractServiceMock.getEvmAddressesFromHederaIds).toHaveBeenCalledTimes(3);
                expect(accountServiceMock.getAccountEvmAddress).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getHandler().createTrexSuiteEquity).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getTransactionResult).toHaveBeenCalledTimes(1);
                expect(mirrorNodeAdapterMock.getHederaIdfromContractAddress).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getHandler().createTrexSuiteEquity).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({
                    votingRight: command.votingRight,
                    informationRight: command.informationRight,
                    liquidationRight: command.liquidationRight,
                    subscriptionRight: command.subscriptionRight,
                    conversionRight: command.conversionRight,
                    redemptionRight: command.redemptionRight,
                    putRight: command.putRight,
                    dividendRight: command.dividendRight,
                    currency: command.currency,
                    nominalValue: BigDecimal_1.default.fromString(command.nominalValue),
                }), evmAddress, evmAddress, command.configId, command.configVersion, evmAddress, evmAddress, evmAddress, [externalPauseEvmAddress], [externalControlEvmAddress], [externalKycEvmAddress], command.factory?.toString());
                expect(transactionServiceMock.getTransactionResult).toHaveBeenCalledWith(expect.objectContaining({
                    res: {
                        id: transactionId,
                        response: { _token: evmAddress.value },
                    },
                    result: evmAddress.value,
                    className: CreateTrexSuiteEquityCommandHandler_1.CreateTrexSuiteEquityCommandHandler.name,
                    position: 0,
                    numberOfResultsItems: 1,
                }));
            });
            it("should handle error and return fallback response if response code is 1", async () => {
                mirrorNodeAdapterMock.getContractInfo.mockResolvedValue({
                    id: hederaId,
                    evmAddress: evmAddress.value,
                });
                transactionServiceMock.getHandler().createTrexSuiteEquity.mockResolvedValue({
                    id: transactionId,
                    response: 1,
                });
                const result = await handler.execute(command);
                expect(result).toBeInstanceOf(CreateTrexSuiteEquityCommand_1.CreateTrexSuiteEquityCommandResponse);
                expect(result.securityId.toString()).toBe(hederaIdZeroAddress);
                expect(result.transactionId.toString()).toBe(transactionId);
            });
        });
    });
});
