"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Mocks_1 = require("../../Babylon/Mocks");
const MockDispatchStateChange_1 = require("../../Dispatcher/Mocks/MockDispatchStateChange");
const Transform3D_1 = require("../../Transform3D/Entities/Transform3D");
const UndoRedoEntity_1 = require("../../UndoRedo/Entities/UndoRedoEntity");
const Stickable_1 = require("../Entities/Stickable");
const PanTransformWithStickUC_1 = require("./PanTransformWithStickUC");
const TryPickStickTarget_1 = require("./TryPickStickTarget");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const ao = appObjects.getOrCreate("transform");
    const transform = (0, Transform3D_1.makeTransform3D)(ao);
    const panUC = (0, PanTransformWithStickUC_1.makePanTransformWithStickUC)(ao);
    const stickable = (0, Stickable_1.makeStickableEntity)(ao);
    const tryPickTarget = (0, TryPickStickTarget_1.makeTryPickStickTargetUC)(ao);
    tryPickTarget.tryPick = () => {
        return true;
    };
    const mockPick = (0, Mocks_1.makeMockSceneRayPickUC)(appObjects);
    const mockDispatch = (0, MockDispatchStateChange_1.makeMockDispatchStateChange)(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatch.doDispatch = doDispatchMock;
    const mockPixelRay = (0, Mocks_1.makeMockPixelToSceneRayUC)(appObjects);
    mockPixelRay.pixelToSceneRay = mockPicker;
    const undo = (0, UndoRedoEntity_1.makeUndoRedoEntity)(appObjects.getOrCreate("Undo"));
    return {
        panUC,
        transform,
        mockPick,
        doDispatchMock,
        appObjects,
        mockPixelRay,
        undo,
        stickable,
        tryPickTarget,
    };
}
function mockPicker(pixel) {
    return core_2.ParametricLine.FromPointDirection(new core_2.Vector3(pixel.x, pixel.y, 3), core_2.Vector3.Forward());
}
describe("Pan Transform With Stick UC", () => {
    it("Moves the transform", () => {
        const { transform, panUC } = makeTestRig();
        panUC.initializePixel(core_2.Vector2.Zero());
        panUC.updatePixel(new core_2.Vector2(1, 2));
        expect(transform.worldPosition).toEqual(new core_2.Vector3(1, 2, 0));
    });
    it("Moves the transform relative to the initial position", () => {
        const { transform, panUC } = makeTestRig();
        transform.worldPosition = new core_2.Vector3(10, 10, 10);
        panUC.initializePixel(core_2.Vector2.Zero());
        panUC.updatePixel(new core_2.Vector2(1, 2));
        expect(transform.worldPosition).toEqual(new core_2.Vector3(11, 12, 10));
    });
    it("Dispatches a state change if the position has changed since init", () => {
        const { transform, panUC, doDispatchMock } = makeTestRig();
        transform.worldPosition = new core_2.Vector3(1, 2, 3);
        panUC.initializePixel(core_2.Vector2.Zero());
        transform.worldPosition = new core_2.Vector3(4, 5, 6);
        panUC.finish();
        expect(doDispatchMock).toHaveBeenCalled();
    });
    it("Does not dispatch a state change if the position has not changed", () => {
        const { transform, panUC, doDispatchMock } = makeTestRig();
        transform.worldPosition = new core_2.Vector3(1, 2, 3);
        panUC.initializePixel(core_2.Vector2.Zero());
        panUC.finish();
        expect(doDispatchMock).not.toHaveBeenCalled();
    });
    it("Does not dispatch state change if dispatchStateChangeOnComplete is false", () => {
        const { transform, panUC, doDispatchMock } = makeTestRig();
        panUC.dispatchStateChangeOnComplete = false;
        transform.worldPosition = new core_2.Vector3(1, 2, 3);
        panUC.initializePixel(core_2.Vector2.Zero());
        transform.worldPosition = new core_2.Vector3(4, 5, 6);
        panUC.finish();
        expect(doDispatchMock).not.toHaveBeenCalled();
    });
    it("Doesn't not move the transform after cancel", () => {
        const { transform, panUC } = makeTestRig();
        panUC.initializePixel(core_2.Vector2.Zero());
        panUC.cancelPan();
        panUC.updatePixel(new core_2.Vector2(1, 2));
        expect(transform.worldPosition).toEqual(core_2.Vector3.Zero());
    });
    it("Limits the minimum X", () => {
        const { transform, panUC } = makeTestRig();
        panUC.minX = 0;
        panUC.initializePixel(core_2.Vector2.Zero());
        panUC.updatePixel(new core_2.Vector2(-1, 2));
        expect(transform.worldPosition).toEqual(new core_2.Vector3(0, 2, 0));
    });
    it("Limits the maximum X", () => {
        const { transform, panUC } = makeTestRig();
        panUC.maxX = 0;
        panUC.initializePixel(core_2.Vector2.Zero());
        panUC.updatePixel(new core_2.Vector2(1, 2));
        expect(transform.worldPosition).toEqual(new core_2.Vector3(0, 2, 0));
    });
    it("Limits the minimum Y", () => {
        const { transform, panUC } = makeTestRig();
        panUC.minY = 0;
        panUC.initializePixel(core_2.Vector2.Zero());
        panUC.updatePixel(new core_2.Vector2(-1, -2));
        expect(transform.worldPosition).toEqual(new core_2.Vector3(-1, 0, 0));
    });
    it("Limits the maximum Y", () => {
        const { transform, panUC } = makeTestRig();
        panUC.maxY = 0;
        panUC.initializePixel(core_2.Vector2.Zero());
        panUC.updatePixel(new core_2.Vector2(1, 2));
        expect(transform.worldPosition).toEqual(new core_2.Vector3(1, 0, 0));
    });
    it("Does not update if disabled", () => {
        const { transform, panUC } = makeTestRig();
        transform.worldPosition = core_2.Vector3.Zero();
        panUC.enabled = false;
        panUC.initializePixel(core_2.Vector2.Zero());
        panUC.updatePixel(new core_2.Vector2(10, 20));
        expect(transform.worldPosition).toEqual(core_2.Vector3.Zero());
    });
    it("Submits an undo on stop", () => {
        const { panUC, undo } = makeTestRig();
        panUC.initializePixel(core_2.Vector2.Zero());
        panUC.updatePixel(new core_2.Vector2(10, 20));
        panUC.finish();
        expect(undo.hasUndo).toEqual(true);
    });
    it("Does not submit an undo on stop if flag is set to false", () => {
        const { panUC, undo } = makeTestRig();
        panUC.submitUndoOnComplete = false;
        panUC.initializePixel(core_2.Vector2.Zero());
        panUC.updatePixel(new core_2.Vector2(10, 20));
        panUC.finish();
        expect(undo.hasUndo).toEqual(false);
    });
    it("Does not submit an undo on stop if there was no movement", () => {
        const { panUC, undo } = makeTestRig();
        panUC.initializePixel(core_2.Vector2.Zero());
        panUC.finish();
        expect(undo.hasUndo).toEqual(false);
    });
    it("Wires up the dispatch for undo and redo", () => {
        const { panUC, undo, doDispatchMock } = makeTestRig();
        panUC.initializePixel(core_2.Vector2.Zero());
        panUC.updatePixel(new core_2.Vector2(10, 20));
        panUC.finish();
        doDispatchMock.mockClear();
        undo.doUndo();
        expect(doDispatchMock).toHaveBeenCalledTimes(1);
        undo.doRedo();
        expect(doDispatchMock).toHaveBeenCalledTimes(2);
    });
    it("Undoes and redoes the move", () => {
        const { panUC, undo, transform } = makeTestRig();
        const initialPos = transform.localPosition;
        panUC.initializePixel(core_2.Vector2.Zero());
        panUC.updatePixel(new core_2.Vector2(10, 20));
        panUC.finish();
        const fianlPos = transform.localPosition;
        undo.doUndo();
        expect(transform.localPosition).toEqual(initialPos);
        undo.doRedo();
        expect(transform.localPosition).toEqual(fianlPos);
    });
});
//# sourceMappingURL=PanTransformWithStickUC.test.js.map