"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 Stickable_1 = require("../../Stick/Entities/Stickable");
const Transform3D_1 = require("../../Transform3D/Entities/Transform3D");
const UndoRedoEntity_1 = require("../../UndoRedo/Entities/UndoRedoEntity");
const Entities_1 = require("../Entities");
const StickAllChannelModelObjectsUC_1 = require("./StickAllChannelModelObjectsUC");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const mockDispatchStateChange = (0, MockDispatchStateChange_1.makeMockDispatchStateChange)(appObjects);
    const ao = appObjects.getOrCreate("ChannelModel");
    const channelModel = (0, Entities_1.makeChannelModelEntity)(ao);
    const uc = (0, StickAllChannelModelObjectsUC_1.makeStickAllChannelModelObjectsUC)(ao);
    const modelObject1 = channelModel.addModelObject("modelId1");
    const modelObject2 = channelModel.addModelObject("modelId2");
    const sm1Transform = Transform3D_1.Transform3D.addIfMissing(modelObject1.appObject);
    const sm2Transform = Transform3D_1.Transform3D.addIfMissing(modelObject2.appObject);
    sm1Transform.localPosition = new core_2.Vector3(1, 2, 3);
    sm2Transform.localPosition = new core_2.Vector3(4, 5, 6);
    const stickableObject1 = (0, Stickable_1.makeStickableEntity)(modelObject1.appObject);
    const stickableObject2 = (0, Stickable_1.makeStickableEntity)(modelObject2.appObject);
    const mockSm1Stick = jest.fn();
    const mockSm2Stick = jest.fn();
    stickableObject1.doStick = mockSm1Stick;
    stickableObject2.doStick = mockSm2Stick;
    const undo = (0, UndoRedoEntity_1.makeUndoRedoEntity)(appObjects.getOrCreate("Undo"));
    return {
        uc,
        stickableObject1,
        stickableObject2,
        mockDispatchStateChange,
        sm1Transform,
        sm2Transform,
        undo,
        mockSm1Stick,
        mockSm2Stick,
    };
}
describe("uc", () => {
    it("Sticks all channel model objects", () => {
        const { uc, mockSm1Stick, mockSm2Stick, mockDispatchStateChange } = makeTestRig();
        uc.stickAll();
        expect(mockSm1Stick).toHaveBeenCalledWith(mockDispatchStateChange.doDispatch);
        expect(mockSm2Stick).toHaveBeenCalledWith(mockDispatchStateChange.doDispatch);
    });
    it("Submits an undo action", () => {
        const { uc, undo } = makeTestRig();
        uc.stickAll();
        expect(undo.hasUndo).toEqual(true);
    });
    it("Doesn't submit undo action when hidden dissectable meshes are not found", () => {
        const { uc, sm1Transform, sm2Transform, undo } = makeTestRig();
        sm1Transform.localPosition = core_2.Vector3.Zero();
        sm2Transform.localPosition = core_2.Vector3.Zero();
        uc.stickAll();
        expect(undo.hasUndo).toEqual(false);
    });
    it("Wires up the dispatch for undo redo", () => {
        const { uc, undo, mockDispatchStateChange } = makeTestRig();
        uc.stickAll();
        mockDispatchStateChange.doDispatch.mockClear();
        undo.doUndo();
        expect(mockDispatchStateChange.doDispatch).toHaveBeenCalledTimes(1);
        undo.doRedo();
        expect(mockDispatchStateChange.doDispatch).toHaveBeenCalledTimes(2);
    });
    it("Undo redo move", () => {
        const { uc, sm1Transform, undo, stickableObject1 } = makeTestRig();
        sm1Transform.localPosition = new core_2.Vector3(1, 2, 3);
        stickableObject1.stickPosition = new core_2.Vector3(4, 5, 6);
        uc.stickAll();
        sm1Transform.localPosition = new core_2.Vector3(4, 5, 6); // Fake the stick since we hijacked the lerp
        undo.doUndo();
        expect(sm1Transform.localPosition).toEqual(new core_2.Vector3(1, 2, 3));
        undo.doRedo();
        expect(sm1Transform.localPosition).toEqual(new core_2.Vector3(4, 5, 6));
    });
    it("Undo redo scale", () => {
        const { uc, sm1Transform, undo, stickableObject1 } = makeTestRig();
        sm1Transform.localScale = new core_2.Vector3(5, 5, 5);
        stickableObject1.stickScale = 3;
        uc.stickAll();
        sm1Transform.localScale = new core_2.Vector3(3, 3, 3); // Fake the stick since we hijacked the lerp
        undo.doUndo();
        expect(sm1Transform.localScale).toEqual(new core_2.Vector3(5, 5, 5));
        undo.doRedo();
        expect(sm1Transform.localScale).toEqual(new core_2.Vector3(3, 3, 3));
    });
    it("Undo redo rotation", () => {
        const { uc, sm1Transform, undo, stickableObject1 } = makeTestRig();
        sm1Transform.localRotation = core_2.Quaternion.Identity();
        const stickQuat = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(3, 4, 5), core_2.Angle.FromDegrees(6));
        stickableObject1.stickRotation = stickQuat;
        uc.stickAll();
        sm1Transform.localRotation = stickQuat; // Fake the stick since we hijacked the lerp
        undo.doUndo();
        expect(sm1Transform.localRotation).toEqual(core_2.Quaternion.Identity());
        undo.doRedo();
        expect(core_2.Quaternion.Close(sm1Transform.localRotation, stickQuat)).toEqual(true);
    });
});
//# sourceMappingURL=StickAllChannelModelObjectsUC.test.js.map