"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const MockDispatchStateChange_1 = require("../../Dispatcher/Mocks/MockDispatchStateChange");
const Tools_1 = require("../../Tools");
const MockUndoRedo_1 = require("../../UndoRedo/Mocks/MockUndoRedo");
const Entities_1 = require("../Entities");
const LandmarkRepo_1 = require("../Entities/LandmarkRepo");
const TryAddNewLandmarkUC_1 = require("./TryAddNewLandmarkUC");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const dispatchStateChange = (0, MockDispatchStateChange_1.makeMockDispatchStateChange)(appObjects);
    const doDispatchMock = jest.fn();
    dispatchStateChange.doDispatch = doDispatchMock;
    const mockUndo = (0, MockUndoRedo_1.makeMockUndoRedo)(appObjects);
    const submitActionMock = jest.fn();
    mockUndo.submitAction = submitActionMock;
    const mockRayPick = (0, Babylon_1.makeMockSceneRayPickUC)(appObjects);
    const mockPick = jest.fn().mockReturnValue({
        hit: true,
        appObjectID: "anAppObject",
        normal: core_2.Vector3.Forward(),
        worldPickPoint: core_2.Vector3.Zero(),
    });
    mockRayPick.sceneRayPick = mockPick;
    const pickedAO = appObjects.getOrCreate("anAppObject");
    const repo = (0, LandmarkRepo_1.makeLandmarkRepoEntity)(pickedAO);
    const landmarkGlobals = (0, Entities_1.makeLandmarkGlobals)(appObjects.getOrCreate("LandmarkGlobals"));
    const tryAddLandmarkUC = (0, TryAddNewLandmarkUC_1.makeTryAddLandmarkUC)(pickedAO);
    const toolRepo = (0, Tools_1.makeToolRepo)(appObjects.getOrCreate("ToolRepo"));
    toolRepo.makeTool("LANDMARK");
    toolRepo.makeTool("OTHER");
    toolRepo.triggerTool("LANDMARK");
    return {
        tryAddLandmarkUC,
        landmarkGlobals,
        mockPick,
        repo,
        doDispatchMock,
        mockUndo,
        submitActionMock,
        toolRepo,
    };
}
describe("Try Add Landmark Test", () => {
    it("Tries to add new landmark", () => {
        const { repo, tryAddLandmarkUC } = makeTestRig();
        const newLandmark = tryAddLandmarkUC.addFromRay(core_2.ParametricLine.Forward());
        if (!newLandmark) {
            expect(1).toEqual(0);
            return;
        }
        expect(repo.hasForAppObject(newLandmark.appObject.id)).toEqual(true);
    });
    it("Sets the position to the pick pos", () => {
        const { tryAddLandmarkUC, mockPick } = makeTestRig();
        mockPick.mockReturnValue({
            hit: true,
            appObjectID: "anAppObject",
            normal: core_2.Vector3.Forward(),
            worldPickPoint: new core_2.Vector3(1, 2, 3),
        });
        const newLandmark = tryAddLandmarkUC.addFromRay(core_2.ParametricLine.Forward());
        if (!newLandmark) {
            expect(1).toEqual(0);
            return;
        }
        expect(newLandmark.position).toEqual(new core_2.Vector3(1, 2, 3));
    });
    it("Sets the rotation to the look rotation", () => {
        const { tryAddLandmarkUC, mockPick } = makeTestRig();
        mockPick.mockReturnValue({
            hit: true,
            appObjectID: "anAppObject",
            normal: new core_2.Vector3(1, 2, 3),
            worldPickPoint: core_2.Vector3.Zero(),
        });
        const newLandmark = tryAddLandmarkUC.addFromRay(core_2.ParametricLine.Forward());
        if (!newLandmark) {
            expect(1).toEqual(0);
            return;
        }
        const expectedQ = core_2.Quaternion.FromDirectionVector(new core_2.Vector3(1, 2, 3));
        expect(core_2.Quaternion.Close(newLandmark.rotation, expectedQ)).toEqual(true);
    });
    it("Sets the last size from landmarkGlobals", () => {
        const { tryAddLandmarkUC, landmarkGlobals } = makeTestRig();
        landmarkGlobals.lastSize = 0.8;
        const newLandmark = tryAddLandmarkUC.addFromRay(core_2.ParametricLine.Forward());
        expect(newLandmark?.size).toEqual(0.8);
    });
    it("Sets label", () => {
        const { tryAddLandmarkUC } = makeTestRig();
        const newLandmark = tryAddLandmarkUC.addFromRay(core_2.ParametricLine.Forward());
        expect(newLandmark?.label.display).toEqual("");
    });
    it("Submits an undo", () => {
        const { tryAddLandmarkUC, submitActionMock } = makeTestRig();
        tryAddLandmarkUC.addFromRay(core_2.ParametricLine.Forward());
        expect(submitActionMock).toHaveBeenCalled();
    });
    it("Dispatches state change", () => {
        const { tryAddLandmarkUC, doDispatchMock } = makeTestRig();
        const newLandmark = tryAddLandmarkUC.addFromRay(core_2.ParametricLine.Forward());
        if (!newLandmark) {
            expect(1).toEqual(0);
            return;
        }
        expect(doDispatchMock).toHaveBeenCalled();
    });
    it("Does not add if allow is returns false", () => {
        const { tryAddLandmarkUC } = makeTestRig();
        tryAddLandmarkUC.allow = () => {
            return false;
        };
        const newLandmark = tryAddLandmarkUC.addFromRay(core_2.ParametricLine.Forward());
        expect(newLandmark).toBeUndefined();
    });
    it("Does not allow if the tool is not Callout", () => {
        const { tryAddLandmarkUC, toolRepo } = makeTestRig();
        toolRepo.triggerTool("OTHER");
        expect(tryAddLandmarkUC.allow()).toEqual(false);
        toolRepo.triggerTool("LANDMARK");
        expect(tryAddLandmarkUC.allow()).toEqual(true);
    });
});
//# sourceMappingURL=TryAddNewLandmarkUC.test.js.map