import { makeAppObjectRepo, ParametricLine, Vector2 } from "@vived/core";
import { makeMockPixelToSceneRayUC } from "../../Babylon";
import { MockMouseInput } from "../../MouseInput/Mocks/MockMouseInput";
import { makeZSpaceEntity } from "../../zSpace/Entities/ZSpaceEntity";
import { PressAppObjectUCMock } from "../Mocks/PressAppObjectUCMock";
import { MousePressOnAppObject } from "./MousePressOnAppObject";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const zSpaceAO = appObjects.getOrCreate("zSpace");
    const zSpace = makeZSpaceEntity(zSpaceAO);
    zSpace.emulate = false;
    const selectionAO = appObjects.getOrCreate("selection");
    const mockUC = new PressAppObjectUCMock(selectionAO);
    const mockPixelToSceneUC = makeMockPixelToSceneRayUC(appObjects);
    const mockPixelToRay = jest.fn().mockReturnValue(ParametricLine.Forward());
    mockPixelToSceneUC.pixelToSceneRay = mockPixelToRay;
    const mockMouseInput = new MockMouseInput(selectionAO);
    const controller = new MousePressOnAppObject(selectionAO);
    const mockAllow = jest.fn();
    mockAllow.mockReturnValue(true);
    controller.allow = mockAllow;
    return {
        mockMouseInput,
        appObjects,
        zSpace,
        controller,
        mockUC,
        mockAllow,
    };
}
describe("Mouse Press Scene Object On App Controller", () => {
    it("Sends a ray to the UC when the left button is pressed", () => {
        const { mockUC, controller } = makeTestRig();
        const event = {
            button: 0,
            position: new Vector2(2, 3),
            event: new MouseEvent("buttondown"),
        };
        controller.onDown(event);
        expect(mockUC.tryPressAppObject).toHaveBeenCalledWith(ParametricLine.Forward());
    });
    it("Does not send a ray to the UC when the left button is not pressed", () => {
        const { controller, mockUC } = makeTestRig();
        const event = {
            button: -1,
            position: new Vector2(2, 3),
            event: new MouseEvent("buttondown"),
        };
        controller.onDown(event);
        expect(mockUC.tryPressAppObject).not.toHaveBeenCalled();
    });
    it("Warns if it is added to an app object that does not have a UC", () => {
        const { appObjects } = makeTestRig();
        const anotherAppObj = appObjects.getOrCreate("newAppObj");
        console.warn = jest.fn();
        new MousePressOnAppObject(anotherAppObj);
        expect(console.warn).toHaveBeenCalled();
    });
    it("Does not register itself if the object does not have a UC", () => {
        const { appObjects, mockMouseInput } = makeTestRig();
        const anotherAppObj = appObjects.getOrCreate("newAppObj");
        console.warn = jest.fn();
        mockMouseInput.registerController.mockClear();
        new MousePressOnAppObject(anotherAppObj);
        expect(mockMouseInput.registerController).not.toHaveBeenCalled();
    });
    it("Sets pressID to null when the left button is released", () => {
        const { mockUC, controller } = makeTestRig();
        const event = {
            button: 0,
            position: new Vector2(2, 3),
            event: new MouseEvent("buttonup"),
        };
        controller.onUp(event);
        expect(mockUC.pressAppObject).toHaveBeenCalled();
    });
    it("Sets pressID to null when the mouse moves more than 5 pixels while pressed", () => {
        const { controller, mockUC } = makeTestRig();
        const event = {
            button: 0,
            position: new Vector2(0, 0),
            event: new MouseEvent("buttondown"),
        };
        const event2 = {
            button: 0,
            position: new Vector2(0, MousePressOnAppObject.maxTapDistancePx + 1),
            event: new MouseEvent("buttonmove"),
        };
        controller.onDown(event);
        controller.onMove(event2);
        expect(mockUC.pressAppObject).toHaveBeenCalled();
    });
    it("Does not set pressID to null when the mouse moves less than 5 pixels while pressed", () => {
        const { controller, mockUC } = makeTestRig();
        const event = {
            button: 0,
            position: new Vector2(0, 0),
            event: new MouseEvent("buttondown"),
        };
        const event2 = {
            button: 0,
            position: new Vector2(0, MousePressOnAppObject.maxTapDistancePx - 1),
            event: new MouseEvent("buttonmove"),
        };
        controller.onDown(event);
        controller.onMove(event2);
        expect(mockUC.pressAppObject).not.toHaveBeenCalled();
    });
    it("Does not send a ray to the UC if zSpace mouse emulate is on", () => {
        const { mockUC, controller, zSpace } = makeTestRig();
        zSpace.emulate = true;
        const event = {
            button: 0,
            position: new Vector2(2, 3),
            event: new MouseEvent("buttondown"),
        };
        controller.onDown(event);
        expect(mockUC.pressAppObject).not.toHaveBeenCalledWith();
    });
});
//# sourceMappingURL=MousePressOnAppObject.test.js.map