import { makeAppObjectRepo } from "@vived/core";
import { Vector2 } from "@vived/core";
import { makeToolRepo, ToolState } from "../../Tools";
import { MockTouchInputEntity } from "../../TouchController/Mock/MockTouchInput";
import { mockTouchInputEvent } from "../../TouchController/Mock/mockTouchInputEvent";
import { MockPanSceneRootUC } from "../Mocks";
import { TouchPan } from "./TouchPan";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const app = appObjects.getOrCreate("app");
    new MockTouchInputEntity(app);
    const appObj = appObjects.getOrCreate("AO");
    const mockPan = new MockPanSceneRootUC(appObj);
    const panController = new TouchPan(appObj);
    const toolRepo = makeToolRepo(appObjects.getOrCreate("ToolRepo"));
    const panTool = toolRepo.makeTool("PAN");
    panTool.state = ToolState.ACTIVE;
    return { panController, mockPan, panTool };
}
describe("Touch Pan Controller", () => {
    it("Initializes if allow return true", () => {
        const { panController, mockPan } = makeTestRig();
        panController.allow = () => {
            return true;
        };
        const input = mockTouchInputEvent([new Vector2(0, 0), new Vector2(79, 0)]);
        panController.onTouchStart(input);
        expect(mockPan.initializePan).toHaveBeenCalledWith(new Vector2(39.5, 0));
    });
    it("Does not initialize if allow return false", () => {
        const { panController, mockPan } = makeTestRig();
        panController.allow = () => {
            return false;
        };
        const input = mockTouchInputEvent([new Vector2(0, 0), new Vector2(79, 0)]);
        panController.onTouchStart(input);
        expect(mockPan.initializePan).not.toHaveBeenCalled();
    });
    it("Sends updates the Pan if the fingers move after initialization", () => {
        const { panController, mockPan } = makeTestRig();
        const startInput = mockTouchInputEvent([new Vector2(0, 0)]);
        panController.onTouchStart(startInput);
        const moveInput = mockTouchInputEvent([new Vector2(-50, 0)]);
        panController.onTouchMove(moveInput);
        expect(mockPan.doPan).toHaveBeenCalledWith(new Vector2(-50, 0));
    });
    it("Does not send Pan if not initialized", () => {
        const { panController, mockPan } = makeTestRig();
        const moveInput = mockTouchInputEvent([new Vector2(-50, 0)]);
        panController.onTouchMove(moveInput);
        expect(mockPan.doPan).not.toHaveBeenCalled();
    });
    it("Finalizes the pan", () => {
        const { panController, mockPan } = makeTestRig();
        const startInput = mockTouchInputEvent([new Vector2(0, 0)]);
        panController.onTouchStart(startInput);
        const endInput = mockTouchInputEvent([new Vector2(-50, 0)]);
        panController.onTouchEnd(endInput);
        expect(mockPan.finishPan).toHaveBeenCalled();
    });
    it("Cancels orbit if the allow changes it's response", () => {
        const { panController, mockPan } = makeTestRig();
        const startInput = mockTouchInputEvent([new Vector2(50, 0)]);
        panController.onTouchStart(startInput);
        const moveInput = mockTouchInputEvent([new Vector2(40, 0)]);
        panController.onTouchMove(moveInput);
        expect(mockPan.doPan).toHaveBeenCalledWith(new Vector2(40, 0));
        mockPan.doPan.mockClear();
        panController.allow = () => {
            return false;
        };
        const startInput2 = mockTouchInputEvent([
            new Vector2(0, 0),
            new Vector2(85, 0),
        ]);
        panController.onTouchStart(startInput2);
        const moveInput2 = mockTouchInputEvent([new Vector2(-50, 0)]);
        panController.onTouchMove(moveInput2);
        expect(mockPan.doPan).not.toHaveBeenCalled();
        expect(mockPan.cancelPan).toHaveBeenCalled();
    });
    it("Allows pan if the touch count is 2 and the touches are greater than 80px apart", () => {
        const { panController } = makeTestRig();
        const evt = mockTouchInputEvent([new Vector2(0, 0), new Vector2(81, 0)]);
        expect(panController.allow(evt)).toEqual(true);
    });
    it("Does not allow pan if the touch count is 2 and the touches are less than 80px apart", () => {
        const { panController } = makeTestRig();
        const evt = mockTouchInputEvent([new Vector2(0, 0), new Vector2(79, 0)]);
        expect(panController.allow(evt)).toEqual(false);
    });
    it("Allows Pan if the touch count is 1 and the tools is PAN", () => {
        const { panController, panTool } = makeTestRig();
        panTool.state = ToolState.ACTIVE;
        const evt = mockTouchInputEvent([new Vector2(0, 0)]);
        expect(panController.allow(evt)).toEqual(true);
    });
    it("Does not allow if the touch count is 1 but Pan is not active", () => {
        const { panController, panTool } = makeTestRig();
        panTool.state = ToolState.ENABLED;
        const evt = mockTouchInputEvent([new Vector2(0, 0)]);
        expect(panController.allow(evt)).toEqual(false);
    });
});
//# sourceMappingURL=TouchPan.test.js.map