import { useCallback } from 'react'

import isEmpty from 'lodash/isEmpty'
import map from 'lodash/map'
import uniq from 'lodash/uniq'

import type { Events } from 'requests'

import { useLexicsStore } from 'features/LexicsStore'

export const useEventsLexics = () => {
  const { addLexicsConfig } = useLexicsStore()

  const fetchLexics = useCallback((events: Events) => {
    const lexics = uniq(map(events, ({ l }) => l))

    if (!isEmpty(lexics)) {
      addLexicsConfig(lexics)
    }

    return events
  }, [addLexicsConfig])

  return { fetchLexics }
}
