import { useCallback, useState } from 'react'

import type { UseMenuExpansionReturn } from '../types'

export function useMenuExpansion(initialMenus: string[] = ['analytics']): UseMenuExpansionReturn {
  const [expandedMenus, setExpandedMenus] = useState<string[]>(initialMenus)

  const toggleMenu = useCallback((menuId: string) => {
    setExpandedMenus(prev =>
      prev.includes(menuId)
        ? prev.filter(id => id !== menuId)
        : [...prev, menuId],
    )
  }, [])

  const expandMenu = useCallback((menuId: string) => {
    setExpandedMenus(prev =>
      prev.includes(menuId) ? prev : [...prev, menuId],
    )
  }, [])

  const collapseMenu = useCallback((menuId: string) => {
    setExpandedMenus(prev => prev.filter(id => id !== menuId))
  }, [])

  return {
    expandedMenus,
    toggleMenu,
    expandMenu,
    collapseMenu,
  }
}