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 { MockOrbitSceneRootUC } from "../Mocks/MockOrbitSceneRootUC";
import { TouchOrbit } from "./TouchOrbit";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const app = appObjects.getOrCreate("app");
    new MockTouchInputEntity(app);
    const appObj = appObjects.getOrCreate("AO");
    const mockOrbit = new MockOrbitSceneRootUC(appObj);
    const orbitController = new TouchOrbit(appObj);
    const toolRepo = makeToolRepo(appObjects.getOrCreate("ToolRepo"));
    const orbitTool = toolRepo.makeTool("ORBIT");
    orbitTool.state = ToolState.ACTIVE;
    return { orbitController, mockOrbit, orbitTool };
}
describe("Touch Orbit Controller", () => {
    it("Initializes if allow return true", () => {
        const { orbitController, mockOrbit } = makeTestRig();
        orbitController.allow = () => {
            return true;
        };
        const input = mockTouchInputEvent([new Vector2(0, 0), new Vector2(79, 0)]);
        orbitController.onTouchStart(input);
        expect(mockOrbit.initializeOrbit).toHaveBeenCalledWith(new Vector2(39.5, 0));
    });
    it("Does not initialize if allow return false", () => {
        const { orbitController, mockOrbit } = makeTestRig();
        orbitController.allow = () => {
            return false;
        };
        const input = mockTouchInputEvent([new Vector2(0, 0), new Vector2(79, 0)]);
        orbitController.onTouchStart(input);
        expect(mockOrbit.initializeOrbit).not.toHaveBeenCalled();
    });
    it("Sends updates the Orbit if the fingers move after initialization", () => {
        const { orbitController, mockOrbit } = makeTestRig();
        const startInput = mockTouchInputEvent([
            new Vector2(0, 0),
            new Vector2(50, 0),
        ]);
        orbitController.onTouchStart(startInput);
        const moveInput = mockTouchInputEvent([
            new Vector2(0, 0),
            new Vector2(40, 0),
        ]);
        orbitController.onTouchMove(moveInput);
        expect(mockOrbit.doOrbit).toHaveBeenCalledWith(new Vector2(20, 0));
    });
    it("Sends updates to the Orbit if the one finger moves after initialization when the orbit tool is selected", () => {
        const { orbitController, mockOrbit } = makeTestRig();
        const startInput = mockTouchInputEvent([new Vector2(50, 0)]);
        orbitController.onTouchStart(startInput);
        const moveInput = mockTouchInputEvent([new Vector2(40, 0)]);
        orbitController.onTouchMove(moveInput);
        expect(mockOrbit.doOrbit).toHaveBeenCalledWith(new Vector2(40, 0));
    });
    it("Does not send Orbit if not initialized", () => {
        const { orbitController, mockOrbit } = makeTestRig();
        const moveInput = mockTouchInputEvent([
            new Vector2(-50, 0),
            new Vector2(100, 0),
        ]);
        orbitController.onTouchMove(moveInput);
        expect(mockOrbit.doOrbit).not.toHaveBeenCalled();
    });
    it("Finishes the orbit", () => {
        const { orbitController, mockOrbit } = makeTestRig();
        const startInput = mockTouchInputEvent([
            new Vector2(0, 0),
            new Vector2(75, 0),
        ]);
        orbitController.onTouchStart(startInput);
        orbitController.onTouchEnd();
        expect(mockOrbit.finishOrbit).toHaveBeenCalled();
    });
    it("Cancels orbit if the allow changes it's response", () => {
        const { orbitController, mockOrbit } = makeTestRig();
        const startInput = mockTouchInputEvent([new Vector2(50, 0)]);
        orbitController.onTouchStart(startInput);
        const moveInput = mockTouchInputEvent([new Vector2(40, 0)]);
        orbitController.onTouchMove(moveInput);
        expect(mockOrbit.doOrbit).toHaveBeenCalledWith(new Vector2(40, 0));
        mockOrbit.doOrbit.mockClear();
        orbitController.allow = () => {
            return false;
        };
        const startInput2 = mockTouchInputEvent([
            new Vector2(0, 0),
            new Vector2(85, 0),
        ]);
        orbitController.onTouchStart(startInput2);
        const moveInput2 = mockTouchInputEvent([new Vector2(-50, 0)]);
        orbitController.onTouchMove(moveInput2);
        expect(mockOrbit.doOrbit).not.toHaveBeenCalled();
        expect(mockOrbit.cancelOrbit).toHaveBeenCalled();
    });
    it("Allows orbit if the touch count is 2 and the touches are less than 80px apart", () => {
        const { orbitController } = makeTestRig();
        const evt = mockTouchInputEvent([new Vector2(0, 0), new Vector2(79, 0)]);
        expect(orbitController.allow(evt)).toEqual(true);
    });
    it("Does not allow orbit if the touch count is 2 and the touches are greater than 80px apart", () => {
        const { orbitController } = makeTestRig();
        const evt = mockTouchInputEvent([new Vector2(0, 0), new Vector2(81, 0)]);
        expect(orbitController.allow(evt)).toEqual(false);
    });
    it("Allows Orbit if the touch count is 1 and the tools is Orbit", () => {
        const { orbitController, orbitTool } = makeTestRig();
        orbitTool.state = ToolState.ACTIVE;
        const evt = mockTouchInputEvent([new Vector2(0, 0)]);
        expect(orbitController.allow(evt)).toEqual(true);
    });
    it("Does not allow if the touch count is 1 but orbit is not active", () => {
        const { orbitController, orbitTool } = makeTestRig();
        orbitTool.state = ToolState.ENABLED;
        const evt = mockTouchInputEvent([new Vector2(0, 0)]);
        expect(orbitController.allow(evt)).toEqual(false);
    });
});
//# sourceMappingURL=TouchOrbit.test.js.map