"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 MockDispatchStateChange_1 = require("../../Dispatcher/Mocks/MockDispatchStateChange");
const Entities_1 = require("../Entities");
const PinchTransform_1 = require("./PinchTransform");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const ao = appObjects.getOrCreate("AO");
    const transform = (0, Entities_1.makeTransform3D)(ao);
    const pinchUC = (0, PinchTransform_1.makePinchTransformUC)(ao);
    const mockPick = (0, Babylon_1.makeMockScenePixelPickUC)(appObjects);
    const mockDispatch = (0, MockDispatchStateChange_1.makeMockDispatchStateChange)(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatch.doDispatch = doDispatchMock;
    const mockRay = (0, Babylon_1.makeMockPixelToSceneRayUC)(appObjects);
    mockRay.pixelToSceneRay = mockPicker;
    return {
        appObjects,
        pinchUC,
        transform,
        mockPick,
        doDispatchMock,
        mockRay,
    };
}
function mockPicker(pixel) {
    return core_2.ParametricLine.FromPointDirection(new core_2.Vector3(pixel.x, pixel.y, 3), core_2.Vector3.Forward());
}
describe("Pinch Scene Root Use Case", () => {
    it("Move with no scaling adjusts the root as expected", () => {
        const { pinchUC, transform } = makeTestRig();
        transform.worldPosition = 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 = transform.worldPosition;
        expect(core_2.Vector3.Close(wPos, expected)).toEqual(true);
    });
    it("Plane pick is relative", () => {
        const { pinchUC, transform, mockPick } = makeTestRig();
        mockPick.scenePixelPick.mockReturnValue(new core_2.Vector3(10, 20, 60));
        transform.worldPosition = 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 = transform.worldPosition;
        expect(core_2.Vector3.Close(wPos, expected)).toEqual(true);
    });
    it("Scales smaller with a pinch that moves closer", () => {
        const { pinchUC, transform } = makeTestRig();
        transform.worldPosition = new core_2.Vector3(0, 0, 0);
        transform.worldScale = core_2.Vector3.One();
        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(transform.worldScale.x).toEqual(1 - expectedScaleDelta);
    });
    it("Scales larger with a pinch that moves further", () => {
        const { pinchUC, transform } = makeTestRig();
        transform.worldPosition = new core_2.Vector3(0, 0, 0);
        transform.worldScale = core_2.Vector3.One();
        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(transform.worldScale.x).toEqual(1 + expectedScaleDelta);
    });
    it("Dispatches a state change if the position has changed since init", () => {
        const { transform, pinchUC, doDispatchMock } = makeTestRig();
        transform.worldScale = new core_2.Vector3(5, 5, 5);
        const touch1 = new core_2.Vector2(0, 8);
        const touch2 = new core_2.Vector2(0, -8);
        pinchUC.initializePinch(touch1, touch2);
        transform.worldScale = new core_2.Vector3(6, 6, 6);
        pinchUC.finishPinch();
        expect(doDispatchMock).toHaveBeenCalled();
    });
    it("Does not dispatch a state change if the scale has not changed", () => {
        const { transform, pinchUC, doDispatchMock } = makeTestRig();
        transform.worldScale = new core_2.Vector3(5, 5, 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("Does not update if not enabled", () => {
        const { pinchUC, transform } = makeTestRig();
        pinchUC.enabled = false;
        transform.worldPosition = core_2.Vector3.Zero();
        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);
        expect(transform.worldPosition).toEqual(core_2.Vector3.Zero());
    });
});
//# sourceMappingURL=PinchTransform.test.js.map