import { jsxs, Fragment, jsx } from "react/jsx-runtime";
import { useRef } from "react";
import { useXRInputSourceState, useXR } from "@react-three/xr";
import { useThree, useFrame } from "@react-three/fiber";
function getHandJoint(hand, jointName) {
  return hand.get(jointName);
}
function HandBbox() {
  const rightHand = useXRInputSourceState("hand", "right");
  const leftHand = useXRInputSourceState("hand", "left");
  const rightTipRef = useRef(null);
  const leftTipRef = useRef(null);
  const { gl } = useThree();
  useFrame((_state, _delta, frame) => {
    if (!frame || !gl.xr.isPresenting)
      return;
    const refSpace = gl.xr.getReferenceSpace();
    if (!refSpace)
      return;
    if (rightHand?.inputSource?.hand && rightTipRef.current) {
      const jointSpace = getHandJoint(rightHand.inputSource.hand, "index-finger-tip");
      if (jointSpace) {
        const pose = frame.getJointPose?.(jointSpace, refSpace);
        if (pose) {
          rightTipRef.current.position.set(pose.transform.position.x, pose.transform.position.y, pose.transform.position.z);
        }
      }
    }
    if (leftHand?.inputSource?.hand && leftTipRef.current) {
      const jointSpace = getHandJoint(leftHand.inputSource.hand, "index-finger-tip");
      if (jointSpace) {
        const pose = frame.getJointPose?.(jointSpace, refSpace);
        if (pose) {
          leftTipRef.current.position.set(pose.transform.position.x, pose.transform.position.y, pose.transform.position.z);
        }
      }
    }
  });
  return jsxs(Fragment, { children: [jsxs("mesh", { name: "leftTipBbox", ref: leftTipRef, children: [jsx("boxGeometry", { args: [0.02, 0.02, 0.02] }), jsx("meshStandardMaterial", { color: "blue", transparent: true, opacity: 0 })] }), jsxs("mesh", { name: "rightTipBbox", ref: rightTipRef, children: [jsx("boxGeometry", { args: [0.02, 0.02, 0.02] }), jsx("meshStandardMaterial", { color: "orange", transparent: true, opacity: 0 })] })] });
}
function HandDecorate() {
  const session = useXR((state) => state.session);
  const { scene } = useThree();
  useFrame(() => {
    if (!session)
      return;
    scene.traverse((child) => {
      if (child.isMesh && child.material && child.userData?.xpiHand) {
        const mesh = child;
        mesh.material.transparent = true;
        mesh.material.opacity = 0.5;
      }
    });
  });
  return null;
}
function XRSceneComponents() {
  return jsxs(Fragment, { children: [jsx(HandBbox, {}), jsx(HandDecorate, {})] });
}
export {
  XRSceneComponents as default
};
