import { makeAppObjectRepo } from "@vived/core";
import { ParametricLine, Quaternion, Vector3 } from "@vived/core";
import { makeMockSceneRayPickUC } from "../../Babylon";
import { makeMockDispatchStateChange } from "../../Dispatcher/Mocks/MockDispatchStateChange";
import { makeToolRepo } from "../../Tools";
import { makeMockUndoRedo } from "../../UndoRedo/Mocks/MockUndoRedo";
import { makeLandmarkGlobals } from "../Entities";
import { makeLandmarkRepoEntity } from "../Entities/LandmarkRepo";
import { makeTryAddLandmarkUC } from "./TryAddNewLandmarkUC";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const dispatchStateChange = makeMockDispatchStateChange(appObjects);
    const doDispatchMock = jest.fn();
    dispatchStateChange.doDispatch = doDispatchMock;
    const mockUndo = makeMockUndoRedo(appObjects);
    const submitActionMock = jest.fn();
    mockUndo.submitAction = submitActionMock;
    const mockRayPick = makeMockSceneRayPickUC(appObjects);
    const mockPick = jest.fn().mockReturnValue({
        hit: true,
        appObjectID: "anAppObject",
        normal: Vector3.Forward(),
        worldPickPoint: Vector3.Zero(),
    });
    mockRayPick.sceneRayPick = mockPick;
    const pickedAO = appObjects.getOrCreate("anAppObject");
    const repo = makeLandmarkRepoEntity(pickedAO);
    const landmarkGlobals = makeLandmarkGlobals(appObjects.getOrCreate("LandmarkGlobals"));
    const tryAddLandmarkUC = makeTryAddLandmarkUC(pickedAO);
    const toolRepo = 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(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: Vector3.Forward(),
            worldPickPoint: new Vector3(1, 2, 3),
        });
        const newLandmark = tryAddLandmarkUC.addFromRay(ParametricLine.Forward());
        if (!newLandmark) {
            expect(1).toEqual(0);
            return;
        }
        expect(newLandmark.position).toEqual(new Vector3(1, 2, 3));
    });
    it("Sets the rotation to the look rotation", () => {
        const { tryAddLandmarkUC, mockPick } = makeTestRig();
        mockPick.mockReturnValue({
            hit: true,
            appObjectID: "anAppObject",
            normal: new Vector3(1, 2, 3),
            worldPickPoint: Vector3.Zero(),
        });
        const newLandmark = tryAddLandmarkUC.addFromRay(ParametricLine.Forward());
        if (!newLandmark) {
            expect(1).toEqual(0);
            return;
        }
        const expectedQ = Quaternion.FromDirectionVector(new Vector3(1, 2, 3));
        expect(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(ParametricLine.Forward());
        expect(newLandmark?.size).toEqual(0.8);
    });
    it("Sets label", () => {
        const { tryAddLandmarkUC } = makeTestRig();
        const newLandmark = tryAddLandmarkUC.addFromRay(ParametricLine.Forward());
        expect(newLandmark?.label.display).toEqual("");
    });
    it("Submits an undo", () => {
        const { tryAddLandmarkUC, submitActionMock } = makeTestRig();
        tryAddLandmarkUC.addFromRay(ParametricLine.Forward());
        expect(submitActionMock).toHaveBeenCalled();
    });
    it("Dispatches state change", () => {
        const { tryAddLandmarkUC, doDispatchMock } = makeTestRig();
        const newLandmark = tryAddLandmarkUC.addFromRay(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(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