/**
 * `useLiveConnectionState` — ticket #13, lot F6. Web-only hook exposing the shared
 * live WebSocket's current {@link ConnectionState} to the UI so a reconnection
 * banner can be shown even when no terminal is open (the F3 xterm notice only
 * covers terminal cells; live-only surfaces had no visible signal).
 *
 * Inert outside web transport (no client registered), so it is safe to mount
 * unconditionally: it returns `null` on desktop.
 */

import { useEffect, useState } from "react";

import { getWebLiveClient, type ConnectionState } from "@/adapters/http";

export function useLiveConnectionState(): ConnectionState | null {
  const [state, setState] = useState<ConnectionState | null>(
    () => getWebLiveClient()?.getConnectionState() ?? null,
  );

  useEffect(() => {
    const client = getWebLiveClient();
    if (!client) return;
    // Sync in case the state changed between the initial render and this effect.
    setState(client.getConnectionState());
    return client.onConnectionStateChange(setState);
  }, []);

  return state;
}
