import { makeAppObjectRepo } from "@vived/core";
import { ParametricLine, Vector2 } from "@vived/core";
import { makeMockPixelToSceneRayUC } from "../../Babylon";
import { MockTouchInputEntity } from "../../TouchController/Mock/MockTouchInput";
import { PressAppObjectUCMock } from "../Mocks/PressAppObjectUCMock";
import { TouchPressOnAppObject } from "./TouchPressOnAppObject";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const mockPixelToSceneUC = makeMockPixelToSceneRayUC(appObjects);
    const mockPixelToRay = jest.fn().mockReturnValue(ParametricLine.Forward());
    mockPixelToSceneUC.pixelToSceneRay = mockPixelToRay;
    const appObj = appObjects.getOrCreate("appObj");
    const mockUC = new PressAppObjectUCMock(appObj);
    const mockTouchInput = new MockTouchInputEntity(appObj);
    const controller = new TouchPressOnAppObject(appObj);
    const mockAllow = jest.fn();
    mockAllow.mockReturnValue(true);
    controller.allow = mockAllow;
    return {
        appObjects,
        controller,
        mockTouchInput,
        mockUC,
        mockPixelToRay,
        mockAllow,
    };
}
function mockTouchInputEvent(touchPositions) {
    const mockEvt = new TouchEvent("");
    const touchCount = touchPositions.length;
    let sumPos = Vector2.Zero();
    const touches = touchPositions.map((pos, i) => {
        sumPos = Vector2.Add(sumPos, pos);
        return {
            id: i,
            position: pos,
        };
    });
    const avgPos = touchCount === 0
        ? Vector2.Zero()
        : new Vector2(sumPos.x / touchCount, sumPos.y / touchCount);
    return {
        event: mockEvt,
        touchCenter: avgPos,
        touchCount,
        touches,
    };
}
describe("Touch Press Scene Object On App Controller", () => {
    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 TouchPressOnAppObject(anotherAppObj);
        expect(console.warn).toHaveBeenCalled();
    });
    it("Does not register itself if the object does not have a UC", () => {
        const { appObjects, mockTouchInput } = makeTestRig();
        const anotherAppObj = appObjects.getOrCreate("newAppObj");
        console.warn = jest.fn();
        mockTouchInput.registerController.mockClear();
        new TouchPressOnAppObject(anotherAppObj);
        expect(mockTouchInput.registerController).not.toHaveBeenCalled();
    });
    it("Sends a ray to the UC when there is a single touch", () => {
        const { mockUC, controller } = makeTestRig();
        const event = mockTouchInputEvent([new Vector2(1, 2)]);
        controller.onTouchStart(event);
        expect(mockUC.tryPressAppObject).toHaveBeenCalledWith(ParametricLine.Forward());
    });
    it("Doesn not send a ray to the UC when there is a single but not allowed", () => {
        const { mockUC, controller, mockAllow } = makeTestRig();
        const event = mockTouchInputEvent([new Vector2(1, 2)]);
        mockAllow.mockReturnValue(false);
        controller.onTouchStart(event);
        expect(mockUC.tryPressAppObject).not.toHaveBeenCalled();
    });
    it("Does not send a ray to the UC when there are two touches", () => {
        const { mockUC, controller } = makeTestRig();
        const event = mockTouchInputEvent([new Vector2(1, 2), new Vector2(2, 3)]);
        controller.onTouchStart(event);
        expect(mockUC.tryPressAppObject).not.toHaveBeenCalled();
    });
    it("Sets pressedID to null when finger lifts", () => {
        const { mockUC, controller } = makeTestRig();
        const event = mockTouchInputEvent([new Vector2(1, 2)]);
        controller.onTouchStart(event);
        controller.onTouchEnd();
        expect(mockUC.pressAppObject).toHaveBeenCalled();
    });
    it("Sets pressedID to null when when there is more than one touch", () => {
        const { mockUC, controller } = makeTestRig();
        const event = mockTouchInputEvent([new Vector2(1, 2), new Vector2(2, 3)]);
        controller.onTouchStart(event);
        controller.onTouchMove(event);
        expect(mockUC.pressAppObject).toHaveBeenCalled();
    });
    it("Sets pressedID to null when the touch moves more than 5 pixels while pressed", () => {
        const { controller, mockUC } = makeTestRig();
        const event = mockTouchInputEvent([new Vector2(0, 0)]);
        const event2 = mockTouchInputEvent([
            new Vector2(0, TouchPressOnAppObject.maxTapDistancePx + 1),
        ]);
        controller.onTouchStart(event);
        controller.onTouchMove(event2);
        expect(mockUC.pressAppObject).toHaveBeenCalled();
    });
});
//# sourceMappingURL=TouchPressOnAppObject.test.js.map