import { makeAppObjectRepo, ParametricLine, Vector2, Vector3 } from "@vived/core";
import { makeMockPixelToSceneRayUC, makeMockSceneRayPickUC, } from "../../Babylon";
import { MockPanTransformUC } from "../Mocks";
import { makeTryPanTransformUC } from "./TryPanTransformUC";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const tryDragAO = appObjects.getOrCreate("tryDragAO");
    const uc = makeTryPanTransformUC(tryDragAO);
    const appObj = appObjects.getOrCreate("appObj");
    const screenPeelUC = new MockPanTransformUC(appObj);
    const mockPixelToRay = makeMockPixelToSceneRayUC(appObjects);
    const mockPickResult = {
        hit: true,
        worldPickPoint: new Vector3(1, 2, 3),
        appObjectID: "appObj",
    };
    const mockRayPick = makeMockSceneRayPickUC(appObjects);
    const mockPick = jest.fn().mockReturnValue(mockPickResult);
    mockRayPick.sceneRayPick = mockPick;
    mockPick.mockReturnValue(mockPickResult);
    return { appObjects, uc, mockPickResult, screenPeelUC, mockPixelToRay };
}
describe("Default Try Screen Drag Transform UC", () => {
    it("Returns true if the initialization is successful", () => {
        const { uc } = makeTestRig();
        expect(uc.tryWithRay(ParametricLine.Forward())).toEqual(true);
    });
    it("Returns false if the pick doesn't return an app id", () => {
        const { mockPickResult, uc } = makeTestRig();
        mockPickResult.appObjectID = undefined;
        expect(uc.tryWithRay(ParametricLine.Forward())).toEqual(false);
    });
    it("Returns false if the picked app object doesn't have a ScreenPeelTransformUC", () => {
        const { mockPickResult, uc } = makeTestRig();
        mockPickResult.appObjectID = "somethingElse";
        expect(uc.tryWithRay(ParametricLine.Forward())).toEqual(false);
    });
    it("Initializes the peel UC", () => {
        const { uc, screenPeelUC } = makeTestRig();
        uc.tryWithRay(ParametricLine.Forward());
        expect(screenPeelUC.initializeRay).toHaveBeenCalledWith(ParametricLine.Forward());
    });
    it("Updates the screenPeelUC", () => {
        const { uc, screenPeelUC } = makeTestRig();
        uc.tryWithRay(ParametricLine.Forward());
        uc.moveWithRay(ParametricLine.Forward());
        expect(screenPeelUC.updateRay).toHaveBeenCalledWith(ParametricLine.Forward());
    });
    it("Doesn't update if the init did not happen the screenPeelUC", () => {
        const { uc, screenPeelUC } = makeTestRig();
        uc.moveWithRay(ParametricLine.Forward());
        expect(screenPeelUC.updateRay).not.toHaveBeenCalled();
    });
    it("Stop the UC", () => {
        const { uc, screenPeelUC } = makeTestRig();
        uc.tryWithRay(ParametricLine.Forward());
        uc.stop();
        expect(screenPeelUC.finish).toHaveBeenCalled();
    });
    it("Doesn't stop if the init did not happen the screenPeelUC", () => {
        const { uc, screenPeelUC } = makeTestRig();
        uc.stop();
        expect(screenPeelUC.finish).not.toHaveBeenCalled();
    });
    it("Cancels the UC", () => {
        const { uc, screenPeelUC } = makeTestRig();
        uc.tryWithRay(ParametricLine.Forward());
        uc.cancel();
        expect(screenPeelUC.cancelPan).toHaveBeenCalled();
    });
    it("Doesn't cancel if the init did not happen the screenPeelUC", () => {
        const { uc, screenPeelUC } = makeTestRig();
        uc.cancel();
        expect(screenPeelUC.cancelPan).not.toHaveBeenCalled();
    });
    it("Doesn't update or stop after a cancel", () => {
        const { uc, screenPeelUC } = makeTestRig();
        uc.tryWithRay(ParametricLine.Forward());
        uc.cancel();
        uc.moveWithRay(ParametricLine.Forward());
        uc.stop();
        expect(screenPeelUC.updateRay).not.toHaveBeenCalled();
        expect(screenPeelUC.finish).not.toHaveBeenCalled();
    });
    it("Doesn't update or stop if not allowed", () => {
        const { uc, screenPeelUC } = makeTestRig();
        uc.allow = () => {
            return false;
        };
        uc.tryWithRay(ParametricLine.Forward());
        uc.moveWithRay(ParametricLine.Forward());
        uc.stop();
        expect(screenPeelUC.updateRay).not.toHaveBeenCalled();
        expect(screenPeelUC.finish).not.toHaveBeenCalled();
    });
    it("Forms the ray and initializes with it when a pixel is passed in", () => {
        const { uc, mockPixelToRay } = makeTestRig();
        const initSpy = jest.spyOn(uc, "tryWithRay");
        const ray = ParametricLine.FromTwoPoint(new Vector3(1, 2, 3), new Vector3(4, 5, 6));
        mockPixelToRay.pixelToSceneRay.mockReturnValue(ray);
        uc.tryWithPixel(new Vector2(8, 9));
        expect(mockPixelToRay.pixelToSceneRay).toHaveBeenCalledWith(new Vector2(8, 9));
        expect(initSpy).toHaveBeenCalledWith(ray);
    });
    it("Forms the ray and moves with it when a pixel is passed in", () => {
        const { uc, mockPixelToRay } = makeTestRig();
        const moveSpy = jest.spyOn(uc, "moveWithRay");
        const ray = ParametricLine.FromTwoPoint(new Vector3(1, 2, 3), new Vector3(4, 5, 6));
        mockPixelToRay.pixelToSceneRay.mockReturnValue(ray);
        uc.moveWithPixel(new Vector2(8, 9));
        expect(mockPixelToRay.pixelToSceneRay).toHaveBeenCalledWith(new Vector2(8, 9));
        expect(moveSpy).toHaveBeenCalledWith(ray);
    });
});
//# sourceMappingURL=TryPanTransformUC.test.js.map