import { makeAppObjectRepo } from "@vived/core";
import { ParametricLine, Vector2 } from "@vived/core";
import { makeMockPixelToSceneRayUC } from "../../Babylon";
import { MockTouchInputEntity } from "../../TouchController/Mock/MockTouchInput";
import { TapToSelectAppObject } from "./TapToSelectAppObject";
import { SelectAppObjectUCMock } from "../Mocks/SelectAppObjectUCMock";
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 SelectAppObjectUCMock(appObj);
    const mockTouchInput = new MockTouchInputEntity(appObj);
    const controller = new TapToSelectAppObject(appObj);
    const mockAllow = jest.fn();
    mockAllow.mockReturnValue(true);
    controller.allow = mockAllow;
    return {
        appObjects,
        controller,
        mockUC,
        mockTouchInput,
        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("Tap to select Scene Object Touch Controller", () => {
    it("Sends a ray to the UC on select", () => {
        const { mockUC, controller } = makeTestRig();
        const event = mockTouchInputEvent([new Vector2(1, 2)]);
        controller.onTouchStart(event);
        controller.onTouchEnd();
        expect(mockUC.trySelectAppObject).toHaveBeenCalledWith(ParametricLine.Forward());
    });
    it("Does not send a ray to the UC on select if not allowed", () => {
        const { mockUC, controller, mockAllow } = makeTestRig();
        const event = mockTouchInputEvent([new Vector2(1, 2)]);
        mockAllow.mockReturnValue(false);
        controller.onTouchStart(event);
        controller.onTouchEnd();
        expect(mockUC.trySelectAppObject).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 TapToSelectAppObject(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 TapToSelectAppObject(anotherAppObj);
        expect(mockTouchInput.registerController).not.toHaveBeenCalled();
    });
    it("Does not send a ray to the UC when a tap occurs with two touches", () => {
        const { mockUC, controller } = makeTestRig();
        const event = mockTouchInputEvent([new Vector2(1, 2), new Vector2(2, 3)]);
        controller.onTouchStart(event);
        controller.onTouchEnd();
        expect(mockUC.trySelectAppObject).not.toHaveBeenCalled();
    });
    it("Sends a ray to the UC when a one touch moves within limit", () => {
        const { controller, mockUC } = makeTestRig();
        const event = mockTouchInputEvent([new Vector2(0, 0)]);
        const event2 = mockTouchInputEvent([
            new Vector2(0, TapToSelectAppObject.maxTapDistancePx - 1),
        ]);
        controller.onTouchStart(event);
        controller.onTouchMove(event2);
        controller.onTouchEnd();
        expect(mockUC.trySelectAppObject).toHaveBeenCalledWith(ParametricLine.Forward());
    });
    it("Doesn't send a ray to the UC when a one touch moves beyond limit", () => {
        const { controller, mockUC } = makeTestRig();
        const event = mockTouchInputEvent([new Vector2(0, 0)]);
        const event2 = mockTouchInputEvent([
            new Vector2(0, TapToSelectAppObject.maxTapDistancePx + 1),
        ]);
        controller.onTouchStart(event);
        controller.onTouchMove(event2);
        controller.onTouchEnd();
        expect(mockUC.trySelectAppObject).not.toHaveBeenCalled();
    });
    it("Does not send a ray to the UC when a one touch starts but a second is added", () => {
        const { controller, mockUC } = makeTestRig();
        const touch = mockTouchInputEvent([new Vector2(0, 0)]);
        const touch2 = mockTouchInputEvent([new Vector2(1, 1), new Vector2(2, 2)]);
        controller.onTouchStart(touch);
        controller.onTouchMove(touch2);
        controller.onTouchEnd();
        expect(mockUC.trySelectAppObject).not.toHaveBeenCalled();
    });
});
//# sourceMappingURL=TapToSelectAppObject.test.js.map