"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const BaseAppState_1 = require("../../BaseApp/Entity/BaseAppState");
const Babylon_1 = require("../../Babylon");
const Challenge_1 = require("../../Challenge");
const Dispatcher_1 = require("../../Dispatcher");
const ObjectPickEntity_1 = require("../Entities/ObjectPickEntity");
const TrySubmitObjectPickUC_1 = require("./TrySubmitObjectPickUC");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const baseApp = (0, BaseAppState_1.makeBaseAppState)(appObjects.getOrCreate("App"));
    baseApp.isAuthoring = false;
    const challenge = (0, Challenge_1.makeChallengeEntityMock)(appObjects);
    const mockSubmitResult = (0, Dispatcher_1.makeMockSubmitResult)(appObjects);
    const mockPickResult = {
        hit: true,
    };
    const mockRayPick = (0, Babylon_1.makeMockSceneRayPickUC)(appObjects);
    const mockPick = jest.fn().mockReturnValue(mockPickResult);
    mockRayPick.sceneRayPick = mockPick;
    const mockPixelToRay = (0, Babylon_1.makeMockPixelToSceneRayUC)(appObjects);
    const objectPick = (0, ObjectPickEntity_1.makeObjectPickEntity)(appObjects.getOrCreate("ObjectPick"));
    const sceneObject = appObjects.getOrCreate("parent");
    const uc = (0, TrySubmitObjectPickUC_1.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_1.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 = core_2.ParametricLine.FromTwoPoint(new core_2.Vector3(1, 2, 3), new core_2.Vector3(4, 5, 6));
        mockPixelToRay.pixelToSceneRay.mockReturnValue(mockRay);
        uc.tryWithRay = jest.fn();
        uc.tryWithPixel(new core_2.Vector2(7, 8));
        expect(mockPixelToRay.pixelToSceneRay).toHaveBeenCalledWith(new core_2.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 core_2.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 core_2.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 core_2.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 core_2.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 core_2.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 core_2.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