/**
 * HyperTwist Browser Runtime Entry Point
 * 
 * Exports all bundled browser-capable libraries for use in the embedded
 * WebBrowser widget or external browser runtime.
 * 
 * Phase 1 wiring targets:
 * - 1G: tentone/rubix-solver (vision + solver UI)
 * - 1H: pmndrs spatial stack (postprocessing, drei, fiber, etc.)
 * - 1I: ecomfe analytics stack (zrender, echarts-gl)
 * - 1J: google/model-viewer sub-packages
 */

// Three.js core (peer dependency for most spatial packages)
import * as THREE from 'three';

// pmndrs spatial stack (1H)
export { THREE };
export { default as postprocessing } from '@pmndrs/postprocessing';
export { default as drei } from '@react-three/drei';
export { Canvas, useFrame, useThree } from '@react-three/fiber';
export { default as leva } from 'leva';
export { default as maath } from 'maath';
export { default as threeStdlib } from 'three-stdlib';
export { create as createZustandStore } from 'zustand';
export { useGesture } from '@use-gesture/react';
export { useSpring, animated } from '@react-spring/three';

// Analytics stack (1I)
export { default as zrender } from 'zrender';
export { default as echarts } from 'echarts';
export { default as echartsGl } from 'echarts-gl';

// Model viewer (1J)
export { default as ModelViewerElement } from '@google/model-viewer';

// claygl / clay-viewer (1H)
export { default as claygl } from 'claygl';

// rubix-solver (1G)
// Note: tentone solver is primarily a C++/OpenCV desktop app.
// The JS bundle exports a placeholder for future WASM or web port.
export const RubixSolver = {
  version: '1.0.0',
  note: 'C++ solver compiled as standalone executable. Web port TBD.'
};

// UE Bridge: postMessage API for state synchronization
interface UEStateBridge {
  sendState(state: object): void;
  onCommand(callback: (cmd: object) => void): void;
}

export const UEBridge: UEStateBridge = {
  sendState(state: object) {
    if ((window as any).ue && (window as any).ue.hypertwist) {
      (window as any).ue.hypertwist.sendState(JSON.stringify(state));
    } else {
      window.parent.postMessage({ type: 'hypertwist-state', payload: state }, '*');
    }
  },
  onCommand(callback: (cmd: object) => void) {
    window.addEventListener('message', (event) => {
      if (event.data && event.data.type === 'hypertwist-command') {
        callback(event.data.payload);
      }
    });
  }
};

console.log('[HyperTwist Browser Runtime] Loaded');
