import { AppObjectPM, getSingletonComponent, } from "@vived/core";
import { Angle, Vector3 } from "@vived/core";
import { CameraEntity } from "../Entities/CameraEntity";
export const defaultCameraVM = {
    farClip: 0,
    fov: Angle.FromDegrees(60),
    nearClip: 0,
    position: Vector3.Zero(),
    target: Vector3.One(),
};
export class CameraPM extends AppObjectPM {
}
CameraPM.type = "CameraPM";
CameraPM.get = (appObjects) => getSingletonComponent(CameraPM.type, appObjects);
export function makeCameraPM(appObject) {
    return new CameraPMImp(appObject);
}
class CameraPMImp extends CameraPM {
    constructor(appObject) {
        super(appObject, CameraPM.type);
        this.onCameraChange = () => {
            if (!this.cameraEntity) {
                return;
            }
            const worldDirection = this.cameraEntity.transform.localDirectionToWorld(Vector3.Forward());
            const target = Vector3.Add(worldDirection, this.cameraEntity.transform.worldPosition);
            const vm = {
                position: this.cameraEntity.transform.worldPosition,
                nearClip: this.cameraEntity.nearClip,
                farClip: this.cameraEntity.farClip,
                fov: this.cameraEntity.fov,
                target,
            };
            this.doUpdateView(vm);
        };
        this.vmsAreEqual = (a, b) => {
            if (a.nearClip === b.nearClip &&
                a.farClip === b.farClip &&
                Vector3.Equal(a.position, b.position) &&
                Vector3.Equal(a.target, b.target) &&
                a.fov.radians === b.fov.radians) {
                return true;
            }
            return false;
        };
        this.dispose = () => {
            super.dispose();
            this.cameraEntity?.removeChangeObserver(this.onCameraChange);
            this.cameraEntity?.removeOnDisposeObserver(this.dispose);
        };
        this.cameraEntity = appObject.getComponent(CameraEntity.type);
        if (this.cameraEntity) {
            this.cameraEntity.addChangeObserver(this.onCameraChange);
            this.cameraEntity.addOnDisposeObserver(this.dispose);
            this.onCameraChange();
        }
        else {
            this.warn("App Object does not have a Camera Entity");
        }
    }
}
//# sourceMappingURL=CameraPM.js.map