"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 Transform3D_1 = require("../../Transform3D/Entities/Transform3D");
const UndoRedoEntity_1 = require("../../UndoRedo/Entities/UndoRedoEntity");
const Stickable_1 = require("../Entities/Stickable");
const MoveTransformWithStickUC_1 = require("./MoveTransformWithStickUC");
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 moveUC = (0, MoveTransformWithStickUC_1.makeMoveTransformWithStickUC)(ao);
    const stickable = (0, Stickable_1.makeStickableEntity)(ao);
    const tryPickTarget = (0, TryPickStickTarget_1.makeTryPickStickTargetUC)(ao);
    tryPickTarget.tryPick = () => {
        return true;
    };
    const mockDispatch = (0, MockDispatchStateChange_1.makeMockDispatchStateChange)(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatch.doDispatch = doDispatchMock;
    const undo = (0, UndoRedoEntity_1.makeUndoRedoEntity)(appObjects.getOrCreate("Undo"));
    const helper = (0, Transform3D_1.makeTransform3D)(appObjects.getOrCreate("Helper"));
    return {
        moveUC,
        transform,
        doDispatchMock,
        appObjects,
        undo,
        stickable,
        tryPickTarget,
        helper,
    };
}
describe("Pan Transform With Stick UC", () => {
    it("Shows the targets on the first move if the pick ray does not hit the targets", () => {
        const { stickable, moveUC, helper, tryPickTarget } = makeTestRig();
        tryPickTarget.tryPick = () => {
            return false;
        };
        moveUC.initialize(helper);
        expect(stickable.showStickTarget).toEqual(false);
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.showStickTarget).toEqual(true);
        expect(stickable.highlightTarget).toEqual(false);
    });
    it("Hides the targets on the first if the pick ray hit the targets", () => {
        const { stickable, moveUC, helper, tryPickTarget } = makeTestRig();
        tryPickTarget.tryPick = () => {
            return true;
        };
        moveUC.initialize(helper);
        expect(stickable.showStickTarget).toEqual(false);
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.showStickTarget).toEqual(false);
        expect(stickable.highlightTarget).toEqual(false);
    });
    it("Shows the targets after moving so that there is no target pick on the first if the pick ray hit the targets", () => {
        const { stickable, moveUC, helper, tryPickTarget } = makeTestRig();
        tryPickTarget.tryPick = () => {
            return true;
        };
        moveUC.initialize(helper);
        expect(stickable.showStickTarget).toEqual(false);
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.showStickTarget).toEqual(false);
        tryPickTarget.tryPick = () => {
            return false;
        };
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.showStickTarget).toEqual(true);
    });
    it("Highlights the targets when the start to pick on them after a move", () => {
        const { stickable, moveUC, helper, tryPickTarget } = makeTestRig();
        tryPickTarget.tryPick = () => {
            return false;
        };
        moveUC.initialize(helper);
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.highlightTarget).toEqual(false);
        tryPickTarget.tryPick = () => {
            return true;
        };
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.highlightTarget).toEqual(true);
    });
    it("Hides the targets on finish", () => {
        const { stickable, moveUC, helper, tryPickTarget } = makeTestRig();
        tryPickTarget.tryPick = () => {
            return false;
        };
        moveUC.initialize(helper);
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.showStickTarget).toEqual(true);
        moveUC.finish();
        expect(stickable.showStickTarget).toEqual(false);
    });
    it("Hides the targets on cancel", () => {
        const { stickable, moveUC, helper, tryPickTarget } = makeTestRig();
        tryPickTarget.tryPick = () => {
            return false;
        };
        moveUC.initialize(helper);
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.showStickTarget).toEqual(true);
        moveUC.cancel();
        expect(stickable.showStickTarget).toEqual(false);
    });
    it("Triggers a stick on finish if picking on a target", () => {
        const { stickable, moveUC, helper, tryPickTarget } = makeTestRig();
        stickable.doStick = jest.fn();
        tryPickTarget.tryPick = () => {
            return false;
        };
        moveUC.initialize(helper);
        moveUC.update(helper, core_2.ParametricLine.Forward());
        tryPickTarget.tryPick = () => {
            return true;
        };
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.highlightTarget).toEqual(true);
        moveUC.finish();
        expect(stickable.doStick).toHaveBeenCalled();
    });
    it("Does not trigger a stick on finish if picking on a target", () => {
        const { stickable, moveUC, helper, tryPickTarget } = makeTestRig();
        stickable.doStick = jest.fn();
        tryPickTarget.tryPick = () => {
            return false;
        };
        moveUC.initialize(helper);
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.highlightTarget).toEqual(false);
        moveUC.finish();
        expect(stickable.doStick).not.toHaveBeenCalled();
    });
    it("Tells stick to dispatch a state change on complete if the flag is set", () => {
        const { stickable, moveUC, helper, tryPickTarget, doDispatchMock } = makeTestRig();
        moveUC.dispatchStateChangeOnComplete = true;
        stickable.doStick = jest.fn();
        tryPickTarget.tryPick = () => {
            return false;
        };
        moveUC.initialize(helper);
        moveUC.update(helper, core_2.ParametricLine.Forward());
        tryPickTarget.tryPick = () => {
            return true;
        };
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.highlightTarget).toEqual(true);
        moveUC.finish();
        expect(stickable.doStick).toHaveBeenCalledWith(doDispatchMock);
    });
    it("Does not tell stick to dispatch a state change on complete if the flag is set", () => {
        const { stickable, moveUC, helper, tryPickTarget } = makeTestRig();
        moveUC.dispatchStateChangeOnComplete = false;
        stickable.doStick = jest.fn();
        tryPickTarget.tryPick = () => {
            return false;
        };
        moveUC.initialize(helper);
        moveUC.update(helper, core_2.ParametricLine.Forward());
        tryPickTarget.tryPick = () => {
            return true;
        };
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.highlightTarget).toEqual(true);
        moveUC.finish();
        expect(stickable.doStick).toHaveBeenCalledWith();
    });
    it("Submits an undo when stuck if the flag is set", () => {
        const { stickable, moveUC, helper, tryPickTarget, undo } = makeTestRig();
        moveUC.submitUndoOnComplete = true;
        stickable.doStick = jest.fn();
        tryPickTarget.tryPick = () => {
            return false;
        };
        moveUC.initialize(helper);
        moveUC.update(helper, core_2.ParametricLine.Forward());
        tryPickTarget.tryPick = () => {
            return true;
        };
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.highlightTarget).toEqual(true);
        moveUC.finish();
        expect(undo.hasUndo).toEqual(true);
    });
    it("Does not submit an undo when stuck if the flag is set", () => {
        const { stickable, moveUC, helper, tryPickTarget, undo } = makeTestRig();
        moveUC.submitUndoOnComplete = false;
        stickable.doStick = jest.fn();
        tryPickTarget.tryPick = () => {
            return false;
        };
        moveUC.initialize(helper);
        moveUC.update(helper, core_2.ParametricLine.Forward());
        tryPickTarget.tryPick = () => {
            return true;
        };
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.highlightTarget).toEqual(true);
        moveUC.finish();
        expect(undo.hasUndo).toEqual(false);
    });
    it("Wires up the dispatch for undo and redo", () => {
        const { stickable, moveUC, helper, tryPickTarget, undo, doDispatchMock } = makeTestRig();
        stickable.doStick = jest.fn();
        tryPickTarget.tryPick = () => {
            return false;
        };
        moveUC.initialize(helper);
        moveUC.update(helper, core_2.ParametricLine.Forward());
        tryPickTarget.tryPick = () => {
            return true;
        };
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.highlightTarget).toEqual(true);
        moveUC.finish();
        doDispatchMock.mockClear();
        undo.doUndo();
        expect(doDispatchMock).toHaveBeenCalledTimes(1);
        undo.doRedo();
        expect(doDispatchMock).toHaveBeenCalledTimes(2);
    });
    it("Undoes and redoes the move", () => {
        const { stickable, moveUC, helper, tryPickTarget, undo, transform } = makeTestRig();
        stickable.doStick = jest.fn();
        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;
        stickable.stickPosition = core_2.Vector3.Zero();
        stickable.stickRotation = core_2.Quaternion.Identity();
        tryPickTarget.tryPick = () => {
            return false;
        };
        moveUC.initialize(helper);
        moveUC.update(helper, core_2.ParametricLine.Forward());
        tryPickTarget.tryPick = () => {
            return true;
        };
        moveUC.update(helper, core_2.ParametricLine.Forward());
        expect(stickable.highlightTarget).toEqual(true);
        moveUC.finish();
        undo.doUndo();
        expect(transform.localPosition).toEqual(initialPos);
        expect(core_2.Quaternion.Close(transform.localRotation, initialRot)).toEqual(true);
        undo.doRedo();
        expect(transform.localPosition).toEqual(core_2.Vector3.Zero());
        expect(core_2.Quaternion.Close(transform.localRotation, core_2.Quaternion.Identity())).toEqual(true);
    });
});
//# sourceMappingURL=MoveTransformWithStickUC.test.js.map