"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const StateMachine_1 = require("../../StateMachine");
const Entities_1 = require("../Entities");
const SerializeTransparentObjects_1 = require("./SerializeTransparentObjects");
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.Transparency.addIfMissing(obj1);
    Entities_1.Transparency.addIfMissing(obj2);
    const serializer = (0, SerializeTransparentObjects_1.makeSerializeTransparentObjects)(ao);
    return {
        appObjects,
        serializer,
        obj1,
        obj2,
        stateMachine,
    };
}
describe("Serialize Transparent 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 transparent objects", () => {
        const { serializer } = makeTestRig();
        const state = serializer.getSystemState();
        expect(state.transparentObjects).toHaveLength(0);
    });
    it("Gets object state with transparent objects", () => {
        const { appObjects, serializer, obj1, obj2 } = makeTestRig();
        const transparent1 = Entities_1.Transparency.get(obj1.id, appObjects);
        transparent1.isTransparent = true;
        const transparent2 = Entities_1.Transparency.get(obj2.id, appObjects);
        transparent2.isTransparent = true;
        const state = serializer.getSystemState();
        expect(state.transparentObjects).toHaveLength(2);
        expect(state.transparentObjects[0]).toEqual(obj1.id);
        expect(state.transparentObjects[1]).toEqual(obj2.id);
    });
    it("Sets all active objects not hidden when applyDefaultState is called", () => {
        const { appObjects, serializer, obj1, obj2 } = makeTestRig();
        const transparent1 = Entities_1.Transparency.get(obj1.id, appObjects);
        transparent1.isTransparent = true;
        const transparent2 = Entities_1.Transparency.get(obj2.id, appObjects);
        transparent2.isTransparent = true;
        serializer.applyDefaultState(1);
        expect(transparent1?.isTransparent).toEqual(false);
        expect(transparent2?.isTransparent).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 transparent objects when applying the state", () => {
        const { appObjects, serializer, obj1, obj2 } = makeTestRig();
        const transparent1 = Entities_1.Transparency.get(obj1.id, appObjects);
        transparent1.isTransparent = false;
        const transparent2 = Entities_1.Transparency.get(obj2.id, appObjects);
        transparent2.isTransparent = false;
        const state = {
            transparentObjects: ["id1", "id2"],
        };
        serializer.applySystemState(state, 1, 2);
        expect(transparent1?.isTransparent).toEqual(true);
        expect(transparent2?.isTransparent).toEqual(true);
    });
    it("Adds a transparent component to any objects in the transparent objects list that are missing one", () => {
        const { appObjects, serializer } = makeTestRig();
        appObjects.getOrCreate("id3");
        const state = {
            transparentObjects: ["id1", "id3"],
        };
        serializer.applySystemState(state, 1, 2);
        const hideable3 = Entities_1.Transparency.get("id3", appObjects);
        expect(hideable3).toBeDefined();
        expect(hideable3.isTransparent).toEqual(true);
    });
});
//# sourceMappingURL=SerializeTransparentObjects.test.js.map