/**
 * DEVKIT entry point. The whole devkit is gated by this single flag.
 *
 * Activation: append `?devkit=1` to URL OR run in console:
 *   localStorage.setItem('devkit', '1')
 * Deactivation: `?devkit=0` OR localStorage.removeItem('devkit')
 *
 * To remove DevKit from the project entirely:
 *   1. rm -rf src/devkit/
 *   2. Strip `// DEVKIT:` blocks in XRSession.tsx & useControllerGrab.ts
 */
import { useEffect, useState } from 'react';

const KEY = 'devkit';

function readFlag(): boolean {
  if (typeof window === 'undefined') return false;
  const params = new URLSearchParams(window.location.search);
  const urlVal = params.get(KEY);
  if (urlVal === '1') {
    localStorage.setItem(KEY, '1');
    return true;
  }
  if (urlVal === '0') {
    localStorage.removeItem(KEY);
    return false;
  }
  return localStorage.getItem(KEY) === '1';
}

export function useDevKit(): boolean {
  const [enabled, setEnabled] = useState(readFlag);
  useEffect(() => {
    const onStorage = () => setEnabled(readFlag());
    window.addEventListener('storage', onStorage);
    return () => window.removeEventListener('storage', onStorage);
  }, []);
  return enabled;
}

export function isDevKit(): boolean {
  return readFlag();
}
