// SPDX-License-Identifier: Apache-2.0
import { createMock } from "@golevelup/ts-jest";
import { GetRegulationDetailsRequest } from "../request";
import { HederaIdPropsFixture } from "../../../../__tests__/fixtures/shared/DataFixture";
import LogService from "../../../app/service/log/LogService";
import ValidatedRequest from "../../../core/validation/ValidatedArgs";
import { ValidationError } from "../../../core/validation/ValidationError";
import ContractId from "../../../domain/context/contract/ContractId";
import Factory from "./Factory";
import { GetRegulationDetailsRequestFixture } from "../../../../__tests__/fixtures/factory/FactoryFixture";
import { RegulationFixture } from "../../../../__tests__/fixtures/shared/RegulationFixture";
import { GetRegulationDetailsQuery } from "../../../app/usecase/query/factory/get/GetRegulationDetailsQuery";
describe("Factory", () => {
    let queryBusMock;
    let networkServiceMock;
    let getRegulationDetailsRequest;
    let handleValidationSpy;
    const factoryAddress = HederaIdPropsFixture.create().value;
    beforeEach(() => {
        queryBusMock = createMock();
        handleValidationSpy = jest.spyOn(ValidatedRequest, "handleValidation");
        networkServiceMock = createMock({
            configuration: {
                factoryAddress: factoryAddress,
            },
        });
        jest.spyOn(LogService, "logError").mockImplementation(() => { });
        Factory.queryBus = queryBusMock;
        Factory.networkService = networkServiceMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("getRegulationDetails", () => {
        getRegulationDetailsRequest = new GetRegulationDetailsRequest(GetRegulationDetailsRequestFixture.create());
        it("should get regulation details successfully", async () => {
            const expectedResponse = {
                regulation: RegulationFixture.create(),
            };
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Factory.getRegulationDetails(getRegulationDetailsRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetRegulationDetailsRequest", getRegulationDetailsRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetRegulationDetailsQuery(getRegulationDetailsRequest.regulationType, getRegulationDetailsRequest.regulationSubType, new ContractId(factoryAddress)));
            expect(result).toEqual(expectedResponse.regulation);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Factory.getRegulationDetails(getRegulationDetailsRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetRegulationDetailsRequest", getRegulationDetailsRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetRegulationDetailsQuery(getRegulationDetailsRequest.regulationType, getRegulationDetailsRequest.regulationSubType, new ContractId(factoryAddress)));
        });
        it("should throw error if regulationType is invalid", async () => {
            getRegulationDetailsRequest = new GetRegulationDetailsRequest({
                ...GetRegulationDetailsRequestFixture.create(),
                regulationType: 100,
            });
            await expect(Factory.getRegulationDetails(getRegulationDetailsRequest)).rejects.toThrow(ValidationError);
        });
        it("should throw error if regulationSubType is invalid", async () => {
            getRegulationDetailsRequest = new GetRegulationDetailsRequest({
                ...GetRegulationDetailsRequestFixture.create(),
                regulationSubType: 100,
            });
            await expect(Factory.getRegulationDetails(getRegulationDetailsRequest)).rejects.toThrow(ValidationError);
        });
    });
});
