"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const MockDispatchStateChange_1 = require("../../Dispatcher/Mocks/MockDispatchStateChange");
const UndoRedo_1 = require("../../UndoRedo");
const Entities_1 = require("../Entities");
const MoveTransformUC_1 = require("./MoveTransformUC");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const appObj = (0, core_1.makeAppObject)("appObj", appObjects);
    const transform = (0, Entities_1.makeTransform3D)(appObj);
    const moveUC = (0, MoveTransformUC_1.makeMoveTransform)(appObj);
    const helper = (0, Entities_1.makeTransform3D)(appObjects.getOrCreate("Helper"));
    const mockDispatch = (0, MockDispatchStateChange_1.makeMockDispatchStateChange)(appObjects);
    const undo = (0, UndoRedo_1.makeUndoRedoEntity)(appObjects.getOrCreate("Undo"));
    return { moveUC, appObj, appObjects, mockDispatch, undo, helper, transform };
}
describe("Move Transform UC", () => {
    it("Gets the UC by ID", () => {
        const { moveUC, appObjects } = makeTestRig();
        expect(MoveTransformUC_1.MoveTransformUC.get("appObj", appObjects)).toEqual(moveUC);
    });
    it("Sets the helper to the world position and rotation of the local transform", () => {
        const { moveUC, transform, helper } = makeTestRig();
        transform.worldPosition = new core_2.Vector3(1, 2, 3);
        transform.worldRotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(7));
        helper.worldPosition = core_2.Vector3.Zero();
        helper.worldRotation = core_2.Quaternion.Identity();
        moveUC.initialize(helper);
        expect(helper.worldPosition).toEqual(transform.worldPosition);
        expect(helper.worldRotation).toEqual(transform.worldRotation);
    });
    it("Moves the transform to where the helper is", () => {
        const { moveUC, transform, helper } = makeTestRig();
        transform.worldPosition = new core_2.Vector3(1, 2, 3);
        transform.worldRotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(7));
        moveUC.initialize(helper);
        helper.worldPosition = core_2.Vector3.Zero();
        helper.worldRotation = core_2.Quaternion.Identity();
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(transform.worldPosition).toEqual(helper.worldPosition);
        expect(transform.worldRotation).toEqual(helper.worldRotation);
    });
    it("Does not move the transform if it was not initialized", () => {
        const { moveUC, transform, helper } = makeTestRig();
        transform.worldPosition = new core_2.Vector3(1, 2, 3);
        transform.worldRotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(7));
        helper.worldPosition = core_2.Vector3.Zero();
        helper.worldRotation = core_2.Quaternion.Identity();
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(transform.worldPosition).not.toEqual(helper.worldPosition);
        expect(transform.worldRotation).not.toEqual(helper.worldRotation);
    });
    it("Dispatches a state change on stop if moved", () => {
        const { moveUC, transform, helper, mockDispatch } = makeTestRig();
        transform.worldPosition = new core_2.Vector3(1, 2, 3);
        transform.worldRotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(7));
        moveUC.initialize(helper);
        helper.worldPosition = core_2.Vector3.Zero();
        helper.worldRotation = core_2.Quaternion.Identity();
        moveUC.update(helper, core_2.ParametricLine.Forward());
        moveUC.finish();
        expect(mockDispatch.doDispatch).toHaveBeenCalled();
    });
    it("Does not dispatch a state change on stop if not moved", () => {
        const { moveUC, transform, helper, mockDispatch } = makeTestRig();
        transform.worldPosition = new core_2.Vector3(1, 2, 3);
        transform.worldRotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(7));
        moveUC.initialize(helper);
        moveUC.finish();
        expect(mockDispatch.doDispatch).not.toHaveBeenCalled();
    });
    it("Does not dispatch a state change on stop if moved and told not to", () => {
        const { moveUC, transform, helper, mockDispatch } = makeTestRig();
        moveUC.dispatchStateChangeOnComplete = false;
        transform.worldPosition = new core_2.Vector3(1, 2, 3);
        transform.worldRotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(7));
        moveUC.initialize(helper);
        helper.worldPosition = core_2.Vector3.Zero();
        helper.worldRotation = core_2.Quaternion.Identity();
        moveUC.update(helper, core_2.ParametricLine.Forward());
        moveUC.finish();
        expect(mockDispatch.doDispatch).not.toHaveBeenCalled();
    });
    it("Submits an undo on stop", () => {
        const { moveUC, transform, helper, undo } = makeTestRig();
        transform.worldPosition = new core_2.Vector3(1, 2, 3);
        transform.worldRotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(7));
        moveUC.initialize(helper);
        helper.worldPosition = core_2.Vector3.Zero();
        helper.worldRotation = core_2.Quaternion.Identity();
        moveUC.update(helper, core_2.ParametricLine.Forward());
        moveUC.finish();
        expect(undo.hasUndo).toEqual(true);
    });
    it("Does not submit an undo on stop if flag is set to false", () => {
        const { moveUC, transform, helper, undo } = makeTestRig();
        moveUC.submitUndoOnComplete = false;
        transform.worldPosition = new core_2.Vector3(1, 2, 3);
        transform.worldRotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(7));
        moveUC.initialize(helper);
        helper.worldPosition = core_2.Vector3.Zero();
        helper.worldRotation = core_2.Quaternion.Identity();
        moveUC.update(helper, core_2.ParametricLine.Forward());
        moveUC.finish();
        expect(undo.hasUndo).toEqual(false);
    });
    it("Does not submit an undo on stop if there was no movement", () => {
        const { moveUC, transform, helper, undo } = makeTestRig();
        transform.worldPosition = new core_2.Vector3(1, 2, 3);
        transform.worldRotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(7));
        moveUC.initialize(helper);
        moveUC.finish();
        expect(undo.hasUndo).toEqual(false);
    });
    it("Wires up the dispatch for undo and redo", () => {
        const { transform, undo, mockDispatch, helper, moveUC } = makeTestRig();
        transform.worldPosition = new core_2.Vector3(1, 2, 3);
        transform.worldRotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(7));
        moveUC.initialize(helper);
        helper.worldPosition = core_2.Vector3.Zero();
        helper.worldRotation = core_2.Quaternion.Identity();
        moveUC.update(helper, core_2.ParametricLine.Forward());
        moveUC.finish();
        mockDispatch.doDispatch.mockClear();
        undo.doUndo();
        expect(mockDispatch.doDispatch).toHaveBeenCalledTimes(1);
        undo.doRedo();
        expect(mockDispatch.doDispatch).toHaveBeenCalledTimes(2);
    });
    it("Undoes and redoes the move", () => {
        const { transform, undo, helper, moveUC } = makeTestRig();
        const initialPos = new core_2.Vector3(1, 2, 3);
        const initialRot = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(7));
        transform.localPosition = initialPos;
        transform.localRotation = initialRot;
        moveUC.initialize(helper);
        const movedPos = core_2.Vector3.Zero();
        const movedRot = core_2.Quaternion.Identity();
        helper.localPosition = movedPos;
        helper.localRotation = movedRot;
        moveUC.update(helper, core_2.ParametricLine.Forward());
        moveUC.finish();
        expect(transform.localPosition).toEqual(movedPos);
        expect(core_2.Quaternion.Close(transform.localRotation, movedRot)).toEqual(true);
        undo.doUndo();
        expect(transform.localPosition).toEqual(initialPos);
        expect(core_2.Quaternion.Close(transform.localRotation, initialRot)).toEqual(true);
        undo.doRedo();
        expect(transform.localPosition).toEqual(movedPos);
        expect(core_2.Quaternion.Close(transform.localRotation, movedRot)).toEqual(true);
    });
});
//# sourceMappingURL=MoveTransformUC.test.js.map