import { makeAppObjectRepo } from "@vived/core";
import { MockStateMachine } from "../../StateMachine";
import { LabelEntity } from "../Entities/LabelEntity";
import { makeSerializeCustomLabels, } from "./SerializeCustomLabels";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const ao = appObjects.getOrCreate("ao");
    const stateMachine = new MockStateMachine(ao);
    const obj1 = appObjects.getOrCreate("id1");
    const obj2 = appObjects.getOrCreate("id2");
    LabelEntity.addIfMissing(obj1);
    LabelEntity.addIfMissing(obj2);
    const serializer = makeSerializeCustomLabels(ao);
    return {
        appObjects,
        serializer,
        obj1,
        obj2,
        stateMachine,
    };
}
describe("Serialize Custom Labels", () => {
    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.customLabels).toHaveLength(0);
    });
    it("Gets object state with custom labels objects", () => {
        const { serializer, obj1, obj2 } = makeTestRig();
        const customLabel1 = LabelEntity.get(obj1);
        customLabel1.custom = "custom1";
        const customLabel2 = LabelEntity.get(obj2);
        customLabel2.custom = "custom2";
        const state = serializer.getSystemState();
        expect(state.customLabels).toHaveLength(2);
        expect(state.customLabels[0].objectId).toEqual(obj1.id);
        expect(state.customLabels[1].objectId).toEqual(obj2.id);
    });
    it("Sets all active objects to empty string of custom when applyDefaultState is called", () => {
        const { serializer, obj1, obj2 } = makeTestRig();
        const customLabel1 = LabelEntity.get(obj1);
        customLabel1.custom = "custom1";
        const customLabel2 = LabelEntity.get(obj2);
        customLabel2.custom = "custom2";
        serializer.applyDefaultState(1);
        expect(customLabel1?.custom).toEqual("");
        expect(customLabel2?.custom).toEqual("");
    });
    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 custom labels when applying the state", () => {
        const { serializer, obj1, obj2 } = makeTestRig();
        const customLabel1 = LabelEntity.get(obj1);
        customLabel1.custom = "custom1";
        const customLabel2 = LabelEntity.get(obj2);
        customLabel2.custom = "custom2";
        const state = {
            customLabels: [
                {
                    objectId: "id1",
                    customLabel: "custom11",
                },
                {
                    objectId: "id2",
                    customLabel: "custom22",
                },
            ],
        };
        serializer.applySystemState(state, 1, 2);
        expect(customLabel1?.custom).toEqual("custom11");
        expect(customLabel2?.custom).toEqual("custom22");
    });
    it("Adds a label component to any objects in the custom label objects list that are missing one", () => {
        const { appObjects, serializer } = makeTestRig();
        const obj3 = appObjects.getOrCreate("id3");
        const state = {
            customLabels: [
                {
                    objectId: "id1",
                    customLabel: "custom1",
                },
                {
                    objectId: "id3",
                    customLabel: "custom3",
                },
            ],
        };
        serializer.applySystemState(state, 1, 2);
        const customLabel3 = LabelEntity.get(obj3);
        expect(customLabel3).toBeDefined();
        expect(customLabel3.custom).toEqual("custom3");
    });
    it("Creates the app object and label entity if it doesn't exist", () => {
        const { appObjects, serializer } = makeTestRig();
        const state = {
            customLabels: [
                {
                    objectId: "id1",
                    customLabel: "custom1",
                },
                {
                    objectId: "id3",
                    customLabel: "custom3",
                },
            ],
        };
        serializer.applySystemState(state, 1, 2);
        const customLabel3 = LabelEntity.getByID("id3", appObjects);
        expect(customLabel3).toBeDefined();
        expect(customLabel3?.custom).toEqual("custom3");
    });
});
//# sourceMappingURL=SerializeCustomLabels.test.js.map