import { useCallback } from 'react'

import isEmpty from 'lodash/isEmpty'
import compact from 'lodash/compact'
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) => {
    const lexics = compact(values(playlist.lexics))
    if (!isEmpty(lexics)) {
      addLexicsConfig(lexics)
    }
    return playlist
  }, [addLexicsConfig])

  return { fetchLexics }
}
