"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 Mocks_1 = require("../../Babylon/Mocks");
const ChallengeEntityMock_1 = require("../../Challenge/Mocks/ChallengeEntityMock");
const MockDispatchStateChange_1 = require("../../Dispatcher/Mocks/MockDispatchStateChange");
const UndoRedoEntity_1 = require("../../UndoRedo/Entities/UndoRedoEntity");
const ObjectPickEntity_1 = require("../Entities/ObjectPickEntity");
const TrySelectObjectPickUC_1 = require("./TrySelectObjectPickUC");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const mockDispatchStateChange = (0, MockDispatchStateChange_1.makeMockDispatchStateChange)(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatchStateChange.doDispatch = doDispatchMock;
    const baseApp = (0, BaseAppState_1.makeBaseAppState)(appObjects.getOrCreate("App"));
    const undo = (0, UndoRedoEntity_1.makeUndoRedoEntity)(appObjects.getOrCreate("Undo"));
    const challenge = (0, ChallengeEntityMock_1.makeChallengeEntityMock)(appObjects);
    const objectPick = (0, ObjectPickEntity_1.makeObjectPickEntity)(appObjects.getOrCreate("ObjectPick"));
    baseApp.isAuthoring = true;
    objectPick.isSelectingCorrectObjects = true;
    challenge.selectedChallenge = "OBJECT_PICK";
    const mockPickResult = {
        hit: true,
        appObjectID: "sceneObject",
    };
    const mockRayPick = (0, Mocks_1.makeMockSceneRayPickUC)(appObjects);
    const mockPick = jest.fn().mockReturnValue(mockPickResult);
    mockRayPick.sceneRayPick = mockPick;
    const mockPixelToRay = (0, Mocks_1.makeMockPixelToSceneRayUC)(appObjects);
    const uc = (0, TrySelectObjectPickUC_1.makeTrySelectObjectPickUC)(appObjects.getOrCreate("ObjectPick"));
    return {
        objectPick,
        uc,
        baseApp,
        doDispatchMock,
        appObjects,
        mockPixelToRay,
        undo,
        mockPick,
    };
}
describe("Try Select Object Pick Use Case", () => {
    it("Gets the singleton", () => {
        const { appObjects, uc } = makeTestRig();
        expect(TrySelectObjectPickUC_1.TrySelectObjectPickUC.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("Tries Select Object Pick successfully", () => {
        const { uc, objectPick } = makeTestRig();
        const spy = jest.spyOn(objectPick, "addCorrectObjectId");
        uc.tryWithRay(core_2.ParametricLine.Forward());
        expect(spy).toHaveBeenCalledWith("sceneObject");
    });
    it("Does not add when the pick does not hit", () => {
        const { uc, mockPick, objectPick } = makeTestRig();
        const mockPickResult = {
            hit: false,
        };
        mockPick.mockReturnValue(mockPickResult);
        mockPickResult.hit = false;
        const spy = jest.spyOn(objectPick, "addCorrectObjectId");
        uc.tryWithRay(core_2.ParametricLine.Forward());
        expect(spy).not.toHaveBeenCalled();
    });
    it("Does not add when the pick does not an app object", () => {
        const { uc, mockPick, objectPick } = makeTestRig();
        const mockPickResult = {
            hit: true,
            appObjectID: undefined,
        };
        mockPick.mockReturnValue(mockPickResult);
        mockPickResult.hit = false;
        const spy = jest.spyOn(objectPick, "addCorrectObjectId");
        uc.tryWithRay(core_2.ParametricLine.Forward());
        expect(spy).not.toHaveBeenCalled();
    });
    it("Dispatches a state change", () => {
        const { uc, doDispatchMock } = makeTestRig();
        uc.tryWithRay(core_2.ParametricLine.Forward());
        expect(doDispatchMock).toHaveBeenCalled();
    });
    it("Is undoable and redoable", () => {
        const { uc, undo, objectPick } = makeTestRig();
        uc.tryWithRay(core_2.ParametricLine.Forward());
        expect(objectPick.correctObjectIds).toEqual(["sceneObject"]);
        undo.doUndo();
        expect(objectPick.correctObjectIds).toEqual([]);
        undo.doRedo();
        expect(objectPick.correctObjectIds).toEqual(["sceneObject"]);
    });
    it("Undo and redo dispatch a state change", () => {
        const { uc, undo, doDispatchMock } = makeTestRig();
        uc.tryWithRay(core_2.ParametricLine.Forward());
        doDispatchMock.mockClear();
        undo.doUndo();
        expect(doDispatchMock).toHaveBeenCalled();
        doDispatchMock.mockClear();
        undo.doRedo();
        expect(doDispatchMock).toHaveBeenCalled();
    });
});
//# sourceMappingURL=TrySelectObjectPickUC.test.js.map