import React, { forwardRef, useEffect } from 'react';
import isEqual from 'lodash-es/isEqual';
import classNames from 'classnames';
import DropdownItemContent from './dropdown-item-content';
import { MENU_ROLE } from './types';
import { useTreeNodeNavigation } from '../hooks';
import { jsx as _jsx } from "react/jsx-runtime";
const DropdownItem = /*#__PURE__*/forwardRef(({
  option,
  selectedKey,
  onCloseDropdown,
  onSelect,
  onMouseEnter = () => {},
  onMouseLeave = () => {},
  isMultiLevelHandler,
  LinkComponent
}, ref) => {
  const {
    disabled,
    isShown,
    key,
    className
  } = option;
  const {
    node,
    setFocused,
    isFirstFocusable,
    isDisabled
  } = useTreeNodeNavigation({
    id: option.privateKey || option.key
  });
  useEffect(() => {
    if (selectedKey && isEqual(key, selectedKey) || isFirstFocusable) {
      setFocused(node.id);
    }
  }, [key, selectedKey, isFirstFocusable]);
  const isValidKey = selectedKey !== null && selectedKey !== undefined;
  const isActive = isValidKey && key === Math.floor(selectedKey) || selectedKey && isEqual(key, selectedKey);
  const testId = option.testId ? `menu-item-${option.testId}` : null;
  const elementId = key === undefined ? option.privateKey : key;
  if (isShown === undefined || isShown) {
    return /*#__PURE__*/_jsx("li", {
      ref: ref,
      className: classNames(className, {
        disabled,
        active: isActive,
        focused: !isDisabled && !option.radioOptions?.length && node.isFocused,
        'drop-item': !option.radioOptions?.length
      }),
      role: option.isFileUpload || option.isFolderUpload ? MENU_ROLE.MENUITEM : 'none',
      "test-id": testId,
      id: `dropdown-item-${elementId}`,
      children: /*#__PURE__*/_jsx(DropdownItemContent, {
        option: option,
        onMouseEnter: onMouseEnter,
        onMouseLeave: onMouseLeave,
        onCloseDropdown: onCloseDropdown,
        onSelect: onSelect,
        isMultiLevelHandler: isMultiLevelHandler,
        LinkComponent: LinkComponent
      })
    });
  }
  return null;
});
DropdownItem.displayName = 'DropdownItem';
export default DropdownItem;