import { useMemo } from 'react';
import MenuItem from '../../MenuItem/index.js';
import { VerticalMenuProps } from '../types.js';
import { addIdsToMenuItems } from '../../utils/transformMenuData.js';
import styles from './index.module.scss';
import cx from '../../../utils/classname.js';
const VerticalMenu = ({
  maxDepth,
  className = '',
  style = {},
  linkColor,
  linkColorHover,
  backgroundColor,
  backgroundColorHover,
  submenuLinkColor,
  submenuLinkColorHover,
  submenuBackgroundColor,
  submenuBackgroundColorHover,
  indentation,
  menuData,
}: VerticalMenuProps) => {
  if (!menuData?.pagesTree?.children) return null;

  const menuItems = useMemo(
    () => addIdsToMenuItems(menuData.pagesTree.children),
    [menuData]
  );
  const level = 1;

  const inlineStyles: React.CSSProperties &
    Record<`--hs-${string}`, string> = {
    ...(linkColor && { '--hs-verticalMenu-color': linkColor }),
    ...(linkColorHover && {
      '--hs-verticalMenu-color-hover': linkColorHover,
    }),
    ...(backgroundColor && {
      '--hs-verticalMenu-backgroundColor': backgroundColor,
    }),
    ...(backgroundColorHover && {
      '--hs-verticalMenu-backgroundColor-hover': backgroundColorHover,
    }),
    ...(submenuLinkColor && {
      '--hs-verticalMenu-submenu-color': submenuLinkColor,
    }),
    ...(submenuLinkColorHover && {
      '--hs-verticalMenu-submenu-color-hover': submenuLinkColorHover,
    }),
    ...(submenuBackgroundColor && {
      '--hs-verticalMenu-submenu-backgroundColor': submenuBackgroundColor,
    }),
    ...(submenuBackgroundColorHover && {
      '--hs-verticalMenu-submenu-backgroundColor-hover':
        submenuBackgroundColorHover,
    }),
    ...(indentation && {
      '--hs-verticalMenu-paddingInlineStart': indentation,
    }),
    ...style,
  };

  return (
    <nav
      className={cx(styles.verticalMenu, className)}
      style={inlineStyles}
    >
      <ul className={styles.menuList}>
        {menuItems.map(item => (
          <MenuItem
            key={item._id}
            item={item}
            level={level}
            maxDepth={maxDepth}
          />
        ))}
      </ul>
    </nav>
  );
};

export default VerticalMenu;
