import { useCallback } from 'react'

import isEmpty from 'lodash/isEmpty'
import values from 'lodash/values'

import type { MatchPlaylists } from 'requests'

import { useLexicsStore } from 'features/LexicsStore'

export const usePlaylistLexics = () => {
  const { addLexicsConfig } = useLexicsStore()
  const fetchLexics = useCallback((playlist: MatchPlaylists | null) => {
    const lexics = values(playlist?.lexics)
    if (!isEmpty(lexics)) {
      addLexicsConfig(lexics)
    }
    return playlist
  }, [addLexicsConfig])

  return { fetchLexics }
}
