/**
 * VideoPlayerContext - Context for streaming configuration
 * Simplifies streaming API by providing getStreamUrl globally
 */

'use client';

import React, { createContext, useContext, useMemo } from 'react';

import type { VideoPlayerContextValue, VideoPlayerProviderProps } from '../types';

// =============================================================================
// Context
// =============================================================================

const VideoPlayerContext = createContext<VideoPlayerContextValue | null>(null);

/**
 * Provider for VideoPlayer streaming configuration
 *
 * @example
 * // In your app layout or FileWorkspace
 * <VideoPlayerProvider
 *   sessionId={sessionId}
 *   getStreamUrl={terminalClient.terminal_media.streamStreamRetrieveUrl}
 * >
 *   <VideoPlayer source={{ type: 'stream', path: '/video.mp4' }} />
 * </VideoPlayerProvider>
 */
export function VideoPlayerProvider({
  children,
  getStreamUrl,
  sessionId,
}: VideoPlayerProviderProps) {
  const value = useMemo(
    () => ({ getStreamUrl, sessionId }),
    [getStreamUrl, sessionId]
  );

  return (
    <VideoPlayerContext.Provider value={value}>
      {children}
    </VideoPlayerContext.Provider>
  );
}

/**
 * Hook to access VideoPlayer context
 */
export function useVideoPlayerContext(): VideoPlayerContextValue | null {
  return useContext(VideoPlayerContext);
}
