import { makeAppObjectRepo } from "@vived/core";
import { ParametricLine, ParametricPlane, Vector2, Vector3 } from "@vived/core";
import { makeMockPixelToSceneRayUC, makeMockSceneRayPickUC, } from "../../Babylon";
import { makeMockDispatchStateChange } from "../../Dispatcher/Mocks/MockDispatchStateChange";
import { makeTransform3D } from "../Entities";
import { makeScaleTransformUC } from "./ScaleTransform";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const ao = appObjects.getOrCreate("ao");
    const transform = makeTransform3D(ao);
    const mockDispatch = makeMockDispatchStateChange(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatch.doDispatch = doDispatchMock;
    const scaleUC = makeScaleTransformUC(ao);
    const mockRayPick = makeMockSceneRayPickUC(appObjects);
    const mockPick = jest.fn().mockReturnValue({ appObjectID: "hideableObj" });
    mockRayPick.sceneRayPick = mockPick;
    const mockSceneRay = makeMockPixelToSceneRayUC(appObjects);
    mockSceneRay.pixelToSceneRay = mockPixelToRay;
    return {
        scaleUC,
        appObjects,
        doDispatchMock,
        mockPick,
        transform,
    };
}
function mockPixelToRay(pix) {
    return ParametricLine.FromPointDirection(new Vector3(pix.x, pix.y, -5), Vector3.Forward());
}
describe("Scale Transform UC - Ray", () => {
    it("Decreases the scaleUC with forward movement", () => {
        const { scaleUC, transform } = makeTestRig();
        transform.worldScale = Vector3.One();
        scaleUC.initializeRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 4.5), Vector3.Forward()));
        scaleUC.updateRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 5), Vector3.Forward()));
        expect(transform.worldScale.x).toBeLessThan(1);
    });
    it("Increases the scaleUC with backwards movement", () => {
        const { scaleUC, transform } = makeTestRig();
        transform.worldScale = Vector3.One();
        scaleUC.initializeRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 5), Vector3.Forward()));
        scaleUC.updateRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 4.5), Vector3.Forward()));
        expect(transform.worldScale.x).toBeGreaterThan(1);
    });
    it("Scales around the origin if there is no pick", () => {
        const { scaleUC, mockPick, transform } = makeTestRig();
        transform.worldPosition = Vector3.Zero();
        const pick = {
            hit: false,
            sceneRay: ParametricLine.Forward(),
        };
        mockPick.mockReturnValue(pick);
        scaleUC.initializeRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 5), Vector3.Forward()));
        scaleUC.updateRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 4.5), Vector3.Forward()));
        expect(transform.worldPosition).toEqual(Vector3.Zero());
    });
    it("Scales around the pick point", () => {
        const { scaleUC, transform, mockPick } = makeTestRig();
        transform.worldPosition = Vector3.Zero();
        const pick = {
            hit: true,
            sceneRay: ParametricLine.Forward(),
            worldPickPoint: new Vector3(1, 2, 3),
        };
        mockPick.mockReturnValue(pick);
        scaleUC.initializeRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 5), Vector3.Forward()));
        scaleUC.updateRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 4), Vector3.Forward()));
        expect(transform.worldPosition).toEqual(new Vector3(-1, -2, -3));
    });
    it("Dispatches state change", () => {
        const { scaleUC, doDispatchMock } = makeTestRig();
        scaleUC.initializeRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 5), Vector3.Forward()));
        scaleUC.updateRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 4.5), Vector3.Forward()));
        scaleUC.finish();
        expect(doDispatchMock).toHaveBeenCalled();
    });
    it("Does not dispatches state change if the scale does not change", () => {
        const { scaleUC, doDispatchMock } = makeTestRig();
        scaleUC.initializeRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 5), Vector3.Forward()));
        scaleUC.finish();
        expect(doDispatchMock).not.toHaveBeenCalled();
    });
    it("Does not dispatches state change if it was not initialized", () => {
        const { scaleUC, doDispatchMock } = makeTestRig();
        scaleUC.finish();
        expect(doDispatchMock).not.toHaveBeenCalled();
    });
    it("Does not scale if it is not initialized", () => {
        const { scaleUC, transform } = makeTestRig();
        transform.worldScale = Vector3.One();
        scaleUC.updateRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 4.5), Vector3.Forward()));
        expect(transform.worldScale.x).toEqual(1);
    });
    it("Limits the minimum scale", () => {
        const { scaleUC, transform } = makeTestRig();
        scaleUC.minScale = 0.5;
        transform.worldScale = Vector3.One();
        scaleUC.initializeRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 4), Vector3.Forward()));
        scaleUC.updateRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 5), Vector3.Forward()));
        expect(transform.worldScale).toEqual(new Vector3(0.5, 0.5, 0.5));
    });
    it("Limits the maximum scale", () => {
        const { scaleUC, transform } = makeTestRig();
        scaleUC.maxScale = 1.5;
        transform.worldScale = Vector3.One();
        scaleUC.initializeRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 5), Vector3.Forward()));
        scaleUC.updateRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 4), Vector3.Forward()));
        expect(transform.worldScale).toEqual(new Vector3(1.5, 1.5, 1.5));
    });
    it("Does not update if disabled", () => {
        const { scaleUC, transform } = makeTestRig();
        scaleUC.enabled = false;
        transform.worldScale = Vector3.One();
        scaleUC.initializeRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 5), Vector3.Forward()));
        scaleUC.updateRay(ParametricLine.FromPointDirection(new Vector3(0, 0, 4.5), Vector3.Forward()));
        expect(transform.worldScale).toEqual(Vector3.One());
    });
    it("Ray scales around the pick point on the plane if the plane is supplied", () => {
        const { scaleUC, transform } = makeTestRig();
        transform.worldPosition = Vector3.Zero();
        const pickPlane = ParametricPlane.FromPointNormal(new Vector3(0, 0, -1), Vector3.Forward());
        scaleUC.pickPlane = pickPlane;
        scaleUC.initializeRay(ParametricLine.FromPointDirection(new Vector3(0, 0, -5), Vector3.Forward()));
        scaleUC.updateRay(ParametricLine.FromPointDirection(new Vector3(0, 0, -4), Vector3.Forward()));
        expect(transform.worldPosition.x).toBeCloseTo(0);
        expect(transform.worldPosition.y).toBeCloseTo(0);
        expect(transform.worldPosition.z).toBeCloseTo(-1);
    });
});
describe("Scale Transform UC - Pixel", () => {
    it("Decreases the scaleUC with down movement", () => {
        const { scaleUC, transform } = makeTestRig();
        transform.worldScale = Vector3.One();
        scaleUC.initializePixel(Vector2.Zero());
        scaleUC.updatePixel(new Vector2(0, -5));
        expect(transform.worldScale.x).toBeLessThan(1);
    });
    it("Increases the scaleUC with up movement", () => {
        const { scaleUC, transform } = makeTestRig();
        transform.worldScale = Vector3.One();
        scaleUC.initializePixel(Vector2.Zero());
        scaleUC.updatePixel(new Vector2(0, 5));
        expect(transform.worldScale.x).toBeGreaterThan(1);
    });
    it("Scales around the origin if there is no pick", () => {
        const { scaleUC, mockPick, transform } = makeTestRig();
        transform.worldPosition = Vector3.Zero();
        const pick = {
            hit: false,
            sceneRay: ParametricLine.Forward(),
        };
        mockPick.mockReturnValue(pick);
        scaleUC.initializePixel(Vector2.Zero());
        scaleUC.updatePixel(new Vector2(0, 10));
        expect(transform.worldPosition).toEqual(Vector3.Zero());
    });
    it("Scales around the pick point", () => {
        const { scaleUC, transform, mockPick } = makeTestRig();
        transform.worldPosition = Vector3.Zero();
        const pick = {
            hit: true,
            sceneRay: ParametricLine.Forward(),
            worldPickPoint: new Vector3(1, 2, 3),
        };
        mockPick.mockReturnValue(pick);
        scaleUC.initializePixel(Vector2.Zero());
        scaleUC.updatePixel(new Vector2(0, 10));
        expect(transform.worldPosition).toEqual(new Vector3(-1, -2, -3));
    });
    it("Dispatches state change", () => {
        const { scaleUC, doDispatchMock } = makeTestRig();
        scaleUC.initializePixel(Vector2.Zero());
        scaleUC.updatePixel(new Vector2(0, -5));
        scaleUC.finish();
        expect(doDispatchMock).toHaveBeenCalled();
    });
    it("Does not dispatches state change if the scale does not change", () => {
        const { scaleUC, doDispatchMock } = makeTestRig();
        scaleUC.initializePixel(Vector2.Zero());
        scaleUC.finish();
        expect(doDispatchMock).not.toHaveBeenCalled();
    });
    it("Does not dispatches state change if it was not initialized", () => {
        const { scaleUC, doDispatchMock } = makeTestRig();
        scaleUC.finish();
        expect(doDispatchMock).not.toHaveBeenCalled();
    });
    it("Does not scale if it is not initialized", () => {
        const { scaleUC, transform } = makeTestRig();
        transform.worldScale = Vector3.One();
        scaleUC.updatePixel(new Vector2(0, 10));
        expect(transform.worldScale.x).toEqual(1);
    });
    it("Limits the minimum scale", () => {
        const { scaleUC, transform } = makeTestRig();
        scaleUC.minScale = 0.5;
        transform.worldScale = Vector3.One();
        scaleUC.initializePixel(Vector2.Zero());
        scaleUC.updatePixel(new Vector2(0, -50));
        expect(transform.worldScale).toEqual(new Vector3(0.5, 0.5, 0.5));
    });
    it("Limits the maximum scale", () => {
        const { scaleUC, transform } = makeTestRig();
        scaleUC.maxScale = 1.5;
        transform.worldScale = Vector3.One();
        scaleUC.initializePixel(Vector2.Zero());
        scaleUC.updatePixel(new Vector2(0, 50));
        expect(transform.worldScale).toEqual(new Vector3(1.5, 1.5, 1.5));
    });
    it("Does not update if disabled", () => {
        const { scaleUC, transform } = makeTestRig();
        scaleUC.enabled = false;
        transform.worldScale = Vector3.One();
        scaleUC.initializePixel(Vector2.Zero());
        scaleUC.updatePixel(new Vector2(0, -5));
        expect(transform.worldScale).toEqual(Vector3.One());
    });
    it("Pixel scales around the pick point on the plane if the plane is supplied", () => {
        const { scaleUC, mockPick, transform } = makeTestRig();
        transform.worldPosition = Vector3.Zero();
        const pickPlane = ParametricPlane.FromPointNormal(new Vector3(0, 0, -1), // Offset forward, closer to the camera
        Vector3.Forward());
        scaleUC.pickPlane = pickPlane;
        scaleUC.initializePixel(Vector2.Zero());
        scaleUC.updatePixel(new Vector2(0, 10));
        expect(transform.worldPosition.x).toBeCloseTo(0);
        expect(transform.worldPosition.y).toBeCloseTo(0);
        expect(transform.worldPosition.z).toBeCloseTo(1); // Pushes it away
    });
});
//# sourceMappingURL=ScaleTransform.test.js.map