"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 HoverLabelEntity_1 = require("../Entities/HoverLabelEntity");
const MockTryShowHoverLabelUC_1 = require("../Mocks/MockTryShowHoverLabelUC");
const HoverLabelTouch_1 = require("./HoverLabelTouch");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const appObject = appObjects.getOrCreate("AppObject");
    const hoverLabel = (0, HoverLabelEntity_1.makeHoverLabelEntity)(appObject);
    (0, TouchController_1.makeTouchInput)(appObject);
    const mockTryShowHoverLabelUC = (0, MockTryShowHoverLabelUC_1.makeMockTryShowHoverLabelUC)(appObjects);
    const controller = (0, HoverLabelTouch_1.makeHoverLabelTouch)(appObject);
    return { hoverLabel, controller, mockTryShowHoverLabelUC };
}
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("Touch label controller", () => {
    it("Calls try hover label use case if there is one touch", () => {
        const { controller, mockTryShowHoverLabelUC } = makeTestRig();
        const input = mockTouchInputEvent([new core_2.Vector2(0, 0)]);
        controller.onTouchStart(input);
        controller.onTouchEnd();
        expect(mockTryShowHoverLabelUC.tryShowScreenHoverLabel).toHaveBeenCalledWith(input.touches[0].position);
    });
    it("Doesn't Call try hover label use case if there is more than one touch", () => {
        const { controller, mockTryShowHoverLabelUC } = makeTestRig();
        const input = mockTouchInputEvent([new core_2.Vector2(0, 0), new core_2.Vector2(1, 1)]);
        controller.onTouchStart(input);
        controller.onTouchEnd();
        expect(mockTryShowHoverLabelUC.tryShowScreenHoverLabel).not.toHaveBeenCalled();
    });
    it("Clears hover label when a new touch starts", () => {
        const { controller, hoverLabel } = makeTestRig();
        hoverLabel.showScreenHoverLabel("obj1", core_2.Vector2.Zero());
        hoverLabel.showScreenHoverLabel("obj1", core_2.Vector2.One());
        expect(hoverLabel.show).toEqual(true);
        const input = mockTouchInputEvent([new core_2.Vector2(0, 0)]);
        controller.onTouchStart(input);
        expect(hoverLabel.show).toEqual(false);
    });
    it("Allows for slight movement when pressing and still shows the label", () => {
        const { controller, mockTryShowHoverLabelUC } = makeTestRig();
        const input = mockTouchInputEvent([new core_2.Vector2(0, 0)]);
        controller.onTouchStart(input);
        const input2 = mockTouchInputEvent([
            new core_2.Vector2(controller.touchLabelDragThreshold - 1, 0),
        ]);
        controller.onTouchMove(input2);
        controller.onTouchEnd();
        expect(mockTryShowHoverLabelUC.tryShowScreenHoverLabel).toHaveBeenCalledWith(input2.touches[0].position);
    });
    it("Doesn't show the label when movement is more than the threshold", () => {
        const { controller, mockTryShowHoverLabelUC } = makeTestRig();
        const input = mockTouchInputEvent([new core_2.Vector2(0, 0)]);
        controller.onTouchStart(input);
        const input2 = mockTouchInputEvent([
            new core_2.Vector2(controller.touchLabelDragThreshold + 1, 0),
        ]);
        controller.onTouchMove(input2);
        controller.onTouchEnd();
        expect(mockTryShowHoverLabelUC.tryShowScreenHoverLabel).not.toHaveBeenCalled();
    });
});
//# sourceMappingURL=HoverLabelTouch.test.js.map