import { makeAppObjectRepo } from "@vived/core";
import { ParametricLine, Vector2, Vector3 } from "@vived/core";
import { makeBaseAppState } from "../../BaseApp/Entity/BaseAppState";
import { makeMockPixelToSceneRayUC, makeMockSceneRayPickUC, } from "../../Babylon";
import { makeChallengeEntityMock } from "../../Challenge";
import { makeMockSubmitResult } from "../../Dispatcher";
import { makeObjectPickEntity } from "../Entities/ObjectPickEntity";
import { makeTrySubmitObjectPickUC, TrySubmitObjectPickUC, } from "./TrySubmitObjectPickUC";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const baseApp = makeBaseAppState(appObjects.getOrCreate("App"));
    baseApp.isAuthoring = false;
    const challenge = makeChallengeEntityMock(appObjects);
    const mockSubmitResult = makeMockSubmitResult(appObjects);
    const mockPickResult = {
        hit: true,
    };
    const mockRayPick = makeMockSceneRayPickUC(appObjects);
    const mockPick = jest.fn().mockReturnValue(mockPickResult);
    mockRayPick.sceneRayPick = mockPick;
    const mockPixelToRay = makeMockPixelToSceneRayUC(appObjects);
    const objectPick = makeObjectPickEntity(appObjects.getOrCreate("ObjectPick"));
    const sceneObject = appObjects.getOrCreate("parent");
    const uc = makeTrySubmitObjectPickUC(appObjects.getOrCreate("ObjectPick"));
    const trySubmitObjectPick = uc.tryWithRay;
    return {
        trySubmitObjectPick,
        objectPick,
        mockSubmitResult,
        sceneObject,
        mockPickResult,
        baseApp,
        challenge,
        mockPixelToRay,
        appObjects,
        uc,
    };
}
describe("Submit Object Pick UC", () => {
    it("Gets the singleton", () => {
        const { appObjects, uc } = makeTestRig();
        expect(TrySubmitObjectPickUC.get(appObjects)).toEqual(uc);
    });
    it("Forms the pixel into a ray and passed it to the try with ray", () => {
        const { mockPixelToRay, uc } = makeTestRig();
        const mockRay = ParametricLine.FromTwoPoint(new Vector3(1, 2, 3), new Vector3(4, 5, 6));
        mockPixelToRay.pixelToSceneRay.mockReturnValue(mockRay);
        uc.tryWithRay = jest.fn();
        uc.tryWithPixel(new Vector2(7, 8));
        expect(mockPixelToRay.pixelToSceneRay).toHaveBeenCalledWith(new Vector2(7, 8));
        expect(uc.tryWithRay).toHaveBeenCalledWith(mockRay);
    });
    it("Doesn't submit object pick when authoring", () => {
        const { baseApp, objectPick, trySubmitObjectPick } = makeTestRig();
        const mockSubmit = jest.fn();
        baseApp.isAuthoring = true;
        objectPick.submit = mockSubmit;
        const pickRay = new ParametricLine({
            x0: 1,
            y0: 1,
            z0: 1,
            a: 1,
            b: 1,
            c: 1,
        });
        trySubmitObjectPick(pickRay);
        expect(mockSubmit).not.toHaveBeenCalled();
    });
    it("Doesn't submit object pick when challenge is not OBJECT_PICK", () => {
        const { challenge, objectPick, trySubmitObjectPick } = makeTestRig();
        const mockSubmit = jest.fn();
        challenge.selectedChallenge = "QUESTION";
        objectPick.submit = mockSubmit;
        const pickRay = new ParametricLine({
            x0: 1,
            y0: 1,
            z0: 1,
            a: 1,
            b: 1,
            c: 1,
        });
        trySubmitObjectPick(pickRay);
        expect(mockSubmit).not.toHaveBeenCalled();
    });
    it("Doesn't submit object pick when app mode is review", () => {
        const { baseApp, objectPick, trySubmitObjectPick } = makeTestRig();
        const mockSubmit = jest.fn();
        baseApp.isReviewing = true;
        objectPick.submit = mockSubmit;
        const pickRay = new ParametricLine({
            x0: 1,
            y0: 1,
            z0: 1,
            a: 1,
            b: 1,
            c: 1,
        });
        trySubmitObjectPick(pickRay);
        expect(mockSubmit).not.toHaveBeenCalled();
    });
    it("Submits object pick when not authoring", () => {
        const { sceneObject, challenge, objectPick, trySubmitObjectPick, mockPickResult, } = makeTestRig();
        mockPickResult.appObjectID = sceneObject.id;
        const mockSubmit = jest.fn();
        challenge.selectedChallenge = "OBJECT_PICK";
        objectPick.submit = mockSubmit;
        const pickRay = new ParametricLine({
            x0: 1,
            y0: 1,
            z0: 1,
            a: 1,
            b: 1,
            c: 1,
        });
        trySubmitObjectPick(pickRay);
        expect(mockSubmit).toHaveBeenCalled();
    });
    it("Dispatches a submitHitResult", () => {
        const { challenge, objectPick, sceneObject, mockPickResult, trySubmitObjectPick, mockSubmitResult, baseApp, } = makeTestRig();
        const mockSubmit = jest.fn();
        mockPickResult.appObjectID = sceneObject.id;
        challenge.selectedChallenge = "OBJECT_PICK";
        objectPick.submit = mockSubmit;
        const pickRay = new ParametricLine({
            x0: 1,
            y0: 1,
            z0: 1,
            a: 1,
            b: 1,
            c: 1,
        });
        trySubmitObjectPick(pickRay);
        expect(mockSubmitResult.submitHitResult).toHaveBeenCalledWith({
            success: objectPick.hasSubmitted,
            description: baseApp.slideText,
        });
    });
    it("Sets the appState to Review", () => {
        const { sceneObject, challenge, objectPick, trySubmitObjectPick, mockPickResult, baseApp, } = makeTestRig();
        mockPickResult.appObjectID = sceneObject.id;
        const mockSubmit = jest.fn();
        challenge.selectedChallenge = "OBJECT_PICK";
        objectPick.submit = mockSubmit;
        const pickRay = new ParametricLine({
            x0: 1,
            y0: 1,
            z0: 1,
            a: 1,
            b: 1,
            c: 1,
        });
        trySubmitObjectPick(pickRay);
        expect(baseApp.isReviewing).toEqual(true);
    });
});
//# sourceMappingURL=TrySubmitObjectPickUC.test.js.map