import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import React, { useContext, useCallback } from 'react';
import PropTypes from 'prop-types';
import SafeAnchor from '../SafeAnchor';
import { isOneOf, createChainedFunction, useClassNames, useControlled } from '../utils';
import { SidenavContext } from '../Sidenav/Sidenav';
var defaultProps = {
  as: SafeAnchor,
  classPrefix: 'dropdown-item',
  trigger: 'hover'
};
var DropdownMenuItem = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _withClassPrefix;

  var Component = props.as,
      children = props.children,
      divider = props.divider,
      panel = props.panel,
      active = props.active,
      disabled = props.disabled,
      className = props.className,
      submenu = props.submenu,
      style = props.style,
      classPrefix = props.classPrefix,
      tabIndex = props.tabIndex,
      pullLeft = props.pullLeft,
      icon = props.icon,
      trigger = props.trigger,
      openProp = props.open,
      expandedProp = props.expanded,
      eventKey = props.eventKey,
      onClick = props.onClick,
      onMouseOver = props.onMouseOver,
      onMouseOut = props.onMouseOut,
      onSelect = props.onSelect,
      rest = _objectWithoutPropertiesLoose(props, ["as", "children", "divider", "panel", "active", "disabled", "className", "submenu", "style", "classPrefix", "tabIndex", "pullLeft", "icon", "trigger", "open", "expanded", "eventKey", "onClick", "onMouseOver", "onMouseOut", "onSelect"]);

  var _useClassNames = useClassNames(classPrefix),
      merge = _useClassNames.merge,
      withClassPrefix = _useClassNames.withClassPrefix,
      prefix = _useClassNames.prefix;

  var _ref = useContext(SidenavContext) || {},
      sidenav = _ref.sidenav,
      expanded = _ref.expanded;

  var _useControlled = useControlled(openProp, false),
      open = _useControlled[0],
      setOpen = _useControlled[1];

  var classes = merge(className, withClassPrefix((_withClassPrefix = {}, _withClassPrefix["pull-" + (pullLeft ? 'left' : 'right')] = submenu, _withClassPrefix[expandedProp ? 'expand' : 'collapse'] = submenu && sidenav, _withClassPrefix['with-icon'] = icon, _withClassPrefix.open = open, _withClassPrefix.submenu = submenu, _withClassPrefix.active = active, _withClassPrefix.disabled = disabled, _withClassPrefix)));
  var handleClick = useCallback(function (event) {
    if (disabled) {
      event.preventDefault();
      return;
    }

    if (isOneOf('click', trigger) && submenu) {
      setOpen(!open);
    }

    onSelect === null || onSelect === void 0 ? void 0 : onSelect(eventKey, event);
  }, [disabled, open, eventKey, trigger, submenu, setOpen, onSelect]);
  var handleMouseOver = useCallback(function () {
    setOpen(true);
  }, [setOpen]);
  var handleMouseOut = useCallback(function () {
    setOpen(false);
  }, [setOpen]);
  var itemEventProps = {};

  if (isOneOf('hover', trigger) && submenu && !expanded) {
    itemEventProps.onMouseOver = createChainedFunction(handleMouseOver, onMouseOver);
    itemEventProps.onMouseOut = createChainedFunction(handleMouseOut, onMouseOut);
  }

  if (divider) {
    return /*#__PURE__*/React.createElement("div", {
      ref: ref,
      role: "separator",
      style: style,
      className: merge(prefix('divider'), className)
    });
  }

  if (panel) {
    return /*#__PURE__*/React.createElement("div", {
      ref: ref,
      role: "menuitem",
      style: style,
      className: merge(prefix('panel'), className)
    }, children);
  }

  return /*#__PURE__*/React.createElement(Component, _extends({
    role: "menuitem"
  }, rest, itemEventProps, {
    tabIndex: disabled ? -1 : tabIndex,
    ref: ref,
    "aria-disabled": disabled,
    style: style,
    className: classes,
    onClick: createChainedFunction(handleClick, onClick)
  }), icon && /*#__PURE__*/React.cloneElement(icon, {
    className: prefix('menu-icon')
  }), children);
});
DropdownMenuItem.displayName = 'DropdownMenuItem';
DropdownMenuItem.defaultProps = defaultProps;
DropdownMenuItem.propTypes = {
  as: PropTypes.elementType,
  divider: PropTypes.bool,
  panel: PropTypes.bool,
  trigger: PropTypes.oneOfType([PropTypes.array, PropTypes.oneOf(['click', 'hover'])]),
  open: PropTypes.bool,
  expanded: PropTypes.bool,
  active: PropTypes.bool,
  disabled: PropTypes.bool,
  pullLeft: PropTypes.bool,
  submenu: PropTypes.bool,
  onSelect: PropTypes.func,
  onClick: PropTypes.func,
  icon: PropTypes.node,
  eventKey: PropTypes.any,
  className: PropTypes.string,
  style: PropTypes.object,
  children: PropTypes.node,
  classPrefix: PropTypes.string,
  tabIndex: PropTypes.number
};
export default DropdownMenuItem;