import { makeAppObjectRepo } from "@vived/core";
import { Vector2 } from "@vived/core";
import { makeTouchInput, } from "../../TouchController";
import { makeTryStickAppObjectUCMock } from "../Mocks/TryStickAppObjectUCMock";
import { DoubleTapToStickTC } from "./DoubleTapToStickTC";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const touchInput = makeTouchInput(appObjects.getOrCreate("Input"));
    const registerSpy = jest.spyOn(touchInput, "registerController");
    const tryStickMock = makeTryStickAppObjectUCMock(appObjects);
    const dateNowMock = jest.fn();
    Date.now = dateNowMock;
    dateNowMock.mockReturnValue(0);
    const controller = new DoubleTapToStickTC(appObjects.getOrCreate("AO"));
    return { registerSpy, controller, tryStickMock, dateNowMock };
}
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("Double Tap to stick Controller", () => {
    it("Registers with the input when constructed", () => {
        const { registerSpy, controller } = makeTestRig();
        expect(registerSpy).toHaveBeenCalledWith(controller);
    });
    it("Triggers the UC", () => {
        const { controller, tryStickMock, dateNowMock } = makeTestRig();
        const touch = mockTouchInputEvent([new Vector2(1, 2)]);
        controller.onTap(touch);
        dateNowMock.mockReturnValue(controller.tapTimeMS - 1);
        controller.onTap(touch);
        expect(tryStickMock.tryWithPixel).toHaveBeenCalledWith(new Vector2(1, 2));
    });
    it("Does no call if too much time has passed", () => {
        const { controller, tryStickMock, dateNowMock } = makeTestRig();
        const touch = mockTouchInputEvent([new Vector2(1, 2)]);
        controller.onTap(touch);
        dateNowMock.mockReturnValue(controller.tapTimeMS + 1);
        controller.onTap(touch);
        expect(tryStickMock.tryWithPixel).not.toHaveBeenCalled();
    });
    it("Does not count a double tap if the taps are too far apart", () => {
        const { controller, tryStickMock } = makeTestRig();
        const tap1 = mockTouchInputEvent([new Vector2(1, 2)]);
        const tap2 = mockTouchInputEvent([
            new Vector2(1, 2 + controller.maxTapDistancePx + 1),
        ]);
        const dateNowMock = jest.fn();
        Date.now = dateNowMock;
        dateNowMock.mockReturnValue(0);
        controller.onTap(tap1);
        controller.onTap(tap2);
        expect(tryStickMock.tryWithPixel).not.toHaveBeenCalled();
    });
});
//# sourceMappingURL=DoubleTapToStickTC.test.js.map