"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const Entities_1 = require("../Entities");
const SerializeHideableObjects_1 = require("./SerializeHideableObjects");
const StateMachine_1 = require("../../StateMachine");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const ao = appObjects.getOrCreate("ao");
    const stateMachine = new StateMachine_1.MockStateMachine(ao);
    const obj1 = appObjects.getOrCreate("id1");
    const obj2 = appObjects.getOrCreate("id2");
    Entities_1.Hideable.addIfMissing(obj1);
    Entities_1.Hideable.addIfMissing(obj2);
    const serializer = (0, SerializeHideableObjects_1.makeSerializeHideableObjects)(ao);
    return {
        appObjects,
        serializer,
        obj1,
        obj2,
        stateMachine,
    };
}
describe("Serialize Dissectable Object", () => {
    it("Registers with the Serialized System on construction", () => {
        const { serializer, stateMachine } = makeTestRig();
        expect(stateMachine.registerSerializedSystem).toHaveBeenCalledWith(serializer);
    });
    it("Gets the initial state with no hidden objects", () => {
        const { serializer } = makeTestRig();
        const state = serializer.getSystemState();
        expect(state.hiddenObjects).toHaveLength(0);
    });
    it("Gets object state with hidden objects", () => {
        const { appObjects, serializer, obj1, obj2 } = makeTestRig();
        const hideable1 = Entities_1.Hideable.get(obj1.id, appObjects);
        hideable1.isHidden = true;
        const hideable2 = Entities_1.Hideable.get(obj2.id, appObjects);
        hideable2.isHidden = true;
        const state = serializer.getSystemState();
        expect(state.hiddenObjects).toHaveLength(2);
        expect(state.hiddenObjects[0]).toEqual(obj1.id);
        expect(state.hiddenObjects[1]).toEqual(obj2.id);
    });
    it("Sets all active objects not hidden when applyDefaultState is called", () => {
        const { appObjects, serializer, obj1, obj2 } = makeTestRig();
        const hideable1 = Entities_1.Hideable.get(obj1.id, appObjects);
        hideable1.isHidden = true;
        const hideable2 = Entities_1.Hideable.get(obj2.id, appObjects);
        hideable2.isHidden = true;
        serializer.applyDefaultState(1);
        expect(hideable1?.isHidden).toEqual(false);
        expect(hideable2?.isHidden).toEqual(false);
    });
    it("Calls the default state when an unsupported version is passed in", () => {
        const { serializer } = makeTestRig();
        serializer.applyDefaultState = jest.fn();
        const state = { foo: "bar" };
        serializer.applySystemState(state, -1, 2);
        expect(serializer.applyDefaultState).toHaveBeenCalled();
    });
    it("Calls the default state if the state cannot be desrialized", () => {
        const { serializer } = makeTestRig();
        serializer.applyDefaultState = jest.fn();
        const state = { foo: "bar" };
        serializer.applySystemState(state, 1, 2);
        expect(serializer.applyDefaultState).toHaveBeenCalled();
    });
    it("Sets hidden objects when applying the state", () => {
        const { appObjects, serializer, obj1, obj2 } = makeTestRig();
        const hideable1 = Entities_1.Hideable.get(obj1.id, appObjects);
        hideable1.isHidden = false;
        const hideable2 = Entities_1.Hideable.get(obj2.id, appObjects);
        hideable2.isHidden = false;
        const state = {
            hiddenObjects: ["id1", "id2"],
        };
        serializer.applySystemState(state, 1, 2);
        expect(hideable1?.isHidden).toEqual(true);
        expect(hideable2?.isHidden).toEqual(true);
    });
    it("Adds a hideable component to any objects in the hidden objects list that are missing one", () => {
        const { appObjects, serializer } = makeTestRig();
        appObjects.getOrCreate("id3");
        const state = {
            hiddenObjects: ["id1", "id3"],
        };
        serializer.applySystemState(state, 1, 2);
        const hideable3 = Entities_1.Hideable.get("id3", appObjects);
        expect(hideable3).toBeDefined();
        expect(hideable3.isHidden).toEqual(true);
    });
    it("Does not serialize hidden objects that have ignoreSerialization set to true", () => {
        const { appObjects, serializer, obj1, obj2 } = makeTestRig();
        const hideable1 = Entities_1.Hideable.get(obj1.id, appObjects);
        hideable1.isHidden = true;
        hideable1.ignoreSerialization = true;
        const state = serializer.getSystemState();
        expect(state.hiddenObjects).toHaveLength(0);
    });
});
//# sourceMappingURL=SerializeHideableObjects.test.js.map