import { makeAppObjectRepo } from "@vived/core";
import { Vector2 } from "@vived/core";
import { makeTouchInput, } from "../../TouchController";
import { makeHoverLabelEntity } from "../Entities/HoverLabelEntity";
import { makeMockTryShowHoverLabelUC } from "../Mocks/MockTryShowHoverLabelUC";
import { makeHoverLabelTouch } from "./HoverLabelTouch";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const appObject = appObjects.getOrCreate("AppObject");
    const hoverLabel = makeHoverLabelEntity(appObject);
    makeTouchInput(appObject);
    const mockTryShowHoverLabelUC = makeMockTryShowHoverLabelUC(appObjects);
    const controller = makeHoverLabelTouch(appObject);
    return { hoverLabel, controller, mockTryShowHoverLabelUC };
}
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 label controller", () => {
    it("Calls try hover label use case if there is one touch", () => {
        const { controller, mockTryShowHoverLabelUC } = makeTestRig();
        const input = mockTouchInputEvent([new 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 Vector2(0, 0), new 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", Vector2.Zero());
        hoverLabel.showScreenHoverLabel("obj1", Vector2.One());
        expect(hoverLabel.show).toEqual(true);
        const input = mockTouchInputEvent([new 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 Vector2(0, 0)]);
        controller.onTouchStart(input);
        const input2 = mockTouchInputEvent([
            new 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 Vector2(0, 0)]);
        controller.onTouchStart(input);
        const input2 = mockTouchInputEvent([
            new Vector2(controller.touchLabelDragThreshold + 1, 0),
        ]);
        controller.onTouchMove(input2);
        controller.onTouchEnd();
        expect(mockTryShowHoverLabelUC.tryShowScreenHoverLabel).not.toHaveBeenCalled();
    });
});
//# sourceMappingURL=HoverLabelTouch.test.js.map