import { useStores } from '@/app/(root)/AppProviders';
import { ContextType } from '@/logging/contextTypes';

export default function usePlaybar() {
  const { playbar } = useStores();
  return playbar;
}

/**
 * Determines whether a given clip ID is the song that is currently playing
 *
 * If the playbar context matters, you can pass a context type and ID
 */
export function usePlaybarStatusForClip(
  clipId: string,
  playbarContext?: {
    contextId?: string;
    contextType?: ContextType;
  }
) {
  const { playbar, queue } = useStores();

  const isCurrentSongId = playbar.clip?.id === clipId;
  const isCurrentContext = playbarContext
    ? queue.contextType === playbarContext.contextType &&
      queue.contextId === playbarContext.contextId
    : undefined;
  // If we don't know the context, only check against clip ID
  const isCurrentSong = (isCurrentContext ?? true) && isCurrentSongId;

  return {
    isCurrentSong,
    isPlaying: !!isCurrentSong && playbar.isPlaying,
    isCurrentSongId,
    isCurrentContext,
  };
}
