"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const CameraEntity_1 = require("../Entities/CameraEntity");
const CameraPM_1 = require("./CameraPM");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const appObject = appObjects.getOrCreate("AO");
    const camera = (0, CameraEntity_1.makeCameraEntity)(appObject);
    camera.farClip = 10;
    camera.nearClip = 0.01;
    camera.fov = core_2.Angle.FromRadians(0.26);
    camera.transform.localPosition = core_2.Vector3.Zero();
    const pm = (0, CameraPM_1.makeCameraPM)(appObject);
    const observer = jest.fn();
    pm.addView(observer);
    return {
        camera,
        pm,
        observer,
        appObject,
        appObjects,
    };
}
describe("CameraPM Test", () => {
    it("Sets the last vm on construction", () => {
        const { pm } = makeTestRig();
        expect(pm.lastVM).not.toBeUndefined();
        expect(pm.lastVM?.nearClip).toEqual(0.01);
        expect(pm.lastVM?.farClip).toEqual(10);
        expect(pm.lastVM?.fov).toEqual(core_2.Angle.FromRadians(0.26));
        expect(pm.lastVM?.position).toEqual(core_2.Vector3.Zero());
        expect(pm.lastVM?.target).toEqual(core_2.Vector3.Forward());
    });
    it("Calculates the direction from the rotation of the transform", () => {
        const { pm, camera } = makeTestRig();
        camera.transform.localRotation = core_2.Quaternion.FromYawPitchRoll(core_2.Angle.FromDegrees(180), core_2.Angle.FromDegrees(0), core_2.Angle.FromDegrees(0));
        expect(core_2.Vector3.Close(pm.lastVM.target, core_2.Vector3.Backward())).toEqual(true);
    });
    it("Updates the view when camera changes", () => {
        const { observer, camera } = makeTestRig();
        camera.fov = core_2.Angle.FromRadians(0.5);
        observer.mockClear();
        camera.fov = core_2.Angle.FromRadians(1.5);
        expect(observer).toHaveBeenCalled();
    });
    it("Does not update when disposed", () => {
        const { observer, camera } = makeTestRig();
        camera.fov = core_2.Angle.FromRadians(0.5);
        observer.mockClear();
        camera.dispose();
        camera.fov = core_2.Angle.FromRadians(1.5);
        expect(observer).not.toHaveBeenCalled();
    });
    it("Disposed when entity disposed", () => {
        const { camera, appObject } = makeTestRig();
        expect(appObject.hasComponent(CameraPM_1.CameraPM.type)).toEqual(true);
        camera.dispose();
        expect(appObject.hasComponent(CameraPM_1.CameraPM.type)).toEqual(false);
    });
    it("Returns true if the VMs are the same", () => {
        const { pm } = makeTestRig();
        const vm1 = {
            nearClip: 0.01,
            farClip: 10,
            target: core_2.Vector3.Forward(),
            position: core_2.Vector3.Zero(),
            fov: core_2.Angle.FromRadians(0.26),
        };
        const vm2 = { ...vm1 };
        expect(pm.vmsAreEqual(vm1, vm2)).toEqual(true);
    });
    it("Returns false if nearClips are different", () => {
        const { pm } = makeTestRig();
        const vm1 = {
            nearClip: 0.01,
            farClip: 10,
            target: core_2.Vector3.Forward(),
            position: core_2.Vector3.Zero(),
            fov: core_2.Angle.FromRadians(0.26),
        };
        const vm2 = { ...vm1, nearClip: 0 };
        expect(pm.vmsAreEqual(vm1, vm2)).toEqual(false);
    });
    it("Returns false if farClips are different", () => {
        const { pm } = makeTestRig();
        const vm1 = {
            nearClip: 0.01,
            farClip: 10,
            target: core_2.Vector3.Forward(),
            position: core_2.Vector3.Zero(),
            fov: core_2.Angle.FromRadians(0.26),
        };
        const vm2 = { ...vm1, farClip: 0 };
        expect(pm.vmsAreEqual(vm1, vm2)).toEqual(false);
    });
    it("Returns false if targets are different", () => {
        const { pm } = makeTestRig();
        const vm1 = {
            nearClip: 0.01,
            farClip: 10,
            target: core_2.Vector3.Forward(),
            position: core_2.Vector3.Zero(),
            fov: core_2.Angle.FromRadians(0.26),
        };
        const vm2 = { ...vm1, target: new core_2.Vector3(1, 2, 3) };
        expect(pm.vmsAreEqual(vm1, vm2)).toEqual(false);
    });
    it("Returns false if positions are different", () => {
        const { pm } = makeTestRig();
        const vm1 = {
            nearClip: 0.01,
            farClip: 10,
            target: core_2.Vector3.Forward(),
            position: core_2.Vector3.Zero(),
            fov: core_2.Angle.FromRadians(0.26),
        };
        const vm2 = { ...vm1, position: new core_2.Vector3(1, 2, 3) };
        expect(pm.vmsAreEqual(vm1, vm2)).toEqual(false);
    });
    it("Returns false if angles are different", () => {
        const { pm } = makeTestRig();
        const vm1 = {
            nearClip: 0.01,
            farClip: 10,
            target: core_2.Vector3.Forward(),
            position: core_2.Vector3.Zero(),
            fov: core_2.Angle.FromRadians(0.26),
        };
        const vm2 = { ...vm1, fov: core_2.Angle.FromRadians(0) };
        expect(pm.vmsAreEqual(vm1, vm2)).toEqual(false);
        expect(pm.vmsAreEqual(vm1, vm2)).toEqual(false);
    });
    it("Gets the PM", () => {
        const { pm, appObjects } = makeTestRig();
        expect(CameraPM_1.CameraPM.get(appObjects)).toEqual(pm);
    });
});
//# sourceMappingURL=CameraPM.test.js.map