/**
 * `useLiveReconnect` — ticket #13, lot F5. Web-only hook that re-synchronises a
 * live read-model when the shared WebSocket **reconnects** after an outage.
 *
 * During a disconnection, domain events emitted by the server are missed, so a
 * plain event-driven refresh (the desktop `useProjectWorkState` mechanism) would
 * leave the UI stale until the next event. This hook watches the web live
 * client's connection state and calls `onReconnect` on each `reconnecting →
 * connected` transition. It is inert outside web transport (no client
 * registered), so it is safe to mount unconditionally.
 */

import { useEffect } from "react";

import { getWebLiveClient } from "@/adapters/http";

export function useLiveReconnect(onReconnect: () => void): void {
  useEffect(() => {
    const client = getWebLiveClient();
    if (!client) return;
    let previous = client.getConnectionState();
    return client.onConnectionStateChange((state) => {
      if (state === "connected" && previous === "reconnecting") onReconnect();
      previous = state;
    });
  }, [onReconnect]);
}
