import { makeAppObjectRepo } from "@vived/core";
import { ParametricLine, Vector2, Vector3 } from "@vived/core";
import { makeMockScenePixelPickUC, makeMockSceneRayPickUC, } from "../../Babylon";
import { makeToolRepo, ToolState } from "../../Tools";
import { StickUCMock } from "../Mocks/StickUCMock";
import { makeTryStickAppObjectUC, TryStickAppObjectUC, } from "./TryStickAppObjectUC";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const registerSingleton = jest.spyOn(appObjects, "registerSingleton");
    const ao = appObjects.getOrCreate("AO");
    const uc = makeTryStickAppObjectUC(ao);
    const stickableObj = appObjects.getOrCreate("stickableObj");
    const mockHideUC = new StickUCMock(stickableObj);
    const mockRayPick = makeMockSceneRayPickUC(appObjects);
    const mockPick = jest.fn().mockReturnValue({ appObjectID: "stickableObj" });
    mockRayPick.sceneRayPick = mockPick;
    const mockPixelPick = makeMockScenePixelPickUC(appObjects);
    mockPixelPick.scenePixelPick.mockReturnValue({ appObjectID: "stickableObj" });
    const toolRepo = makeToolRepo(appObjects.getOrCreate("Tool"));
    const peelTool = toolRepo.makeTool("PEEL");
    peelTool.state = ToolState.ACTIVE;
    return {
        appObjects,
        ao,
        mockHideUC,
        uc,
        registerSingleton,
        mockPick,
        mockPixelPick,
        peelTool,
    };
}
describe("Try Stick UC", () => {
    it("Gets the singleton", () => {
        const { appObjects, uc } = makeTestRig();
        expect(TryStickAppObjectUC.get(appObjects)).toEqual(uc);
    });
    it("Registers as the singleton", () => {
        const { registerSingleton, uc } = makeTestRig();
        expect(registerSingleton).toHaveBeenCalledWith(uc);
    });
    it("Picks with the ray", () => {
        const { uc, mockPick } = makeTestRig();
        const pickRay = ParametricLine.FromPointDirection(new Vector3(1, 2, 3), new Vector3(4, 5, 6));
        uc.tryWithRay(pickRay);
        expect(mockPick).toHaveBeenCalledWith(pickRay);
    });
    it("Calls the UC", () => {
        const { uc, mockHideUC } = makeTestRig();
        uc.tryWithRay(ParametricLine.Forward());
        expect(mockHideUC.doStick).toHaveBeenCalled();
    });
    it("Does not call on ray pick if not allowed", () => {
        const { uc, mockHideUC } = makeTestRig();
        uc.allow = () => {
            return false;
        };
        uc.tryWithRay(ParametricLine.Forward());
        expect(mockHideUC.doStick).not.toHaveBeenCalled();
    });
    it("Doesn't warn if the picked object does not have the UC", () => {
        const { uc, mockPick, appObjects } = makeTestRig();
        appObjects.submitWarning = jest.fn();
        appObjects.getOrCreate("SomeOtherObject");
        mockPick.mockReturnValue({
            appObjectID: "SomeOtherObject",
        });
        uc.tryWithRay(ParametricLine.Forward());
        expect(appObjects.submitWarning).not.toHaveBeenCalled();
    });
    it("Picks with a pixel", () => {
        const { uc, mockPixelPick } = makeTestRig();
        const pickPixel = new Vector2(1, 2);
        uc.tryWithPixel(pickPixel);
        expect(mockPixelPick.scenePixelPick).toHaveBeenCalledWith(pickPixel);
    });
    it("Calls the UC with a pixel pick", () => {
        const { uc, mockHideUC } = makeTestRig();
        uc.tryWithPixel(Vector2.One());
        expect(mockHideUC.doStick).toHaveBeenCalled();
    });
    it("Does not call on pixel pick if not allowed", () => {
        const { uc, mockHideUC } = makeTestRig();
        uc.allow = () => {
            return false;
        };
        uc.tryWithPixel(Vector2.One());
        expect(mockHideUC.doStick).not.toHaveBeenCalled();
    });
    it("Allows if the peel tool is active", () => {
        const { uc, peelTool } = makeTestRig();
        peelTool.state = ToolState.ACTIVE;
        expect(uc.allow()).toEqual(true);
        peelTool.state = ToolState.ENABLED;
        expect(uc.allow()).toEqual(false);
    });
});
//# sourceMappingURL=TryStickAppObjectUC.test.js.map