"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _isEqual = _interopRequireDefault(require("lodash-es/isEqual"));
var _classnames = _interopRequireDefault(require("classnames"));
var _dropdownItemContent = _interopRequireDefault(require("./dropdown-item-content"));
var _types = require("./types");
var _hooks = require("../hooks");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const DropdownItem = /*#__PURE__*/(0, _react.forwardRef)(({
  option,
  selectedKey,
  onCloseDropdown,
  onSelect,
  onMouseEnter = () => {},
  onMouseLeave = () => {},
  isMultiLevelHandler,
  LinkComponent
}, ref) => {
  const {
    disabled,
    isShown,
    key,
    className
  } = option;
  const {
    node,
    setFocused,
    isFirstFocusable,
    isDisabled
  } = (0, _hooks.useTreeNodeNavigation)({
    id: option.privateKey || option.key
  });
  (0, _react.useEffect)(() => {
    if (selectedKey && (0, _isEqual.default)(key, selectedKey) || isFirstFocusable) {
      setFocused(node.id);
    }
  }, [key, selectedKey, isFirstFocusable]);
  const isValidKey = selectedKey !== null && selectedKey !== undefined;
  const isActive = isValidKey && key === Math.floor(selectedKey) || selectedKey && (0, _isEqual.default)(key, selectedKey);
  const testId = option.testId ? `menu-item-${option.testId}` : null;
  const elementId = key === undefined ? option.privateKey : key;
  if (isShown === undefined || isShown) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("li", {
      ref: ref,
      className: (0, _classnames.default)(className, {
        disabled,
        active: isActive,
        focused: !isDisabled && !option.radioOptions?.length && node.isFocused,
        'drop-item': !option.radioOptions?.length
      }),
      role: option.isFileUpload || option.isFolderUpload ? _types.MENU_ROLE.MENUITEM : 'none',
      "test-id": testId,
      id: `dropdown-item-${elementId}`,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownItemContent.default, {
        option: option,
        onMouseEnter: onMouseEnter,
        onMouseLeave: onMouseLeave,
        onCloseDropdown: onCloseDropdown,
        onSelect: onSelect,
        isMultiLevelHandler: isMultiLevelHandler,
        LinkComponent: LinkComponent
      })
    });
  }
  return null;
});
DropdownItem.displayName = 'DropdownItem';
var _default = exports.default = DropdownItem;