"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const OverlayColor_1 = require("../../OverlayColor/Entities/OverlayColor");
const MockStateMachine_1 = require("../../StateMachine/Mocks/MockStateMachine");
const CalloutEntity_1 = require("../Entities/CalloutEntity");
const CalloutGlobals_1 = require("../Entities/CalloutGlobals");
const CalloutRepo_1 = require("../Entities/CalloutRepo");
const SerializeCallouts_1 = require("./SerializeCallouts");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const stateMachine = (0, MockStateMachine_1.makeMockStateMachine)(appObjects);
    (0, CalloutGlobals_1.makeCalloutGlobals)(appObjects.getOrCreate("CalloutGlobals"));
    const calloutsAO = appObjects.getOrCreate("callouts");
    const serializer = new SerializeCallouts_1.SerializeCallouts(calloutsAO);
    const repo1 = (0, CalloutRepo_1.makeCalloutRepoEntity)(appObjects.getOrCreate("Repo1"));
    const repo2 = (0, CalloutRepo_1.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 = core_1.Quaternion.FromYawPitchRoll(core_1.Angle.FromDegrees(10), core_1.Angle.FromDegrees(20), core_1.Angle.FromDegrees(30));
    const dto = {
        id,
        label: "Callout Label",
        defaultLabel: "Custom Label",
        parentID,
        rootPosition: new core_1.Vector3(1, 2, 3).dto,
        rootRotation: q.dto,
        labelPosition: new core_1.Vector3(4, 5, 6).dto,
        size: 5,
        color: OverlayColor_1.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 = (0, CalloutEntity_1.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_1.CalloutRepo.type);
        expect(repo).not.toBeUndefined();
    });
});
//# sourceMappingURL=SerializeCallouts.test.js.map