import { useCallback, useState } from 'react'

import map from 'lodash/map'

import { SportTypes } from 'config'

import type { Actions } from 'requests'
import { getSportActions } from 'requests'

import { useLexicsStore } from 'features/LexicsStore'

export const useSportActions = (sportType?: SportTypes) => {
  const [actions, setActions] = useState<Actions>([])
  const { addLexicsConfig } = useLexicsStore()

  const fetchSportActions = useCallback(() => {
    if (!sportType) return

    getSportActions(sportType).then((sportActions) => {
      setActions(sportActions)
      addLexicsConfig(map(sportActions, ({ lexic }) => lexic))
    })
  }, [sportType, addLexicsConfig])

  return { actions, fetchSportActions }
}
