import { connectionStateObserver } from '@cc-livekit/components-core';
import type { Room } from '@cc-livekit/livekit-client';
import * as React from 'react';
import { useEnsureRoom } from '../context';
import { useObservableState } from './internal';

/**
 * The `useConnectionState` hook allows you to simply implement your own `ConnectionState` component.
 *
 * @example
 * ```tsx
 * const connectionState = useConnectionState(room);
 * ```
 * @public
 */
export function useConnectionState(room?: Room) {
  // passed room takes precedence, if not supplied get current room context
  const r = useEnsureRoom(room);
  const observable = React.useMemo(() => connectionStateObserver(r), [r]);
  const connectionState = useObservableState(observable, r.state);
  return connectionState;
}
