/**
 * DEVKIT — global mutable store for fake controller inputs.
 *
 * Lives outside React (plain object + listeners) so useControllerGrab can
 * read it from inside useFrame with zero re-render cost. Mirrors the shape
 * of a real WebXR XRInputSource just enough to feed our grab logic.
 */
import * as THREE from 'three';

export interface FakeHandState {
  gripValue: number;     // 0..1
  triggerValue: number;  // 0..1
  hasPose: boolean;      // true if controller is "tracked"
  gripWorld: THREE.Matrix4; // world pose of the gripSpace
}

export interface FakeInputState {
  active: boolean;
  left: FakeHandState;
  right: FakeHandState;
}

function makeHand(): FakeHandState {
  return {
    gripValue: 0,
    triggerValue: 0,
    hasPose: true,
    gripWorld: new THREE.Matrix4(),
  };
}

export const fakeInput: FakeInputState = {
  active: false,
  left: makeHand(),
  right: makeHand(),
};

// Initial poses — a bit in front of the camera, hands apart at chest height
fakeInput.left.gripWorld.makeTranslation(-0.25, 1.2, -0.5);
fakeInput.right.gripWorld.makeTranslation(0.25, 1.2, -0.5);

export function setFakeActive(on: boolean) {
  fakeInput.active = on;
}

export function isFakeActive(): boolean {
  return fakeInput.active;
}
