import { Angle, makeAppObjectRepo, Quaternion, Vector3 } from "@vived/core";
import { OverlayColorEnums } from "../../OverlayColor/Entities/OverlayColor";
import { makeMockStateMachine } from "../../StateMachine/Mocks/MockStateMachine";
import { makeCalloutEntity } from "../Entities/CalloutEntity";
import { makeCalloutGlobals } from "../Entities/CalloutGlobals";
import { CalloutRepo, makeCalloutRepoEntity } from "../Entities/CalloutRepo";
import { SerializeCallouts } from "./SerializeCallouts";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const stateMachine = makeMockStateMachine(appObjects);
    makeCalloutGlobals(appObjects.getOrCreate("CalloutGlobals"));
    const calloutsAO = appObjects.getOrCreate("callouts");
    const serializer = new SerializeCallouts(calloutsAO);
    const repo1 = makeCalloutRepoEntity(appObjects.getOrCreate("Repo1"));
    const repo2 = makeCalloutRepoEntity(appObjects.getOrCreate("Repo2"));
    const callout1 = repo1.makeCallout("Callout1");
    const callout2 = repo2.makeCallout("Callout2");
    return {
        serializer,
        callout1,
        callout2,
        repo1,
        repo2,
        stateMachine,
        calloutsAO,
        appObjects,
    };
}
function makeDTO(id, parentID) {
    const q = Quaternion.FromYawPitchRoll(Angle.FromDegrees(10), Angle.FromDegrees(20), Angle.FromDegrees(30));
    const dto = {
        id,
        label: "Callout Label",
        defaultLabel: "Custom Label",
        parentID,
        rootPosition: new Vector3(1, 2, 3).dto,
        rootRotation: q.dto,
        labelPosition: new Vector3(4, 5, 6).dto,
        size: 5,
        color: OverlayColorEnums.BLUE,
    };
    return dto;
}
describe("Serialize Callouts UC", () => {
    it("Registers with the Serialized System on construction", () => {
        const { serializer, stateMachine } = makeTestRig();
        expect(stateMachine.registerSerializedSystem).toHaveBeenCalledWith(serializer);
    });
    it("Returns all the Callouts", () => {
        const { serializer, callout1, callout2 } = makeTestRig();
        const state = serializer.getSystemState();
        expect(state.callouts.length).toEqual(2);
        expect(state.callouts).toEqual([callout1.data, callout2.data]);
    });
    it("Clears all existing callouts if the default state is applied", () => {
        const { serializer, repo1, repo2 } = makeTestRig();
        expect(repo1.getAll().length).toEqual(1);
        expect(repo2.getAll().length).toEqual(1);
        serializer.applyDefaultState();
        expect(repo1.getAll().length).toEqual(0);
        expect(repo2.getAll().length).toEqual(0);
    });
    it("Disposes all existing callouts if the default state is applied", () => {
        const { serializer, callout1, callout2 } = makeTestRig();
        callout1.appObject.dispose = jest.fn();
        callout2.appObject.dispose = jest.fn();
        serializer.applyDefaultState();
        expect(callout1.appObject.dispose).toHaveBeenCalled();
        expect(callout2.appObject.dispose).toHaveBeenCalled();
    });
    it("Defaults and warns if the version is not supported", () => {
        const { serializer } = makeTestRig();
        serializer.warn = jest.fn();
        serializer.applyDefaultState = jest.fn();
        serializer.applySystemState({ foo: "bar" }, -1);
        expect(serializer.applyDefaultState).toHaveBeenCalled();
        expect(serializer.warn).toHaveBeenCalled();
    });
    it("Defaults and warns if the state is bungled", () => {
        const { serializer } = makeTestRig();
        serializer.warn = jest.fn();
        serializer.applyDefaultState = jest.fn();
        serializer.applySystemState({ foo: "bar" }, 1);
        expect(serializer.applyDefaultState).toHaveBeenCalled();
        expect(serializer.warn).toHaveBeenCalled();
    });
    it("Removes callouts that are not in the state", () => {
        const { serializer, repo1, repo2 } = makeTestRig();
        const state = {
            callouts: [makeDTO("aDifferentCallout", "aDifferentRepo")],
        };
        expect(repo1.getAll().length).toEqual(1);
        expect(repo2.getAll().length).toEqual(1);
        serializer.applySystemState(state, 1);
        expect(repo1.getAll().length).toEqual(0);
        expect(repo2.getAll().length).toEqual(0);
    });
    it("Disposes all existing callouts that are not in the state", () => {
        const { serializer, callout1, callout2 } = makeTestRig();
        const state = {
            callouts: [makeDTO("aDifferentCallout", "aDifferentRepo")],
        };
        callout1.appObject.dispose = jest.fn();
        callout2.appObject.dispose = jest.fn();
        serializer.applySystemState(state, 1);
        expect(callout1.appObject.dispose).toHaveBeenCalled();
        expect(callout2.appObject.dispose).toHaveBeenCalled();
    });
    it("Updates existing callouts that are in the state", () => {
        const { serializer, callout1, repo1 } = makeTestRig();
        const spy = jest.spyOn(callout1, "data", "set");
        const data = makeDTO(callout1.appObject.id, "Repo1");
        const state = {
            callouts: [data],
        };
        serializer.applySystemState(state, 1);
        expect(spy).toHaveBeenCalledWith(data);
        expect(repo1.getAll().length).toEqual(1);
    });
    it("Creates a new callout entity", () => {
        const { serializer, repo1 } = makeTestRig();
        const createSpy = jest.spyOn(repo1, "makeCallout");
        const data = makeDTO("newCallout", repo1.appObject.id);
        const state = {
            callouts: [data],
        };
        serializer.applySystemState(state, 1);
        expect(createSpy).toHaveBeenCalledWith("newCallout");
    });
    it("Applies the data to the new callout entity", () => {
        const { serializer, repo1, appObjects } = makeTestRig();
        const newCallout = makeCalloutEntity(appObjects.getOrCreate("newCallout"));
        const spy = jest.spyOn(newCallout, "data", "set");
        repo1.makeCallout = () => {
            return newCallout;
        };
        const data = makeDTO("newCallout", repo1.appObject.id);
        const state = {
            callouts: [data],
        };
        serializer.applySystemState(state, 1);
        expect(spy).toHaveBeenCalledWith(data);
    });
    it("Creates a missing Repo", () => {
        const { serializer, appObjects } = makeTestRig();
        const data = makeDTO("newCallout", "newRepoID");
        const state = {
            callouts: [data],
        };
        expect(appObjects.has("newRepoID")).toEqual(false);
        serializer.applySystemState(state, 1);
        expect(appObjects.has("newRepoID")).toEqual(true);
        const repoAO = appObjects.get("newRepoID");
        expect(repoAO).not.toBeUndefined();
        const repo = repoAO?.getComponent(CalloutRepo.type);
        expect(repo).not.toBeUndefined();
    });
});
//# sourceMappingURL=SerializeCallouts.test.js.map