import { makeAppObjectRepo } from "@vived/core";
import { Quaternion, Vector3 } from "@vived/core";
import { Mesh } from "./Mesh";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const appObj = appObjects.getOrCreate("appObj");
    const mesh = new Mesh(appObj);
    const observer = jest.fn();
    mesh.addChangeObserver(observer);
    return { mesh, observer, appObj, appObjects };
}
function makeSubMesh(path) {
    return {
        meshPath: path,
        neutralPosition: Vector3.Zero(),
        neutralRotation: Quaternion.Identity(),
        neutralScale: Vector3.One(),
    };
}
describe("Mesh Entity", () => {
    it("Sets the scene object", () => {
        const { mesh, appObj } = makeTestRig();
        expect(mesh.appObject).toEqual(appObj);
    });
    it("Adds itself to the App Object", () => {
        const { mesh, appObj } = makeTestRig();
        expect(appObj.hasComponent(Mesh.type)).toEqual(true);
        expect(appObj.getComponent(Mesh.type)).toEqual(mesh);
    });
    it("Removes itself from the App Object when disposed", () => {
        const { mesh, appObj } = makeTestRig();
        mesh.dispose();
        expect(appObj.hasComponent(Mesh.type)).toEqual(false);
    });
    it("Adds a sub mesh", () => {
        const { mesh } = makeTestRig();
        const subMesh = makeSubMesh(`mesh/path/1`);
        expect(mesh.hasMeshPath(`mesh/path/1`)).toEqual(false);
        mesh.addSubMesh(subMesh);
        expect(mesh.hasMeshPath(`mesh/path/1`)).toEqual(true);
    });
    it("Gets the array of sub meshes", () => {
        const { mesh } = makeTestRig();
        const subMesh = makeSubMesh(`mesh/path/1`);
        expect(mesh.subMeshes).toEqual([]);
        mesh.addSubMesh(subMesh);
        expect(mesh.subMeshes).toEqual([subMesh]);
    });
    it("Gets a sub mesh", () => {
        const { mesh } = makeTestRig();
        expect(mesh.getSubMeshForPath(`mesh/path/1`)).toEqual(undefined);
        const subMesh = makeSubMesh(`mesh/path/1`);
        mesh.addSubMesh(subMesh);
        expect(mesh.getSubMeshForPath(`mesh/path/1`)).toEqual(subMesh);
    });
    it("Removes a sub mesh", () => {
        const { mesh } = makeTestRig();
        const subMesh = makeSubMesh(`mesh/path/1`);
        mesh.addSubMesh(subMesh);
        expect(mesh.hasMeshPath(`mesh/path/1`)).toEqual(true);
        mesh.removeMeshPath(`mesh/path/1`);
        expect(mesh.hasMeshPath(`mesh/path/1`)).toEqual(false);
    });
    it("Only notifies the first time a sub mesh is added", () => {
        const { mesh, observer } = makeTestRig();
        const subMesh = makeSubMesh(`mesh/path/1`);
        mesh.addSubMesh(subMesh);
        mesh.addSubMesh(subMesh);
        mesh.addSubMesh(subMesh);
        expect(observer).toHaveBeenCalledTimes(1);
    });
    it("Only notifies if there is a mesh to remove", () => {
        const { mesh, observer } = makeTestRig();
        const subMesh = makeSubMesh(`mesh/path/1`);
        mesh.addSubMesh(subMesh);
        observer.mockClear();
        mesh.removeMeshPath(`mesh/path/1`);
        mesh.removeMeshPath(`mesh/path/1`);
        mesh.removeMeshPath(`mesh/path/1`);
        expect(observer).toHaveBeenCalledTimes(1);
    });
    it("Gets the entity from an AO", () => {
        const { mesh, appObj } = makeTestRig();
        expect(Mesh.get(appObj)).toEqual(mesh);
    });
    it("Returns the existing when calling add if missing ", () => {
        const { mesh, appObj } = makeTestRig();
        expect(Mesh.addIfMissing(appObj)).toEqual(mesh);
    });
    it("Creates a new one if addIfMissing is called", () => {
        const { appObjects } = makeTestRig();
        const newAO = appObjects.getOrCreate("anotherAO");
        expect(newAO.hasComponent(Mesh.type)).toEqual(false);
        Mesh.addIfMissing(newAO);
        expect(newAO.hasComponent(Mesh.type)).toEqual(true);
    });
});
//# sourceMappingURL=Mesh.test.js.map