'use client';

/**
 * useVideoPlayerSettings - Hook for persisted video player settings
 *
 * Provides volume and loop settings that persist in localStorage.
 * Use this hook in video player providers to apply saved settings.
 *
 * @example
 * const { settings, updateVolume, updateLoop } = useVideoPlayerSettings();
 *
 * // Apply to video element
 * videoRef.current.volume = settings.volume;
 * videoRef.current.loop = settings.isLooping;
 *
 * // Save when user changes
 * const handleVolumeChange = (vol: number) => {
 *   videoRef.current.volume = vol;
 *   updateVolume(vol);
 * };
 */

import { useCallback } from 'react';

import { useVideoPlayerSettings as useSettings } from '../../../stores/mediaCache';

export interface VideoPlayerSettingsReturn {
  /** Current settings */
  settings: {
    volume: number;
    isLooping: boolean;
  };
  /** Update volume (0-1) */
  updateVolume: (volume: number) => void;
  /** Update loop setting */
  updateLoop: (isLooping: boolean) => void;
  /** Update multiple settings at once */
  updateSettings: (settings: { volume?: number; isLooping?: boolean }) => void;
}

export function useVideoPlayerSettings(): VideoPlayerSettingsReturn {
  const { settings, saveSettings } = useSettings();

  const updateVolume = useCallback(
    (volume: number) => {
      saveSettings({ volume: Math.max(0, Math.min(1, volume)) });
    },
    [saveSettings]
  );

  const updateLoop = useCallback(
    (isLooping: boolean) => {
      saveSettings({ isLooping });
    },
    [saveSettings]
  );

  const updateSettings = useCallback(
    (newSettings: { volume?: number; isLooping?: boolean }) => {
      saveSettings(newSettings);
    },
    [saveSettings]
  );

  return {
    settings,
    updateVolume,
    updateLoop,
    updateSettings,
  };
}
