import { Angle, makeAppObjectRepo, ParametricLine, Quaternion, Vector2, Vector3 } from "@vived/core";
import { makeWorkPlaneEntity, WorkPlaneShape, WorkPlaneUnits, } from "./WorkPlaneEntity";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const appObject = appObjects.getOrCreate("workplane");
    const workPlane = makeWorkPlaneEntity(appObject);
    return { workPlane, appObjects };
}
describe("Work Plane Entity", () => {
    it("Returns the expected conversion number", () => {
        const { workPlane } = makeTestRig();
        workPlane.units = WorkPlaneUnits.METERS;
        expect(workPlane.conversionFactor).toEqual(1);
        workPlane.units = WorkPlaneUnits.INCHES;
        expect(workPlane.conversionFactor).toEqual(39.3701);
        workPlane.units = WorkPlaneUnits.FEET;
        expect(workPlane.conversionFactor).toEqual(3.28084);
    });
    it("Returns undefined if there is no collision between the work plane and ray", () => {
        const { workPlane } = makeTestRig();
        const ray = ParametricLine.FromPointDirection(new Vector3(0, 1, 0), Vector3.Forward());
        const pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).toBeUndefined();
    });
    it("Returns the work plane coordinates of the collision between the work plane and ray", () => {
        const { workPlane } = makeTestRig();
        workPlane.transform.localRotation = Quaternion.FromYawPitchRoll(Angle.FromDegrees(0), Angle.FromDegrees(-90), Angle.FromDegrees(0));
        const ray = ParametricLine.FromPointDirection(new Vector3(1, 2, -3), Vector3.Forward());
        workPlane.units = WorkPlaneUnits.METERS;
        let pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).toEqual(new Vector2(1, 2));
        workPlane.units = WorkPlaneUnits.FEET;
        pick = workPlane.pickOnWorkPlane(ray);
        let expected = new Vector2(3.28084, 2 * 3.28084);
        expect(pick).toEqual(expected);
        workPlane.units = WorkPlaneUnits.INCHES;
        pick = workPlane.pickOnWorkPlane(ray) ?? Vector2.Zero();
        expected = new Vector2(39.3701, 2 * 39.3701);
        expect(Vector2.Close(pick, expected)).toEqual(true);
    });
    it("Sets the workplane shape to circle if a radius is set", () => {
        const { workPlane } = makeTestRig();
        expect(workPlane.workPlaneShape).toEqual(WorkPlaneShape.UNKNOWN);
        workPlane.setWorkPlaneRadius(10);
        expect(workPlane.workPlaneShape).toEqual(WorkPlaneShape.CIRCLE);
    });
    it("Sets the workplane shape to rectage if a size is set", () => {
        const { workPlane } = makeTestRig();
        expect(workPlane.workPlaneShape).toEqual(WorkPlaneShape.UNKNOWN);
        workPlane.setWorkPlaneSize(new Vector2(1, 2));
        expect(workPlane.workPlaneSize).toEqual(new Vector2(1, 2));
        expect(workPlane.workPlaneShape).toEqual(WorkPlaneShape.RECTANGLE);
    });
    it("Constrains the pick if the radius is set", () => {
        const { workPlane } = makeTestRig();
        const ray = ParametricLine.FromPointDirection(new Vector3(1, 2, 0), Vector3.Down());
        let pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).not.toBeUndefined();
        workPlane.setWorkPlaneRadius(1);
        pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).not.toBeUndefined();
        workPlane.setWorkPlaneRadius(0.9);
        pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).toBeUndefined();
    });
    it("Constrains the pick positive x direction if the size is set", () => {
        const { workPlane } = makeTestRig();
        const ray = ParametricLine.FromPointDirection(new Vector3(1, 2, 0), Vector3.Down());
        let pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).not.toBeUndefined();
        workPlane.setWorkPlaneSize(new Vector2(2, 0));
        pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).not.toBeUndefined();
        workPlane.setWorkPlaneSize(new Vector2(1.9, 0));
        pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).toBeUndefined();
    });
    it("Constrains the pick negative x direction if the size is set", () => {
        const { workPlane } = makeTestRig();
        const ray = ParametricLine.FromPointDirection(new Vector3(-1, 2, 0), Vector3.Down());
        let pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).not.toBeUndefined();
        workPlane.setWorkPlaneSize(new Vector2(2, 0));
        pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).not.toBeUndefined();
        workPlane.setWorkPlaneSize(new Vector2(1.9, 0));
        pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).toBeUndefined();
    });
    it("Constrains the pick positive y direction if the size is set", () => {
        const { workPlane } = makeTestRig();
        const ray = ParametricLine.FromPointDirection(new Vector3(0, 2, 1), Vector3.Down());
        let pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).not.toBeUndefined();
        workPlane.setWorkPlaneSize(new Vector2(0, 2));
        pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).not.toBeUndefined();
        workPlane.setWorkPlaneSize(new Vector2(0, 1.9));
        pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).toBeUndefined();
    });
    it("Constrains the pick negative y direction if the size is set", () => {
        const { workPlane } = makeTestRig();
        const ray = ParametricLine.FromPointDirection(new Vector3(0, 2, -1), Vector3.Down());
        let pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).not.toBeUndefined();
        workPlane.setWorkPlaneSize(new Vector2(0, 2));
        pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).not.toBeUndefined();
        workPlane.setWorkPlaneSize(new Vector2(0, 1.9));
        pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).toBeUndefined();
    });
    it("Does not constrain if the flag is set to true", () => {
        const { workPlane } = makeTestRig();
        const ray = ParametricLine.FromPointDirection(new Vector3(0, 2, 1), Vector3.Down());
        workPlane.setWorkPlaneSize(new Vector2(0, 1.9));
        let pick = workPlane.pickOnWorkPlane(ray);
        expect(pick).toBeUndefined();
        pick = workPlane.pickOnWorkPlane(ray, true);
        expect(pick).not.toBeUndefined();
    });
});
//# sourceMappingURL=WorkPlaneEntity.test.js.map