"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 DataFixture_1 = require("../../../../../../../__tests__/fixtures/shared/DataFixture");
const SecurityRole_1 = require("../../../../../../domain/context/security/SecurityRole");
const Account_1 = __importDefault(require("../../../../../../domain/context/account/Account"));
const BigDecimal_1 = __importDefault(require("../../../../../../domain/context/shared/BigDecimal"));
const EvmAddress_1 = __importDefault(require("../../../../../../domain/context/contract/EvmAddress"));
const MintCommandHandler_1 = require("./MintCommandHandler");
const MintCommand_1 = require("./MintCommand");
const MintCommandError_1 = require("./error/MintCommandError");
const MintFixture_1 = require("../../../../../../../__tests__/fixtures/mint/MintFixture");
const Kyc_1 = require("../../../../../../domain/context/kyc/Kyc");
describe("MintCommandHandler", () => {
    let handler;
    let command;
    const securityServiceMock = (0, ts_jest_1.createMock)();
    const accountServiceMock = (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 account = new Account_1.default({
        id: DataFixture_1.HederaIdPropsFixture.create().value,
        evmAddress: DataFixture_1.EvmAddressPropsFixture.create().value,
    });
    const security = {
        decimals: 6,
    };
    const securityEvmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value);
    const targetEvmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value);
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    beforeEach(() => {
        handler = new MintCommandHandler_1.MintCommandHandler(securityServiceMock, accountServiceMock, transactionServiceMock, validationServiceMock, contractServiceMock);
        command = MintFixture_1.MintCommandFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        it("should successfully mint tokens", async () => {
            const handlerMock = (0, ts_jest_1.createMock)();
            handlerMock.mint.mockResolvedValue({ id: transactionId });
            transactionServiceMock.getHandler.mockReturnValue(handlerMock);
            securityServiceMock.get.mockResolvedValue(security);
            accountServiceMock.getCurrentAccount.mockReturnValue(account);
            contractServiceMock.getContractEvmAddress.mockResolvedValueOnce(securityEvmAddress);
            accountServiceMock.getAccountEvmAddress.mockResolvedValueOnce(targetEvmAddress);
            const result = await handler.execute(command);
            expect(result).toBeInstanceOf(MintCommand_1.MintCommandResponse);
            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(accountServiceMock.getCurrentAccount).toHaveBeenCalled();
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledWith(command.securityId);
            expect(accountServiceMock.getAccountEvmAddress).toHaveBeenCalledWith(command.targetId);
            expect(validationServiceMock.checkDecimals).toHaveBeenCalledWith(security, command.amount);
            expect(validationServiceMock.checkMaxSupply).toHaveBeenCalledWith(command.securityId, amountBd, security);
            expect(validationServiceMock.checkControlList).toHaveBeenCalledWith(command.securityId, command.targetId);
            expect(validationServiceMock.checkKycAddresses).toHaveBeenCalledWith(command.securityId, [command.targetId], Kyc_1.KycStatus.GRANTED);
            expect(validationServiceMock.checkAnyRole).toHaveBeenCalledWith([SecurityRole_1.SecurityRole._ISSUER_ROLE, SecurityRole_1.SecurityRole._AGENT_ROLE], account.id.toString(), command.securityId);
            expect(validationServiceMock.checkMultiPartition).toHaveBeenCalledWith(security);
            expect(validationServiceMock.checkIssuable).toHaveBeenCalledWith(security);
            expect(handlerMock.mint).toHaveBeenCalledWith(securityEvmAddress, targetEvmAddress, amountBd, command.securityId);
        });
        it("should throw MintCommandError on failure", async () => {
            securityServiceMock.get.mockRejectedValue(new Error("fail"));
            await expect(handler.execute(command)).rejects.toThrow(MintCommandError_1.MintCommandError);
        });
    });
});
