"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const SceneRootEntity_1 = require("./SceneRootEntity");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const rootAO = appObjects.getOrCreate("Scene Root");
    const sceneRoot = (0, SceneRootEntity_1.makeSceneRootEntity)(rootAO);
    const observer = jest.fn();
    sceneRoot.addChangeObserver(observer);
    return { sceneRoot, observer, appObjects };
}
describe("Scene Root Entity", () => {
    it("Initializes the default position to zero", () => {
        const { sceneRoot } = makeTestRig();
        expect(sceneRoot.defaultPosition).toEqual(core_2.Vector3.Zero());
    });
    it("Initializes the default rotation to identity", () => {
        const { sceneRoot } = makeTestRig();
        expect(sceneRoot.defaultRotation).toEqual(core_2.Quaternion.Identity());
    });
    it("Initializes the default scale to one", () => {
        const { sceneRoot } = makeTestRig();
        expect(sceneRoot.defaultScale).toEqual(1);
    });
    it("Forwards notifications when the transform changes", () => {
        const { sceneRoot, observer } = makeTestRig();
        sceneRoot.transform.localPosition = new core_2.Vector3(1, 2, 3);
        expect(observer).toHaveBeenCalled();
    });
    it("Allows an observer to be removed", () => {
        const { sceneRoot, observer } = makeTestRig();
        sceneRoot.removeChangeObserver(observer);
        sceneRoot.transform.localPosition = new core_2.Vector3(1, 2, 3);
        expect(observer).not.toHaveBeenCalled();
    });
    it("Sets the default data", () => {
        const { sceneRoot } = makeTestRig();
        const data = {
            maxScale: 33,
            minScale: 11,
            position: [1, 2, 3],
            rotation: [4, 5, 6, 7],
            scale: 8,
        };
        sceneRoot.applyDefaultData(data);
        expect(sceneRoot.maxScale).toEqual(data.maxScale);
        expect(sceneRoot.minScale).toEqual(data.minScale);
        expect(sceneRoot.defaultPosition).toEqual(core_2.Vector3.FromArray(data.position));
        expect(sceneRoot.defaultRotation).toEqual(core_2.Quaternion.FromArray(data.rotation));
        expect(sceneRoot.defaultScale).toEqual(8);
    });
    it("Sets the position", () => {
        const { sceneRoot, observer } = makeTestRig();
        observer.mockClear();
        expect(sceneRoot.position).toEqual(core_2.Vector3.Zero());
        sceneRoot.position = new core_2.Vector3(1, 2, 3);
        expect(observer).toHaveBeenCalled();
        expect(sceneRoot.position).toEqual(new core_2.Vector3(1, 2, 3));
        expect(sceneRoot.transform.localPosition).toEqual(new core_2.Vector3(1, 2, 3));
    });
    it("Sets the rotation", () => {
        const { sceneRoot, observer } = makeTestRig();
        observer.mockClear();
        expect(sceneRoot.rotation).toEqual(core_2.Quaternion.Identity());
        const rotQ = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(1, 2, 3), core_2.Angle.FromDegrees(25));
        sceneRoot.rotation = rotQ;
        expect(observer).toHaveBeenCalled();
        expect(core_2.Quaternion.Close(sceneRoot.rotation, rotQ)).toEqual(true);
        expect(core_2.Quaternion.Close(sceneRoot.transform.localRotation, rotQ)).toEqual(true);
    });
    it("Sets the scale", () => {
        const { sceneRoot, observer } = makeTestRig();
        observer.mockClear();
        expect(sceneRoot.scale).toEqual(1);
        sceneRoot.scale = 5;
        expect(observer).toHaveBeenCalled();
        expect(sceneRoot.scale).toEqual(5);
        expect(sceneRoot.transform.localScale).toEqual(new core_2.Vector3(5, 5, 5));
    });
    it("Centers on a point", () => {
        const { sceneRoot } = makeTestRig();
        const centerOnPoint = new core_2.Vector3(10, 20, 30);
        const initialPos = new core_2.Vector3(1, 2, 3);
        const initialRotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(37));
        sceneRoot.position = initialPos;
        sceneRoot.rotation = initialRotation;
        const mockLerp = jest.fn();
        sceneRoot.transform.lerp = mockLerp;
        const expectedFinalPos = core_2.Vector3.Subtract(initialPos, centerOnPoint);
        const competedCallback = jest.fn();
        const linealEase = (val) => {
            return val;
        };
        const duration = 500;
        sceneRoot.centerOnPoint(centerOnPoint, duration, linealEase, competedCallback);
        const expectedDTO = {
            durationMS: 500,
            ease: linealEase,
            desiredPosition: expectedFinalPos,
            onCompletedCallback: competedCallback,
        };
        expect(mockLerp).toHaveBeenCalledWith(expectedDTO);
    });
    it("Lerps to the default point", () => {
        const { sceneRoot } = makeTestRig();
        const defaultPosition = new core_2.Vector3(1, 2, 3);
        const defaultRotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(37));
        const defaultScale = new core_2.Vector3(8, 8, 8);
        const competedCallback = jest.fn();
        const data = {
            maxScale: 33,
            minScale: 1,
            position: defaultPosition.array,
            rotation: defaultRotation.toArray(),
            scale: defaultScale.x,
        };
        sceneRoot.applyDefaultData(data);
        const mockLerp = jest.fn();
        sceneRoot.transform.lerp = mockLerp;
        const linealEase = (val) => {
            return val;
        };
        const duration = 500;
        sceneRoot.lerpToDefault(duration, linealEase, competedCallback);
        const expectedDTO = {
            durationMS: 500,
            ease: linealEase,
            desiredPosition: defaultPosition,
            desiredRotation: defaultRotation,
            desiredScale: defaultScale,
            onCompletedCallback: competedCallback,
        };
        expect(mockLerp).toHaveBeenCalledWith(expectedDTO);
        expect(mockLerp).toHaveBeenCalledWith(expectedDTO);
    });
    it("Bumps the scale around the origin", () => {
        const { sceneRoot } = makeTestRig();
        expect(sceneRoot.position).toEqual(core_2.Vector3.Zero());
        expect(sceneRoot.scale).toEqual(1);
        sceneRoot.bumpScaleAboutPoint(1, core_2.Vector3.Zero());
        expect(sceneRoot.position).toEqual(core_2.Vector3.Zero());
        expect(sceneRoot.scale).toEqual(2);
    });
    it("Bumps the scale around a pick point", () => {
        const { sceneRoot } = makeTestRig();
        expect(sceneRoot.position).toEqual(core_2.Vector3.Zero());
        expect(sceneRoot.scale).toEqual(1);
        sceneRoot.bumpScaleAboutPoint(2, new core_2.Vector3(1, 2, 3));
        const expectPos = new core_2.Vector3(1 * -2, -2 * 2, 3 * -2);
        expect(sceneRoot.position).toEqual(expectPos);
    });
    it("Clamps the scale bump to the upper limit", () => {
        const { sceneRoot } = makeTestRig();
        const data = {
            maxScale: 33,
            minScale: 0.11,
            position: [1, 2, 3],
            rotation: [4, 5, 6, 7],
            scale: 8,
        };
        sceneRoot.applyDefaultData(data);
        expect(sceneRoot.scale).toEqual(1);
        sceneRoot.bumpScaleAboutPoint(1000, core_2.Vector3.Zero());
        expect(sceneRoot.scale).toEqual(33);
    });
    it("Clamps the scale bump to the lower limit", () => {
        const { sceneRoot } = makeTestRig();
        const data = {
            maxScale: 33,
            minScale: 0.11,
            position: [1, 2, 3],
            rotation: [4, 5, 6, 7],
            scale: 8,
        };
        sceneRoot.applyDefaultData(data);
        expect(sceneRoot.scale).toEqual(1);
        sceneRoot.bumpScaleAboutPoint(-1000, core_2.Vector3.Zero());
        expect(sceneRoot.scale).toEqual(0.11);
    });
    it("Gets the Entity", () => {
        const { sceneRoot, appObjects } = makeTestRig();
        expect(SceneRootEntity_1.SceneRootEntity.get(appObjects)).toEqual(sceneRoot);
    });
});
//# sourceMappingURL=SceneRootEntity.test.js.map