"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViewFromPoseHook = void 0;
exports.makeViewFromPoseHook = makeViewFromPoseHook;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const XR_1 = require("../../XR");
const ZSpaceEntity_1 = require("../Entities/ZSpaceEntity");
class ViewFromPoseHook extends core_1.AppObjectUC {
}
exports.ViewFromPoseHook = ViewFromPoseHook;
ViewFromPoseHook.type = "ViewFromPoseHook";
function makeViewFromPoseHook(appObj) {
    return new ViewFromPoseImp(appObj);
}
class ViewFromPoseImp extends ViewFromPoseHook {
    constructor(appObject) {
        super(appObject, ViewFromPoseHook.type);
        this.onNewXRFrame = () => {
            const frame = this.xrEntity.lastXRFrame;
            if (!frame)
                return;
            if (!this.xrEntity.xrReferenceSpace) {
                console.log("No reference space");
                return;
            }
            const pose = frame.getViewerPose(this.xrEntity.xrReferenceSpace);
            if (!pose) {
                console.log("No pose");
                return;
            }
            if (!pose.transform) {
                console.log("No Pose transform ");
                return;
            }
            const orientation = pose.transform.orientation;
            if (pose.transform.orientation.x === undefined) {
                console.log("Pose Orientation in undefined");
                return;
            }
            const pos = pose.transform.position;
            const headPos = new core_2.Vector3(pos.x, pos.y, -pos.z);
            const headRotation = new core_2.Quaternion(orientation.x, orientation.y, -orientation.z, -orientation.w);
            pose.views.forEach((view, i) => {
                const viewDTO = this.formViewDTO(view);
                if (view.eye === "right") {
                    this.zSpace.rightEyeView.update(viewDTO);
                }
                else {
                    this.zSpace.leftEyeView.update(viewDTO);
                    const headDTO = { ...viewDTO };
                    headDTO.position = headPos;
                    headDTO.rotation = headRotation;
                    this.zSpace.headView.update(headDTO);
                }
            });
        };
        this.formViewDTO = (view) => {
            const xrProjMatrix = core_2.Matrix.FromFloat32Array(view.projectionMatrix);
            const projectionMatrix = core_2.Matrix.FlipProjectionMatrixHand(xrProjMatrix);
            const xrPosition = view.transform.position;
            const xrOrientation = view.transform.orientation;
            const position = new core_2.Vector3(xrPosition.x, xrPosition.y, -xrPosition.z);
            const rotation = new core_2.Quaternion(xrOrientation.x, xrOrientation.y, -xrOrientation.z, -xrOrientation.w);
            return {
                position,
                rotation,
                projectionMatrix,
                view,
            };
        };
        const zSpaceEntity = appObject.getComponent(ZSpaceEntity_1.ZSpaceEntity.type);
        if (!zSpaceEntity) {
            throw new Error("No ZSpace Entity");
        }
        this.zSpace = zSpaceEntity;
        const xrEntity = zSpaceEntity.appObject.getComponent(XR_1.XREntity.type);
        if (!xrEntity) {
            throw new Error("No XR Entity");
        }
        this.xrEntity = xrEntity;
        this.xrEntity.addChangeObserver(this.onNewXRFrame);
    }
}
//# sourceMappingURL=ViewFromPoseHook.js.map