"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 StickUC_1 = require("./StickUC");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const ao = appObjects.getOrCreate("AO");
    const transform = (0, Transform3D_1.makeTransform3D)(ao);
    const stickEntity = (0, Stickable_1.makeStickableEntity)(ao);
    const uc = (0, StickUC_1.makeStickUC)(ao);
    const stickSpy = jest.spyOn(stickEntity, "doStick");
    const mockDispatch = (0, MockDispatchStateChange_1.makeMockDispatchStateChange)(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatch.doDispatch = doDispatchMock;
    const undo = (0, UndoRedoEntity_1.makeUndoRedoEntity)(appObjects.getOrCreate("Undo"));
    return {
        stickEntity,
        uc,
        appObjects,
        transform,
        doDispatchMock,
        stickSpy,
        undo,
    };
}
describe("Stick UC", () => {
    it("Gets the UC", () => {
        const { appObjects, uc } = makeTestRig();
        expect(StickUC_1.StickUC.get("AO", appObjects)).toEqual(uc);
    });
    it("Triggers do stick if the transform is out of place", () => {
        const { stickSpy, uc, transform, doDispatchMock } = makeTestRig();
        transform.localPosition = new core_2.Vector3(1, 2, 3);
        uc.doStick();
        expect(stickSpy).toHaveBeenCalledWith(doDispatchMock);
    });
    it("Does not trigger the lerp if it is not stickable", () => {
        const { stickSpy, uc, stickEntity } = makeTestRig();
        expect(stickEntity.canBeStuck).toEqual(false);
        uc.doStick();
        expect(stickSpy).not.toHaveBeenCalled();
    });
    it("Does not submit an undo if it is not stickable", () => {
        const { uc, stickEntity, undo } = makeTestRig();
        expect(stickEntity.canBeStuck).toEqual(false);
        uc.doStick();
        expect(undo.hasUndo).toEqual(false);
    });
    it("Submit an undo", () => {
        const { uc, transform, undo } = makeTestRig();
        transform.localPosition = new core_2.Vector3(1, 2, 3);
        uc.doStick();
        expect(undo.hasUndo).toEqual(true);
    });
    it("Dispatches on undo and redo", () => {
        const { uc, transform, undo, doDispatchMock } = makeTestRig();
        transform.localPosition = new core_2.Vector3(1, 2, 3);
        uc.doStick();
        doDispatchMock.mockClear();
        undo.doUndo();
        expect(doDispatchMock).toHaveBeenCalledTimes(1);
        undo.doRedo();
        expect(doDispatchMock).toHaveBeenCalledTimes(2);
    });
    it("Undo redo move", () => {
        const { uc, transform, undo, stickEntity } = makeTestRig();
        transform.localPosition = new core_2.Vector3(1, 2, 3);
        stickEntity.stickPosition = new core_2.Vector3(4, 5, 6);
        uc.doStick();
        transform.localPosition = new core_2.Vector3(4, 5, 6); // Fake the stick since we hijacked the lerp
        undo.doUndo();
        expect(transform.localPosition).toEqual(new core_2.Vector3(1, 2, 3));
        undo.doRedo();
        expect(transform.localPosition).toEqual(new core_2.Vector3(4, 5, 6));
    });
    it("Undo redo scale", () => {
        const { uc, transform, undo, stickEntity } = makeTestRig();
        transform.localScale = new core_2.Vector3(5, 5, 5);
        stickEntity.stickScale = 3;
        uc.doStick();
        transform.localScale = new core_2.Vector3(3, 3, 3); // Fake the stick since we hijacked the lerp
        undo.doUndo();
        expect(transform.localScale).toEqual(new core_2.Vector3(5, 5, 5));
        undo.doRedo();
        expect(transform.localScale).toEqual(new core_2.Vector3(3, 3, 3));
    });
    it("Undo redo rotation", () => {
        const { uc, transform, undo, stickEntity } = makeTestRig();
        transform.localRotation = core_2.Quaternion.Identity();
        const stickQuat = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(3, 4, 5), core_2.Angle.FromDegrees(6));
        stickEntity.stickRotation = stickQuat;
        uc.doStick();
        transform.localRotation = stickQuat; // Fake the stick since we hijacked the lerp
        undo.doUndo();
        expect(transform.localRotation).toEqual(core_2.Quaternion.Identity());
        undo.doRedo();
        expect(core_2.Quaternion.Close(transform.localRotation, stickQuat)).toEqual(true);
    });
});
//# sourceMappingURL=StickUC.test.js.map