import { AppObjectUC } from "@vived/core";
import { Vector2 } from "@vived/core";
import { FramedScreenContainer } from "../../FramedScreen";
import { ZSpaceEntity } from "../Entities/ZSpaceEntity";
export class EmulateDisplaySizeHook extends AppObjectUC {
}
EmulateDisplaySizeHook.type = "EmulateDisplaySizeHook";
export function makeEmulateDisplaySizeHook(appObject) {
    return new EmulateDisplaySizeHookImp(appObject);
}
class EmulateDisplaySizeHookImp extends EmulateDisplaySizeHook {
    constructor(appObject) {
        super(appObject, EmulateDisplaySizeHook.type);
        this.onUpdate = () => {
            if (!this.container || !this.zSpaceEntity)
                return;
            if (!this.zSpaceEntity.enabled)
                return;
            if (!this.zSpaceEntity.emulate)
                return;
            const display = this.zSpaceEntity.display;
            display.displaySizePixels = this.container.size;
            const aspect = this.container.size.y / this.container.size.x;
            // TODO replace is Camera Entity stuff. Hardcoding for now
            const fovRadians = 0.26;
            const distance = 0.59; // Hmm. Had to bump this to make it line up.
            if (this.container.size.x > this.container.size.y) {
                const displayHeight = distance * Math.tan(fovRadians);
                const displayWidth = displayHeight / aspect;
                display.displaySizeMeters = new Vector2(displayWidth, displayHeight);
            }
            else {
                const displayWidth = distance * Math.tan(fovRadians);
                const displayHeight = displayWidth * aspect;
                display.displaySizeMeters = new Vector2(displayWidth, displayHeight);
            }
        };
        this.zSpaceEntity = ZSpaceEntity.get(appObject.appObjectRepo);
        if (!this.zSpaceEntity) {
            this.warn("Unable to find the zSpace Entity");
            return;
        }
        this.container = appObject.getComponent(FramedScreenContainer.type);
        if (!this.container) {
            this.warn("EmulateDisplaySizeHook has been added to an app object without a FramedScreenContainer");
            return;
        }
        this.zSpaceEntity.addChangeObserver(this.onUpdate);
        this.container.addChangeObserver(this.onUpdate);
        this.onUpdate();
    }
}
//# sourceMappingURL=EmulateDisplaySizeHook.js.map