import { makeAppObjectRepo } from "@vived/core";
import { ParametricLine, Vector2, Vector3 } from "@vived/core";
import { makeBaseAppState } from "../../BaseApp/Entity/BaseAppState";
import { makeMockPixelToSceneRayUC, makeMockSceneRayPickUC, } from "../../Babylon/Mocks";
import { makeChallengeEntityMock } from "../../Challenge/Mocks/ChallengeEntityMock";
import { makeMockDispatchStateChange } from "../../Dispatcher/Mocks/MockDispatchStateChange";
import { makeUndoRedoEntity } from "../../UndoRedo/Entities/UndoRedoEntity";
import { makeObjectPickEntity } from "../Entities/ObjectPickEntity";
import { makeTrySelectObjectPickUC, TrySelectObjectPickUC, } from "./TrySelectObjectPickUC";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const mockDispatchStateChange = makeMockDispatchStateChange(appObjects);
    const baseApp = makeBaseAppState(appObjects.getOrCreate("App"));
    const undo = makeUndoRedoEntity(appObjects.getOrCreate("Undo"));
    const challenge = makeChallengeEntityMock(appObjects);
    const objectPick = makeObjectPickEntity(appObjects.getOrCreate("ObjectPick"));
    baseApp.isAuthoring = true;
    objectPick.isSelectingCorrectObjects = true;
    challenge.selectedChallenge = "OBJECT_PICK";
    const mockPickUC = makeMockSceneRayPickUC(appObjects);
    const mockPickResult = {
        hit: true,
        appObjectID: "sceneObject",
    };
    mockPickUC.sceneRayPick.mockReturnValue(mockPickResult);
    const mockPixelToRay = makeMockPixelToSceneRayUC(appObjects);
    const uc = makeTrySelectObjectPickUC(appObjects.getOrCreate("ObjectPick"));
    return {
        objectPick,
        uc,
        baseApp,
        mockDispatchStateChange,
        appObjects,
        mockPixelToRay,
        undo,
        mockPickUC,
    };
}
describe("Try Select Object Pick Use Case", () => {
    it("Gets the singleton", () => {
        const { appObjects, uc } = makeTestRig();
        expect(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 = 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("Tries Select Object Pick successfully", () => {
        const { uc, objectPick } = makeTestRig();
        const spy = jest.spyOn(objectPick, "addCorrectObjectId");
        uc.tryWithRay(ParametricLine.Forward());
        expect(spy).toHaveBeenCalledWith("sceneObject");
    });
    it("Does not add when the pick does not hit", () => {
        const { uc, mockPickUC, objectPick } = makeTestRig();
        const mockPickResult = {
            hit: false,
        };
        mockPickUC.sceneRayPick.mockReturnValue(mockPickResult);
        mockPickResult.hit = false;
        const spy = jest.spyOn(objectPick, "addCorrectObjectId");
        uc.tryWithRay(ParametricLine.Forward());
        expect(spy).not.toHaveBeenCalled();
    });
    it("Does not add when the pick does not an app object", () => {
        const { uc, mockPickUC, objectPick } = makeTestRig();
        const mockPickResult = {
            hit: true,
            appObjectID: undefined,
        };
        mockPickUC.sceneRayPick.mockReturnValue(mockPickResult);
        mockPickResult.hit = false;
        const spy = jest.spyOn(objectPick, "addCorrectObjectId");
        uc.tryWithRay(ParametricLine.Forward());
        expect(spy).not.toHaveBeenCalled();
    });
    it("Dispatches a state change", () => {
        const { uc, mockDispatchStateChange } = makeTestRig();
        uc.tryWithRay(ParametricLine.Forward());
        expect(mockDispatchStateChange.doDispatch).toHaveBeenCalled();
    });
    it("Is undoable and redoable", () => {
        const { uc, undo, objectPick } = makeTestRig();
        uc.tryWithRay(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, mockDispatchStateChange } = makeTestRig();
        uc.tryWithRay(ParametricLine.Forward());
        mockDispatchStateChange.doDispatch.mockClear();
        undo.doUndo();
        expect(mockDispatchStateChange.doDispatch).toHaveBeenCalled();
        mockDispatchStateChange.doDispatch.mockClear();
        undo.doRedo();
        expect(mockDispatchStateChange.doDispatch).toHaveBeenCalled();
    });
});
//# sourceMappingURL=TrySelectObjectPickUC.test.js.map