import { useCallback, useContext } from 'react'
import { ApiServerContext } from '../context/ApiServerContext'

const useCollapseState = (componentName, defaultState = {}) => {
  const { userSettings, updateUserSettings } = useContext(ApiServerContext)
  const collapseState =
    userSettings.collapseState[componentName] ?? defaultState

  const updateCollapseState = useCallback(
    (key, value) => {
      updateUserSettings('collapseState', componentName, {
        ...collapseState,
        [key]: value
      })
    },
    [collapseState, componentName, updateUserSettings]
  )

  return [collapseState, updateCollapseState]
}

export default useCollapseState
