"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const BaseApp_1 = require("../../BaseApp");
const Tools_1 = require("../../Tools");
const Selectable_1 = require("../Entities/Selectable");
const Selection_1 = require("../Entities/Selection");
const SelectAppObjectUC_1 = require("./SelectAppObjectUC");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const registerSpy = jest.spyOn(appObjects, "registerSingleton");
    const baseAppObj = appObjects.getOrCreate("baseAppObj");
    const selection = (0, Selection_1.makeSelection)(baseAppObj);
    const selectabelAppObj = appObjects.getOrCreate("selectableAppObj");
    const selectable = (0, Selectable_1.makeSelectable)(selectabelAppObj);
    selection.addSelectable(selectable);
    const mockRayPick = (0, Babylon_1.makeMockSceneRayPickUC)(appObjects);
    const mockPick = jest.fn().mockReturnValue({ appObjectID: "hideableObj" });
    mockRayPick.sceneRayPick = mockPick;
    const baseApp = (0, BaseApp_1.makeBaseAppState)(appObjects.getOrCreate("BaseApp"));
    baseApp.isAuthoring = true;
    const toolRepo = (0, Tools_1.makeToolRepo)(appObjects.getOrCreate("ToolRepo"));
    toolRepo.makeTool("CALLOUT");
    toolRepo.makeTool("LANDMARK");
    toolRepo.makeTool("OTHER");
    toolRepo.triggerTool("OTHER");
    const selectSceneObject = (0, SelectAppObjectUC_1.makeSelectAppObject)(baseAppObj);
    return {
        appObjects,
        selectSceneObject,
        selectable,
        selection,
        mockPick,
        registerSpy,
        baseApp,
        toolRepo,
    };
}
describe("Select Scene Object UC", () => {
    it("Selects by ID", () => {
        const { selectSceneObject, selection } = makeTestRig();
        expect(selection.selectedObjectId).toEqual(null);
        selectSceneObject.selectAppObject("selectableAppObj", true);
        expect(selection.selectedObjectId).toEqual("selectableAppObj");
    });
    it("Un-Selects by ID", () => {
        const { selectSceneObject, selection } = makeTestRig();
        selectSceneObject.selectAppObject("selectableAppObj", true);
        expect(selection.selectedObjectId).toEqual("selectableAppObj");
        selectSceneObject.selectAppObject("selectableAppObj", false);
        expect(selection.selectedObjectId).toEqual(null);
    });
    it("Only un-selects if the id is currently selected", () => {
        const { selectSceneObject, selection } = makeTestRig();
        selectSceneObject.selectAppObject("selectableAppObj", true);
        selectSceneObject.selectAppObject("some other id", false);
        expect(selection.selectedObjectId).toEqual("selectableAppObj");
    });
    it("Sets the selected object ID if it pick on it", () => {
        const { selectSceneObject, selection, mockPick } = makeTestRig();
        mockPick.mockReturnValue({ appObjectID: "selectableAppObj" });
        expect(selection.selectedObjectId).toEqual(null);
        selectSceneObject.trySelectAppObject(core_1.ParametricLine.Forward()); // Ray doesn't matter since we hijack the pick
        expect(selection.selectedObjectId).toEqual("selectableAppObj");
    });
    it("Sets the selected to null if the pick doesn't hit anything", () => {
        const { selectSceneObject, selection, mockPick } = makeTestRig();
        mockPick.mockReturnValue({});
        selectSceneObject.selectAppObject("selectableAppObj", true);
        selectSceneObject.trySelectAppObject(core_1.ParametricLine.Forward()); // Ray doesn't matter since we hijack the pick
        expect(selection.selectedObjectId).toEqual(null);
    });
    it("Sets the selected to null if the pick doesn't an app object", () => {
        const { selectSceneObject, selection, mockPick } = makeTestRig();
        mockPick.mockReturnValue({ appObjectID: "something weired" });
        selectSceneObject.selectAppObject("selectableAppObj", true);
        selectSceneObject.trySelectAppObject(core_1.ParametricLine.Forward()); // Ray doesn't matter since we hijack the pick
        expect(selection.selectedObjectId).toEqual(null);
    });
    it("Sets the selected to null if the pick doesn't a selectable object", () => {
        const { selectSceneObject, selection, mockPick, appObjects } = makeTestRig();
        appObjects.getOrCreate("unselectableAppObject");
        mockPick.mockReturnValue({ appObjectID: "unselectableAppObject" });
        selectSceneObject.selectAppObject("selectableAppObj", true);
        selectSceneObject.trySelectAppObject(core_1.ParametricLine.Forward()); // Ray doesn't matter since we hijack the pick
        expect(selection.selectedObjectId).toEqual(null);
    });
    it("Gets the singleton", () => {
        const { selectSceneObject, appObjects } = makeTestRig();
        expect(SelectAppObjectUC_1.SelectAppObjectUC.get(appObjects)).toEqual(selectSceneObject);
    });
    it("Registers as the singleton", () => {
        const { selectSceneObject, registerSpy } = makeTestRig();
        expect(registerSpy).toHaveBeenCalledWith(selectSceneObject);
    });
    it("Allows while authoring", () => {
        const { selectSceneObject, baseApp } = makeTestRig();
        baseApp.isAuthoring = true;
        expect(selectSceneObject.allowSelect()).toEqual(true);
        expect(selectSceneObject.allowTrySelect()).toEqual(true);
        baseApp.isAuthoring = false;
        expect(selectSceneObject.allowSelect()).toEqual(false);
        expect(selectSceneObject.allowTrySelect()).toEqual(false);
    });
    it("Does not change the selected object ID the callout tool is enabled", () => {
        const { appObjects, selectSceneObject, selection, mockPick, toolRepo, } = makeTestRig();
        const selectabelAppObj = appObjects.getOrCreate("currentObjID");
        const selectable = (0, Selectable_1.makeSelectable)(selectabelAppObj);
        selection.addSelectable(selectable);
        toolRepo.triggerTool("CALLOUT");
        mockPick.mockReturnValue({ appObjectID: "selectableAppObj" });
        selection.selectedObjectId = "currentObjID";
        expect(selection.selectedObjectId).toEqual("currentObjID");
        selectSceneObject.trySelectAppObject(core_1.ParametricLine.Forward());
        expect(selection.selectedObjectId).toEqual("currentObjID");
    });
    it("Does not change the selected object ID the landmark tool is enabled", () => {
        const { appObjects, selectSceneObject, selection, mockPick, toolRepo, } = makeTestRig();
        const selectabelAppObj = appObjects.getOrCreate("currentObjID");
        const selectable = (0, Selectable_1.makeSelectable)(selectabelAppObj);
        selection.addSelectable(selectable);
        toolRepo.triggerTool("LANDMARK");
        mockPick.mockReturnValue({ appObjectID: "selectableAppObj" });
        selection.selectedObjectId = "currentObjID";
        expect(selection.selectedObjectId).toEqual("currentObjID");
        selectSceneObject.trySelectAppObject(core_1.ParametricLine.Forward());
        expect(selection.selectedObjectId).toEqual("currentObjID");
    });
});
//# sourceMappingURL=SelectAppObjectUC.test.js.map