"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const Dispatcher_1 = require("../../Dispatcher");
const Entities_1 = require("../Entities");
const PinchSceneRoot_1 = require("./PinchSceneRoot");
class MockPick extends Babylon_1.ScenePixelPickUC {
    constructor() {
        super(...arguments);
        this.scenePixelPick = jest.fn();
    }
}
class MockPixelToRay extends Babylon_1.PixelToSceneRayUC {
    constructor() {
        super(...arguments);
        this.pixelToSceneRay = (pixelPos) => {
            return core_2.ParametricLine.FromPointDirection(new core_2.Vector3(pixelPos.x, pixelPos.y, -10), core_2.Vector3.Forward());
        };
    }
}
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const sceneRootAO = appObjects.getOrCreate("sceneRoot");
    const sceneRoot = (0, Entities_1.makeSceneRootEntity)(sceneRootAO);
    const pinchUC = (0, PinchSceneRoot_1.makePinchSceneRootUC)(sceneRootAO);
    const appAO = appObjects.getOrCreate("appAO");
    const mockPick = new MockPick(appAO, Babylon_1.ScenePixelPickUC.type);
    const mockRay = new MockPixelToRay(appAO, Babylon_1.PixelToSceneRayUC.type);
    const mockDispatch = (0, Dispatcher_1.makeMockDispatchStateChange)(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatch.doDispatch = doDispatchMock;
    return {
        appObjects,
        pinchUC,
        sceneRoot,
        mockPick,
        mockDispatch,
        doDispatchMock,
        mockRay,
    };
}
describe("Pinch Scene Root Use Case", () => {
    it("Move with no scaling adjusts the root as expected", () => {
        const { pinchUC, sceneRoot } = makeTestRig();
        sceneRoot.position = new core_2.Vector3(1, 2, 3);
        const touch1 = new core_2.Vector2(5, 8);
        const touch2 = new core_2.Vector2(5, 0);
        pinchUC.initializePinch(touch1, touch2);
        const touch3 = new core_2.Vector2(0, 8);
        const touch4 = new core_2.Vector2(0, 0);
        pinchUC.doPinch(touch3, touch4);
        const expected = new core_2.Vector3(-4, 2, 3); // -5 in the x
        const wPos = sceneRoot.position;
        expect(core_2.Vector3.Close(wPos, expected)).toEqual(true);
    });
    it("Plane pick is relative", () => {
        const { pinchUC, sceneRoot, mockPick } = makeTestRig();
        mockPick.scenePixelPick.mockReturnValue(new core_2.Vector3(10, 20, 60));
        sceneRoot.position = new core_2.Vector3(1, 2, 3);
        const touch1 = new core_2.Vector2(5, 8);
        const touch2 = new core_2.Vector2(5, 0);
        pinchUC.initializePinch(touch1, touch2);
        const touch3 = new core_2.Vector2(0, 8);
        const touch4 = new core_2.Vector2(0, 0);
        pinchUC.doPinch(touch3, touch4);
        const expected = new core_2.Vector3(-4, 2, 3); // -5 in the x
        const wPos = sceneRoot.position;
        expect(core_2.Vector3.Close(wPos, expected)).toEqual(true);
    });
    it("Scales smaller with a pinch that moves closer", () => {
        const { pinchUC, sceneRoot } = makeTestRig();
        sceneRoot.position = new core_2.Vector3(0, 0, 0);
        sceneRoot.scale = 1;
        const touch1 = new core_2.Vector2(0, 8);
        const touch2 = new core_2.Vector2(0, -8);
        pinchUC.initializePinch(touch1, touch2);
        const touch3 = new core_2.Vector2(0, 4);
        const touch4 = new core_2.Vector2(0, -4);
        pinchUC.doPinch(touch3, touch4);
        const expectedScaleDelta = pinchUC.sensitivity * 8;
        expect(sceneRoot.scale).toEqual(1 - expectedScaleDelta);
    });
    it("Scales larger with a pinch that moves further", () => {
        const { pinchUC, sceneRoot } = makeTestRig();
        sceneRoot.position = new core_2.Vector3(0, 0, 0);
        sceneRoot.scale = 1;
        const touch1 = new core_2.Vector2(0, 8);
        const touch2 = new core_2.Vector2(0, -8);
        pinchUC.initializePinch(touch1, touch2);
        const touch3 = new core_2.Vector2(0, 12);
        const touch4 = new core_2.Vector2(0, -12);
        pinchUC.doPinch(touch3, touch4);
        const expectedScaleDelta = pinchUC.sensitivity * 8;
        expect(sceneRoot.scale).toEqual(1 + expectedScaleDelta);
    });
    it("Dispatches a state change if the position has changed since init", () => {
        const { sceneRoot, pinchUC, doDispatchMock, mockPick } = makeTestRig();
        sceneRoot.scale = 5;
        const touch1 = new core_2.Vector2(0, 8);
        const touch2 = new core_2.Vector2(0, -8);
        pinchUC.initializePinch(touch1, touch2);
        sceneRoot.scale = 6;
        pinchUC.finishPinch();
        expect(doDispatchMock).toHaveBeenCalled();
    });
    it("Does not dispatch a state change if the scale has not changed", () => {
        const { sceneRoot, pinchUC, doDispatchMock } = makeTestRig();
        sceneRoot.scale = 5;
        const touch1 = new core_2.Vector2(0, 8);
        const touch2 = new core_2.Vector2(0, -8);
        pinchUC.initializePinch(touch1, touch2);
        pinchUC.finishPinch();
        expect(doDispatchMock).not.toHaveBeenCalled();
    });
    it("Gets the UC", () => {
        const { pinchUC, appObjects } = makeTestRig();
        expect(PinchSceneRoot_1.PinchSceneRootUC.get(appObjects)).toEqual(pinchUC);
    });
});
//# sourceMappingURL=PinchSceneRoot.test.js.map