"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Tools_1 = require("../../Tools");
const MockTouchInput_1 = require("../../TouchController/Mock/MockTouchInput");
const mockTouchInputEvent_1 = require("../../TouchController/Mock/mockTouchInputEvent");
const Mocks_1 = require("../Mocks");
const TouchPan_1 = require("./TouchPan");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const app = appObjects.getOrCreate("app");
    new MockTouchInput_1.MockTouchInputEntity(app);
    const appObj = appObjects.getOrCreate("AO");
    const mockPan = new Mocks_1.MockPanSceneRootUC(appObj);
    const panController = new TouchPan_1.TouchPan(appObj);
    const toolRepo = (0, Tools_1.makeToolRepo)(appObjects.getOrCreate("ToolRepo"));
    const panTool = toolRepo.makeTool("PAN");
    panTool.state = Tools_1.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 = (0, mockTouchInputEvent_1.mockTouchInputEvent)([new core_2.Vector2(0, 0), new core_2.Vector2(79, 0)]);
        panController.onTouchStart(input);
        expect(mockPan.initializePan).toHaveBeenCalledWith(new core_2.Vector2(39.5, 0));
    });
    it("Does not initialize if allow return false", () => {
        const { panController, mockPan } = makeTestRig();
        panController.allow = () => {
            return false;
        };
        const input = (0, mockTouchInputEvent_1.mockTouchInputEvent)([new core_2.Vector2(0, 0), new core_2.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 = (0, mockTouchInputEvent_1.mockTouchInputEvent)([new core_2.Vector2(0, 0)]);
        panController.onTouchStart(startInput);
        const moveInput = (0, mockTouchInputEvent_1.mockTouchInputEvent)([new core_2.Vector2(-50, 0)]);
        panController.onTouchMove(moveInput);
        expect(mockPan.doPan).toHaveBeenCalledWith(new core_2.Vector2(-50, 0));
    });
    it("Does not send Pan if not initialized", () => {
        const { panController, mockPan } = makeTestRig();
        const moveInput = (0, mockTouchInputEvent_1.mockTouchInputEvent)([new core_2.Vector2(-50, 0)]);
        panController.onTouchMove(moveInput);
        expect(mockPan.doPan).not.toHaveBeenCalled();
    });
    it("Finalizes the pan", () => {
        const { panController, mockPan } = makeTestRig();
        const startInput = (0, mockTouchInputEvent_1.mockTouchInputEvent)([new core_2.Vector2(0, 0)]);
        panController.onTouchStart(startInput);
        const endInput = (0, mockTouchInputEvent_1.mockTouchInputEvent)([new core_2.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 = (0, mockTouchInputEvent_1.mockTouchInputEvent)([new core_2.Vector2(50, 0)]);
        panController.onTouchStart(startInput);
        const moveInput = (0, mockTouchInputEvent_1.mockTouchInputEvent)([new core_2.Vector2(40, 0)]);
        panController.onTouchMove(moveInput);
        expect(mockPan.doPan).toHaveBeenCalledWith(new core_2.Vector2(40, 0));
        mockPan.doPan.mockClear();
        panController.allow = () => {
            return false;
        };
        const startInput2 = (0, mockTouchInputEvent_1.mockTouchInputEvent)([
            new core_2.Vector2(0, 0),
            new core_2.Vector2(85, 0),
        ]);
        panController.onTouchStart(startInput2);
        const moveInput2 = (0, mockTouchInputEvent_1.mockTouchInputEvent)([new core_2.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 = (0, mockTouchInputEvent_1.mockTouchInputEvent)([new core_2.Vector2(0, 0), new core_2.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 = (0, mockTouchInputEvent_1.mockTouchInputEvent)([new core_2.Vector2(0, 0), new core_2.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 = Tools_1.ToolState.ACTIVE;
        const evt = (0, mockTouchInputEvent_1.mockTouchInputEvent)([new core_2.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 = Tools_1.ToolState.ENABLED;
        const evt = (0, mockTouchInputEvent_1.mockTouchInputEvent)([new core_2.Vector2(0, 0)]);
        expect(panController.allow(evt)).toEqual(false);
    });
});
//# sourceMappingURL=TouchPan.test.js.map