"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const _1 = require(".");
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const StateMachineEntity_1 = require("../../StateMachine/Entities/StateMachineEntity");
const StoredView_1 = require("../Entities/StoredView");
const SerializeStoredView_1 = require("./SerializeStoredView");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const appObj = appObjects.getOrCreate(StoredView_1.StoredView.type);
    const storedView = (0, StoredView_1.makeStoredView)(appObj);
    const stateMachine = (0, StateMachineEntity_1.makeStateMachineEntity)(appObj);
    const restoreStoredViewUC = (0, _1.makeRestoreStoredViewUC)(appObj);
    const mockRestoreStoredView = jest.fn();
    restoreStoredViewUC.restoreStoredView = mockRestoreStoredView;
    const mockRegister = jest.fn();
    stateMachine.registerSerializedSystem = mockRegister;
    const serializer = (0, SerializeStoredView_1.makeSerializeStoredView)(appObj);
    return {
        mockRegister,
        mockRestoreStoredView,
        storedView,
        stateMachine,
        serializer,
        appObj,
        appObjects,
    };
}
describe("Serializing the Scene Root", () => {
    it("Registers with the state machine on construction", () => {
        const { mockRegister } = makeTestRig();
        expect(mockRegister).toHaveBeenCalled();
    });
    it("Serializes the Scene Root as expected", () => {
        const { storedView, serializer } = makeTestRig();
        storedView.position = new core_2.Vector3(1, 2, 3);
        const rot = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(35));
        storedView.rotation = rot;
        storedView.scale = 5;
        const state = serializer.getSystemState();
        expect(state.position).toEqual(storedView.position.dto);
        expect(state.scale).toEqual(5);
        expect(state.rotation).toEqual(storedView.rotation.dto);
    });
    it("Applies V1 state", () => {
        const { storedView, serializer, stateMachine } = makeTestRig();
        const desriedPos = new core_2.Vector3(1, 2, 3);
        const desiredRot = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(35));
        const desiredScale = 5;
        const hasStoredView = true;
        const state = {
            position: desriedPos.dto,
            rotation: desiredRot.dto,
            scale: desiredScale,
            hasStoredView,
        };
        serializer.applySystemState(state, 1);
        expect(storedView.position).toEqual(desriedPos);
        expect(storedView.rotation).toEqual(desiredRot);
        expect(storedView.scale).toEqual(desiredScale);
        expect(storedView.hasStoredView).toEqual(hasStoredView);
    });
    it("Calls RestoreStoredViewUC if has stored view when applies V1 state", () => {
        const { serializer, mockRestoreStoredView } = makeTestRig();
        const desriedPos = new core_2.Vector3(1, 2, 3);
        const desiredRot = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(35));
        const desiredScale = 5;
        const hasStoredView = true;
        const state = {
            position: desriedPos.dto,
            rotation: desiredRot.dto,
            scale: desiredScale,
            hasStoredView,
        };
        serializer.applySystemState(state, 1);
        expect(mockRestoreStoredView).toHaveBeenCalled();
    });
    it("No calls RestoreStoredViewUC if doesn't have stored view when applies V1 state", () => {
        const { serializer, mockRestoreStoredView } = makeTestRig();
        const desriedPos = new core_2.Vector3(1, 2, 3);
        const desiredRot = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(35));
        const desiredScale = 5;
        const hasStoredView = false;
        const state = {
            position: desriedPos.dto,
            rotation: desiredRot.dto,
            scale: desiredScale,
            hasStoredView,
        };
        serializer.applySystemState(state, 1);
        expect(mockRestoreStoredView).not.toHaveBeenCalled();
    });
    it("Applies the default state", () => {
        const { storedView, serializer, stateMachine } = makeTestRig();
        storedView.position = new core_2.Vector3(1, 2, 3);
        storedView.rotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(35));
        storedView.scale = 5;
        serializer.applyDefaultState(2);
        expect(storedView.position).toEqual(core_2.Vector3.Zero());
        expect(storedView.rotation).toEqual(core_2.Quaternion.Identity());
        expect(storedView.scale).toEqual(1);
        expect(storedView.hasStoredView).toEqual(false);
    });
    it("Warns and applies default if the version is not supported", () => {
        const { storedView, serializer } = makeTestRig();
        storedView.position = new core_2.Vector3(1, 2, 3);
        storedView.rotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(35));
        storedView.scale = 5;
        serializer.applyDefaultState = jest.fn();
        console.warn = jest.fn();
        serializer.applySystemState({ foo: "bar" }, -1);
        expect(console.warn).toHaveBeenCalled();
        expect(serializer.applyDefaultState).toHaveBeenCalled();
    });
    it("Adds itself as a UC to the App Object", () => {
        const { appObj, serializer } = makeTestRig();
        expect(appObj.hasComponent(SerializeStoredView_1.SerializeStoredView.type)).toEqual(true);
        expect(appObj.getComponent(SerializeStoredView_1.SerializeStoredView.type)).toEqual(serializer);
    });
});
//# sourceMappingURL=SerializeStoredView.test.js.map