"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const StateMachine_1 = require("../../StateMachine");
const OverlayColor_1 = require("../Entities/OverlayColor");
const SerializeOverlayColoredObjects_1 = require("./SerializeOverlayColoredObjects");
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");
    OverlayColor_1.OverlayColor.addIfMissing(obj1);
    OverlayColor_1.OverlayColor.addIfMissing(obj2);
    const serializer = (0, SerializeOverlayColoredObjects_1.makeSerializeOverlayColoredObjects)(ao);
    return {
        appObjects,
        serializer,
        obj1,
        obj2,
        stateMachine,
    };
}
describe("Serialize Overlay Colored 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.overlayColoredObjects).toHaveLength(0);
    });
    it("Gets object state with hidden objects", () => {
        const { serializer, obj1, obj2 } = makeTestRig();
        const overlayColor1 = OverlayColor_1.OverlayColor.get(obj1);
        overlayColor1.overlayColor = OverlayColor_1.OverlayColorEnums.BLACK;
        const overlayColor2 = OverlayColor_1.OverlayColor.get(obj2);
        overlayColor2.overlayColor = OverlayColor_1.OverlayColorEnums.RED;
        const state = serializer.getSystemState();
        expect(state.overlayColoredObjects).toHaveLength(2);
        expect(state.overlayColoredObjects[0].id).toEqual(obj1.id);
        expect(state.overlayColoredObjects[1].id).toEqual(obj2.id);
    });
    it("Sets all active objects not hidden when applyDefaultState is called", () => {
        const { serializer, obj1, obj2 } = makeTestRig();
        const overlayColor1 = OverlayColor_1.OverlayColor.get(obj1);
        overlayColor1.overlayColor = OverlayColor_1.OverlayColorEnums.BLACK;
        const overlayColor2 = OverlayColor_1.OverlayColor.get(obj2);
        overlayColor2.overlayColor = OverlayColor_1.OverlayColorEnums.RED;
        serializer.applyDefaultState(1);
        expect(overlayColor1?.overlayColor).toEqual(OverlayColor_1.OverlayColorEnums.NONE);
        expect(overlayColor2?.overlayColor).toEqual(OverlayColor_1.OverlayColorEnums.NONE);
    });
    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 deserialized", () => {
        const { serializer } = makeTestRig();
        serializer.applyDefaultState = jest.fn();
        const state = { foo: "bar" };
        serializer.applySystemState(state, 1, 2);
        expect(serializer.applyDefaultState).toHaveBeenCalled();
    });
    it("Sets overlay colored objects when applying the state", () => {
        const { serializer, obj1, obj2 } = makeTestRig();
        const overlayColor1 = OverlayColor_1.OverlayColor.get(obj1);
        overlayColor1.overlayColor = OverlayColor_1.OverlayColorEnums.NONE;
        const overlayColor2 = OverlayColor_1.OverlayColor.get(obj2);
        overlayColor2.overlayColor = OverlayColor_1.OverlayColorEnums.NONE;
        const state = {
            overlayColoredObjects: [
                {
                    id: "id1",
                    overlayColor: OverlayColor_1.OverlayColorEnums.BLACK,
                },
                {
                    id: "id2",
                    overlayColor: OverlayColor_1.OverlayColorEnums.RED,
                },
            ],
        };
        serializer.applySystemState(state, 1, 2);
        expect(overlayColor1?.overlayColor).toEqual(OverlayColor_1.OverlayColorEnums.BLACK);
        expect(overlayColor2?.overlayColor).toEqual(OverlayColor_1.OverlayColorEnums.RED);
    });
    it("Adds a overlay colored component to any objects in the overlay colored objects list that are missing one", () => {
        const { appObjects, serializer } = makeTestRig();
        const obj3 = appObjects.getOrCreate("id3");
        const state = {
            overlayColoredObjects: [
                {
                    id: "id1",
                    overlayColor: OverlayColor_1.OverlayColorEnums.BLACK,
                },
                {
                    id: "id3",
                    overlayColor: OverlayColor_1.OverlayColorEnums.GREEN,
                },
            ],
        };
        serializer.applySystemState(state, 1, 2);
        const overlayColor3 = OverlayColor_1.OverlayColor.get(obj3);
        expect(overlayColor3).toBeDefined();
        expect(overlayColor3.overlayColor).toEqual(OverlayColor_1.OverlayColorEnums.GREEN);
    });
    it("Creates the app object and overlay color if it doesn't exist", () => {
        const { appObjects, serializer } = makeTestRig();
        const state = {
            overlayColoredObjects: [
                {
                    id: "id1",
                    overlayColor: OverlayColor_1.OverlayColorEnums.BLACK,
                },
                {
                    id: "id3",
                    overlayColor: OverlayColor_1.OverlayColorEnums.GREEN,
                },
            ],
        };
        serializer.applySystemState(state, 1, 2);
        const overlayColor3 = OverlayColor_1.OverlayColor.getByID("id3", appObjects);
        expect(overlayColor3).toBeDefined();
        expect(overlayColor3?.overlayColor).toEqual(OverlayColor_1.OverlayColorEnums.GREEN);
    });
});
//# sourceMappingURL=SerializeOverlayColoredObjects.test.js.map