import { useState, useCallback } from 'react'

import isEmpty from 'lodash/isEmpty'

import { getObjectConfig } from 'features/LexicsStore/helpers'

import type { LexicsConfig, LexicsId } from '../types'

export const useLexicsConfig = () => {
  const [lexicsConfig, setLexicsConfig] = useState<LexicsConfig>({})

  const addLexicsConfig = useCallback(
    (ids: Array<LexicsId> | LexicsConfig) => {
      if (isEmpty(ids)) return

      const config = getObjectConfig(ids)
      setLexicsConfig((state) => ({ ...state, ...config }))
    },
    [],
  )

  return { addLexicsConfig, lexicsConfig }
}
