// components/HorizontalMenu.tsx
'use client'

import { useTheme } from '@mui/material/styles'
import HorizontalNav, { Menu, MenuItem } from '@menu/horizontal-menu'
import VerticalNavContent from './VerticalNavContent'
import { useLocale } from 'next-intl'
import type { VerticalMenuContextProps } from '@menu/components/vertical-menu/Menu'
import useVerticalNav from '@menu/hooks/useVerticalNav'
import StyledHorizontalNavExpandIcon from '@menu/styles/horizontal/StyledHorizontalNavExpandIcon'
import StyledVerticalNavExpandIcon from '@menu/styles/vertical/StyledVerticalNavExpandIcon'
import menuItemStyles from '@core/styles/horizontal/menuItemStyles'
import menuRootStyles from '@core/styles/horizontal/menuRootStyles'
import verticalNavigationCustomStyles from '@core/styles/vertical/navigationCustomStyles'
import verticalMenuItemStyles from '@core/styles/vertical/menuItemStyles'
import verticalMenuSectionStyles from '@core/styles/vertical/menuSectionStyles'
import { useTranslations } from 'next-intl'

const RenderExpandIcon = ({ level }: { level?: number }) => (
  <StyledHorizontalNavExpandIcon level={level}>
    <i className='tabler-chevron-right' />
  </StyledHorizontalNavExpandIcon>
)

const RenderVerticalExpandIcon = ({
  open,
  transitionDuration
}: {
  open?: boolean
  transitionDuration?: VerticalMenuContextProps['transitionDuration']
}) => (
  <StyledVerticalNavExpandIcon open={open} transitionDuration={transitionDuration}>
    <i className='tabler-chevron-right' />
  </StyledVerticalNavExpandIcon>
)

const HorizontalMenu = () => {
  const verticalNavOptions = useVerticalNav()
  const theme = useTheme()
  const { transitionDuration } = verticalNavOptions
  const locale = useLocale()
  const t = useTranslations('MENU_ITEMS')

  const menuItems = [
    {
      href: `/${locale}`,
      icon: <i className='tabler-smart-home' />,
      label: t('HOME')
    },
    {
      href: `/${locale}/payment-links`,
      icon: <i className='tabler-link' />,
      label: t('PAYMENT_LINKS')
    }
  ]

  return (
    <HorizontalNav
      switchToVertical
      verticalNavContent={VerticalNavContent}
      verticalNavProps={{
        customStyles: verticalNavigationCustomStyles(verticalNavOptions, theme),
        backgroundColor: 'var(--mui-palette-background-paper)'
      }}
    >
      <Menu
        rootStyles={menuRootStyles(theme)}
        renderExpandIcon={({ level }) => <RenderExpandIcon level={level} />}
        menuItemStyles={menuItemStyles(theme, 'tabler-circle')}
        renderExpandedMenuItemIcon={{ icon: <i className='tabler-circle text-xs' /> }}
        popoutMenuOffset={{
          mainAxis: ({ level }) => (level && level > 0 ? 14 : 12),
          alignmentAxis: 0
        }}
        verticalMenuProps={{
          menuItemStyles: verticalMenuItemStyles(verticalNavOptions, theme),
          renderExpandIcon: ({ open }) => (
            <RenderVerticalExpandIcon open={open} transitionDuration={transitionDuration} />
          ),
          renderExpandedMenuItemIcon: { icon: <i className='tabler-circle text-xs' /> },
          menuSectionStyles: verticalMenuSectionStyles(verticalNavOptions, theme)
        }}
      >
        {menuItems.map(item => (
          <MenuItem key={item.href} href={item.href} icon={item.icon}>
            {item.label}
          </MenuItem>
        ))}
      </Menu>
    </HorizontalNav>
  )
}

export default HorizontalMenu
