"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const TouchController_1 = require("../../TouchController");
const TryStickAppObjectUCMock_1 = require("../Mocks/TryStickAppObjectUCMock");
const DoubleTapToStickTC_1 = require("./DoubleTapToStickTC");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const touchInput = (0, TouchController_1.makeTouchInput)(appObjects.getOrCreate("Input"));
    const registerSpy = jest.spyOn(touchInput, "registerController");
    const tryStickMock = (0, TryStickAppObjectUCMock_1.makeTryStickAppObjectUCMock)(appObjects);
    const dateNowMock = jest.fn();
    Date.now = dateNowMock;
    dateNowMock.mockReturnValue(0);
    const controller = new DoubleTapToStickTC_1.DoubleTapToStickTC(appObjects.getOrCreate("AO"));
    return { registerSpy, controller, tryStickMock, dateNowMock };
}
function mockTouchInputEvent(touchPositions) {
    const mockEvt = new TouchEvent("");
    const touchCount = touchPositions.length;
    let sumPos = core_2.Vector2.Zero();
    const touches = touchPositions.map((pos, i) => {
        sumPos = core_2.Vector2.Add(sumPos, pos);
        return {
            id: i,
            position: pos,
        };
    });
    const avgPos = touchCount === 0
        ? core_2.Vector2.Zero()
        : new core_2.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 core_2.Vector2(1, 2)]);
        controller.onTap(touch);
        dateNowMock.mockReturnValue(controller.tapTimeMS - 1);
        controller.onTap(touch);
        expect(tryStickMock.tryWithPixel).toHaveBeenCalledWith(new core_2.Vector2(1, 2));
    });
    it("Does no call if too much time has passed", () => {
        const { controller, tryStickMock, dateNowMock } = makeTestRig();
        const touch = mockTouchInputEvent([new core_2.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 core_2.Vector2(1, 2)]);
        const tap2 = mockTouchInputEvent([
            new core_2.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