// SPDX-License-Identifier: Apache-2.0
import { createMock } from "@golevelup/ts-jest";
import { EvmAddressPropsFixture, HederaIdPropsFixture, TransactionIdFixture } from "../../../../../../../__tests__/fixtures/shared/DataFixture";
import EvmAddress from "../../../../../../domain/context/contract/EvmAddress";
import Account from "../../../../../../domain/context/account/Account";
import { SecurityRole } from "../../../../../../domain/context/security/SecurityRole";
import { UpdateExternalPausesCommandHandler } from "./UpdateExternalPausesCommandHandler.js";
import { UpdateExternalPausesCommandResponse } from "./UpdateExternalPausesCommand.js";
import { UpdateExternalPausesCommandFixture } from "../../../../../../../__tests__/fixtures/externalPauses/ExternalPausesFixture";
describe("UpdateExternalPausesCommandHandler", () => {
    let handler;
    let command;
    const transactionServiceMock = createMock();
    const validationServiceMock = createMock();
    const accountServiceMock = createMock();
    const contractServiceMock = createMock();
    const evmAddress = new EvmAddress(EvmAddressPropsFixture.create().value);
    const externalEvmAddress = new EvmAddress(EvmAddressPropsFixture.create().value);
    const account = new Account({
        id: HederaIdPropsFixture.create().value,
        evmAddress: EvmAddressPropsFixture.create().value,
    });
    const transactionId = TransactionIdFixture.create().id;
    beforeEach(() => {
        handler = new UpdateExternalPausesCommandHandler(accountServiceMock, contractServiceMock, transactionServiceMock, validationServiceMock);
        command = UpdateExternalPausesCommandFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        it("should successfully update external pauses", async () => {
            contractServiceMock.getContractEvmAddress
                .mockResolvedValueOnce(evmAddress)
                .mockResolvedValueOnce(externalEvmAddress);
            accountServiceMock.getCurrentAccount.mockReturnValue(account);
            validationServiceMock.checkPause.mockResolvedValue(undefined);
            validationServiceMock.checkRole.mockResolvedValue(undefined);
            transactionServiceMock.getHandler().updateExternalPauses.mockResolvedValue({
                id: transactionId,
            });
            const result = await handler.execute(command);
            expect(result).toBeInstanceOf(UpdateExternalPausesCommandResponse);
            expect(result.payload).toBe(true);
            expect(result.transactionId).toBe(transactionId);
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledTimes(2);
            expect(accountServiceMock.getCurrentAccount).toHaveBeenCalledTimes(1);
            expect(transactionServiceMock.getHandler().updateExternalPauses).toHaveBeenCalledTimes(1);
            expect(validationServiceMock.checkPause).toHaveBeenCalledWith(command.securityId);
            expect(validationServiceMock.checkRole).toHaveBeenCalledWith(SecurityRole._PAUSE_MANAGER_ROLE, account.id.toString(), command.securityId);
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenNthCalledWith(1, command.securityId);
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenNthCalledWith(2, command.externalPausesAddresses[0]);
            expect(transactionServiceMock.getHandler().updateExternalPauses).toHaveBeenCalledWith(evmAddress, [externalEvmAddress], command.actives, command.securityId);
        });
    });
});
