"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CameraPM = exports.defaultCameraVM = void 0;
exports.makeCameraPM = makeCameraPM;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const CameraEntity_1 = require("../Entities/CameraEntity");
exports.defaultCameraVM = {
    farClip: 0,
    fov: core_2.Angle.FromDegrees(60),
    nearClip: 0,
    position: core_2.Vector3.Zero(),
    target: core_2.Vector3.One(),
};
class CameraPM extends core_1.AppObjectPM {
}
exports.CameraPM = CameraPM;
CameraPM.type = "CameraPM";
CameraPM.get = (appObjects) => (0, core_1.getSingletonComponent)(CameraPM.type, appObjects);
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(core_2.Vector3.Forward());
            const target = core_2.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 &&
                core_2.Vector3.Equal(a.position, b.position) &&
                core_2.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_1.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