"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const Selectable_1 = require("./Selectable");
const Selection_1 = require("./Selection");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const selectionAO = appObjects.getOrCreate("selection");
    const selection = (0, Selection_1.makeSelection)(selectionAO);
    const observer = jest.fn();
    selection.addChangeObserver(observer);
    const appObj1 = appObjects.getOrCreate("ao1");
    const ao1Selectable = (0, Selectable_1.makeSelectable)(appObj1);
    selection.addSelectable(ao1Selectable);
    return { selection, ao1Selectable, appObjects, observer, appObj1 };
}
describe("Selection Entity", () => {
    it("Gets the Selection entity", () => {
        const { selection, appObjects } = makeTestRig();
        expect(Selection_1.Selection.get(appObjects)).toEqual(selection);
    });
    it("Checks for a selectable object", () => {
        const { selection } = makeTestRig();
        expect(selection.hasSelectable("ao1")).toEqual(true);
    });
    it("Makes an app object selectable", () => {
        const { selection, appObjects } = makeTestRig();
        const newAO = appObjects.getOrCreate("NewAO");
        expect(selection.hasSelectable("NewAO")).toEqual(false);
        expect(newAO.hasComponent(Selectable_1.Selectable.type)).toEqual(false);
        selection.makeAppObjectSelectable(newAO);
        expect(selection.hasSelectable("NewAO")).toEqual(true);
        expect(newAO.hasComponent(Selectable_1.Selectable.type)).toEqual(true);
    });
});
describe("Selection Entity - Pressed", () => {
    it("Notifies when the pressed object changes", () => {
        const { selection, observer } = makeTestRig();
        expect(selection.pressedObjectId).toBeNull();
        selection.pressedObjectId = "ao1";
        selection.pressedObjectId = "ao1";
        selection.pressedObjectId = "ao1";
        expect(observer).toHaveBeenCalledTimes(1);
        expect(selection.pressedObjectId).toEqual("ao1");
    });
    it("Does not set or notify if the ID has not been added", () => {
        const { selection, observer } = makeTestRig();
        expect(selection.pressedObjectId).toBeNull();
        selection.pressedObjectId = "hello";
        selection.pressedObjectId = "world";
        expect(observer).toHaveBeenCalledTimes(0);
        expect(selection.pressedObjectId).toBeNull();
    });
    it("Clears the pressed if an unknown is added in", () => {
        const { selection, observer } = makeTestRig();
        selection.pressedObjectId = "ao1";
        observer.mockClear();
        selection.pressedObjectId = "hello";
        selection.pressedObjectId = "world";
        expect(observer).toHaveBeenCalledTimes(1);
        expect(selection.pressedObjectId).toBeNull();
    });
    it("Sets the hovered on the selectable", () => {
        const { selection, ao1Selectable } = makeTestRig();
        expect(ao1Selectable.pressed).toEqual(false);
        selection.pressedObjectId = "ao1";
        expect(ao1Selectable.pressed).toEqual(true);
    });
    it("Turns off the previous pressed when it changes", () => {
        const { selection, ao1Selectable } = makeTestRig();
        selection.pressedObjectId = "ao1";
        expect(ao1Selectable.pressed).toEqual(true);
        selection.pressedObjectId = null;
        expect(ao1Selectable.pressed).toEqual(false);
    });
    it("Sets pressed to undefined if that Selectable is removed", () => {
        const { selection, observer, ao1Selectable } = makeTestRig();
        selection.pressedObjectId = "ao1";
        expect(selection.pressedObjectId).toEqual("ao1");
        selection.removeSelectable(ao1Selectable);
        expect(selection.pressedObjectId).toBeNull();
        expect(observer).toHaveBeenCalled();
    });
    it("A selectable is no longer selectable if it is removed", () => {
        const { selection, observer, ao1Selectable } = makeTestRig();
        selection.removeSelectable(ao1Selectable);
        observer.mockClear();
        selection.pressedObjectId = "ao1";
        selection.pressedObjectId = "ao1";
        expect(selection.pressedObjectId).toBeNull();
        expect(observer).not.toHaveBeenCalled();
    });
});
describe("Selection Entity - Hovered", () => {
    it("Notifies when the hovered object changes", () => {
        const { selection, observer } = makeTestRig();
        expect(selection.hoveredObjectId).toBeNull();
        selection.hoveredObjectId = "ao1";
        selection.hoveredObjectId = "ao1";
        selection.hoveredObjectId = "ao1";
        expect(observer).toHaveBeenCalledTimes(1);
        expect(selection.hoveredObjectId).toEqual("ao1");
    });
    it("Does not set or notify if the ID has not been added", () => {
        const { selection, observer } = makeTestRig();
        expect(selection.hoveredObjectId).toBeNull();
        selection.hoveredObjectId = "hello";
        selection.hoveredObjectId = "world";
        expect(observer).toHaveBeenCalledTimes(0);
        expect(selection.hoveredObjectId).toBeNull();
    });
    it("Clears the hovered if an unknown is added in", () => {
        const { selection, observer } = makeTestRig();
        selection.hoveredObjectId = "ao1";
        observer.mockClear();
        selection.hoveredObjectId = "hello";
        selection.hoveredObjectId = "world";
        expect(observer).toHaveBeenCalledTimes(1);
        expect(selection.hoveredObjectId).toBeNull();
    });
    it("Sets the hovered on the selectable", () => {
        const { selection, ao1Selectable } = makeTestRig();
        expect(ao1Selectable.hovered).toEqual(false);
        selection.hoveredObjectId = "ao1";
        expect(ao1Selectable.hovered).toEqual(true);
    });
    it("Turns off the previous hovered when it changes", () => {
        const { selection, ao1Selectable } = makeTestRig();
        selection.hoveredObjectId = "ao1";
        expect(ao1Selectable.hovered).toEqual(true);
        selection.hoveredObjectId = null;
        expect(ao1Selectable.hovered).toEqual(false);
    });
    it("Sets hovered to undefined if that Selectable is removed", () => {
        const { selection, observer, ao1Selectable } = makeTestRig();
        selection.hoveredObjectId = "ao1";
        expect(selection.hoveredObjectId).toEqual("ao1");
        selection.removeSelectable(ao1Selectable);
        expect(selection.hoveredObjectId).toBeNull();
        expect(observer).toHaveBeenCalled();
    });
    it("A selectable is no longer selectable if it is removed", () => {
        const { selection, observer, ao1Selectable } = makeTestRig();
        selection.removeSelectable(ao1Selectable);
        observer.mockClear();
        selection.hoveredObjectId = "ao1";
        selection.hoveredObjectId = "ao1";
        expect(selection.hoveredObjectId).toBeNull();
        expect(observer).not.toHaveBeenCalled();
    });
});
describe("Selection Entity - Selected", () => {
    it("Notifies when the selected object changes", () => {
        const { selection, observer } = makeTestRig();
        expect(selection.selectedObjectId).toBeNull();
        selection.selectedObjectId = "ao1";
        selection.selectedObjectId = "ao1";
        selection.selectedObjectId = "ao1";
        expect(observer).toHaveBeenCalledTimes(1);
        expect(selection.selectedObjectId).toEqual("ao1");
    });
    it("Does not set or notify if the ID has not been added", () => {
        const { selection, observer } = makeTestRig();
        expect(selection.selectedObjectId).toBeNull();
        selection.selectedObjectId = "hello";
        selection.selectedObjectId = "world";
        expect(observer).toHaveBeenCalledTimes(0);
        expect(selection.selectedObjectId).toBeNull();
    });
    it("Clears the selected if an unknown is added in", () => {
        const { selection, observer } = makeTestRig();
        selection.selectedObjectId = "ao1";
        observer.mockClear();
        selection.selectedObjectId = "hello";
        selection.selectedObjectId = "world";
        expect(observer).toHaveBeenCalledTimes(1);
        expect(selection.selectedObjectId).toBeNull();
    });
    it("Sets the selected on the selectable", () => {
        const { selection, ao1Selectable } = makeTestRig();
        expect(ao1Selectable.selected).toEqual(false);
        selection.selectedObjectId = "ao1";
        expect(ao1Selectable.selected).toEqual(true);
    });
    it("Turns off the previous selected when it changes", () => {
        const { selection, ao1Selectable } = makeTestRig();
        selection.selectedObjectId = "ao1";
        expect(ao1Selectable.selected).toEqual(true);
        selection.selectedObjectId = null;
        expect(ao1Selectable.selected).toEqual(false);
    });
    it("Sets selected to undefined if that Selectable is removed", () => {
        const { selection, observer, ao1Selectable } = makeTestRig();
        selection.selectedObjectId = "ao1";
        expect(selection.selectedObjectId).toEqual("ao1");
        selection.removeSelectable(ao1Selectable);
        expect(selection.selectedObjectId).toBeNull();
        expect(observer).toHaveBeenCalled();
    });
    it("A selectable is no longer selectable if it is removed", () => {
        const { selection, observer, ao1Selectable } = makeTestRig();
        selection.removeSelectable(ao1Selectable);
        observer.mockClear();
        selection.selectedObjectId = "ao1";
        selection.selectedObjectId = "ao1";
        expect(selection.selectedObjectId).toBeNull();
        expect(observer).not.toHaveBeenCalled();
    });
});
//# sourceMappingURL=Selection.test.js.map