import { makeAppObjectRepo } from "@vived/core";
import { ParametricLine, Vector2, Vector3 } from "@vived/core";
import { ScenePixelPickUC } from "../../Babylon";
import { makeSceneRootEntity } from "../Entities";
import { centerOnPixel, CenterSceneRootOnPixelPickUC, makeCenterSceneRootOnPixelPickUC, } from "./CenterSceneRootOnPixelPick";
class MockPick extends ScenePixelPickUC {
    constructor() {
        super(...arguments);
        this.scenePixelPick = jest.fn();
    }
}
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const sceneRootAO = appObjects.getOrCreate("sceneRoot");
    const sceneRoot = makeSceneRootEntity(sceneRootAO);
    const centerUC = makeCenterSceneRootOnPixelPickUC(sceneRootAO);
    const mockLerpToPoint = jest.fn();
    sceneRoot.centerOnPoint = mockLerpToPoint;
    const appAO = appObjects.getOrCreate("appAO");
    const mockPick = new MockPick(appAO, ScenePixelPickUC.type);
    return { centerUC, appObjects, mockPick, mockLerpToPoint };
}
describe("Center Scene Root Use Case", () => {
    it("Gets the UC", () => {
        const { centerUC, appObjects } = makeTestRig();
        expect(CenterSceneRootOnPixelPickUC.get(appObjects)).toEqual(centerUC);
    });
    it("Warns if it is added to something without a scene root", () => {
        const { appObjects } = makeTestRig();
        appObjects.submitWarning = jest.fn();
        const newAO = appObjects.getOrCreate("newAO");
        makeCenterSceneRootOnPixelPickUC(newAO);
        expect(appObjects.submitWarning).toHaveBeenCalled();
    });
    it("Finds and uses the UC", () => {
        const { appObjects, centerUC } = makeTestRig();
        centerUC.centerOnPixel = jest.fn();
        centerOnPixel(new Vector2(1, 2), appObjects);
        expect(centerUC.centerOnPixel).toHaveBeenCalledWith(new Vector2(1, 2));
    });
    it("Warns if it can't find the UC", () => {
        const { centerUC, appObjects } = makeTestRig();
        appObjects.submitWarning = jest.fn();
        centerUC.dispose();
        centerOnPixel(new Vector2(1, 2), appObjects);
        expect(appObjects.submitWarning).toHaveBeenCalled();
    });
    it("Centers on the pick point", () => {
        const { centerUC, mockPick, mockLerpToPoint } = makeTestRig();
        const pick = {
            hit: true,
            sceneRay: ParametricLine.Forward(),
            worldPickPoint: new Vector3(10, 20, 30),
        };
        mockPick.scenePixelPick.mockReturnValue(pick);
        centerUC.centerOnPixel(Vector2.Zero());
        expect(mockLerpToPoint).toHaveBeenCalled();
        const pickPoint = mockLerpToPoint.mock.calls[0][0];
        expect(pickPoint).toEqual(new Vector3(10, 20, 30));
    });
    it("Sends the duration", () => {
        const { centerUC, mockLerpToPoint, mockPick } = makeTestRig();
        centerUC.durationMS = 750;
        const pick = {
            hit: true,
            sceneRay: ParametricLine.Forward(),
            worldPickPoint: new Vector3(10, 20, 30),
        };
        mockPick.scenePixelPick.mockReturnValue(pick);
        centerUC.centerOnPixel(Vector2.Zero());
        centerUC.centerOnPixel(Vector2.Zero());
        expect(mockLerpToPoint).toHaveBeenCalled();
        const sentDuration = mockLerpToPoint.mock.calls[0][1];
        expect(sentDuration).toEqual(centerUC.durationMS);
    });
    it("Sends the lerp ease", () => {
        const { centerUC, mockLerpToPoint, mockPick } = makeTestRig();
        centerUC.easeFn = (v) => {
            return 0.5 * v;
        };
        const pick = {
            hit: true,
            sceneRay: ParametricLine.Forward(),
            worldPickPoint: new Vector3(10, 20, 30),
        };
        mockPick.scenePixelPick.mockReturnValue(pick);
        centerUC.centerOnPixel(Vector2.Zero());
        centerUC.centerOnPixel(Vector2.Zero());
        expect(mockLerpToPoint).toHaveBeenCalled();
        const sentEase = mockLerpToPoint.mock.calls[0][2];
        expect(sentEase).toEqual(centerUC.easeFn);
    });
    it("If there is no pick it does not start a lerp", () => {
        const { centerUC, mockLerpToPoint, mockPick } = makeTestRig();
        const pick = {
            hit: false,
            sceneRay: ParametricLine.Forward(),
        };
        mockPick.scenePixelPick.mockReturnValue(pick);
        centerUC.centerOnPixel(Vector2.Zero());
        centerUC.centerOnPixel(Vector2.Zero());
        expect(mockLerpToPoint).not.toHaveBeenCalled();
    });
});
//# sourceMappingURL=CenterSceneRootOnPixelPick.test.js.map