import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import React, { useCallback, useContext } from 'react';
import PropTypes from 'prop-types';
import { SidenavContext } from '../Sidenav/Sidenav';
import deprecatePropType from '../utils/deprecatePropType';
import MenuItem from '../Menu/MenuItem';
import DropdownContext from './DropdownContext';
import isNil from 'lodash/isNil';
import { createChainedFunction, mergeRefs, shallowEqual, useClassNames } from '../utils';
import { NavbarContext } from '../Navbar/Navbar';
import SidenavDropdownItem from '../Sidenav/SidenavDropdownItem';
import DisclosureContext, { DisclosureActionTypes } from '../Disclosure/DisclosureContext';
import SafeAnchor from '../SafeAnchor';
import NavContext from '../Nav/NavContext';
var defaultProps = {
  as: 'li',
  classPrefix: 'dropdown-item'
};
/**
 * The <Dropdown.Item> API
 * When used inside <Sidenav>, renders a <TreeviewItem>
 * Otherwise renders a <MenuItem>
 */

var DropdownItem = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var classPrefix = props.classPrefix,
      className = props.className,
      activeProp = props.active,
      eventKey = props.eventKey,
      onSelect = props.onSelect,
      icon = props.icon,
      rest = _objectWithoutPropertiesLoose(props, ["classPrefix", "className", "active", "eventKey", "onSelect", "icon"]);

  var nav = useContext(NavContext);
  var dropdown = useContext(DropdownContext);

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

  var handleSelectItem = useCallback(function (event) {
    var _dropdown$onSelect;

    onSelect === null || onSelect === void 0 ? void 0 : onSelect(eventKey, event);
    dropdown === null || dropdown === void 0 ? void 0 : (_dropdown$onSelect = dropdown.onSelect) === null || _dropdown$onSelect === void 0 ? void 0 : _dropdown$onSelect.call(dropdown, eventKey, event);
  }, [onSelect, eventKey, dropdown]);
  var sidenav = useContext(SidenavContext);
  var navbar = useContext(NavbarContext);
  var disclosure = useContext(DisclosureContext);

  var _ref = disclosure !== null && disclosure !== void 0 ? disclosure : [],
      dispatchDisclosure = _ref[1];

  var handleClickNavbarDropdownItem = useCallback(function (event) {
    dispatchDisclosure({
      type: DisclosureActionTypes.Hide
    });
    handleSelectItem === null || handleSelectItem === void 0 ? void 0 : handleSelectItem(event);
  }, [dispatchDisclosure, handleSelectItem]);

  if (sidenav !== null && sidenav !== void 0 && sidenav.expanded) {
    return /*#__PURE__*/React.createElement(SidenavDropdownItem, _extends({
      ref: ref
    }, props));
  }

  var menuitemSelected = activeProp || !isNil(eventKey) && (shallowEqual(dropdown === null || dropdown === void 0 ? void 0 : dropdown.activeKey, eventKey) || shallowEqual(nav === null || nav === void 0 ? void 0 : nav.activeKey, eventKey));

  var Component = rest.as,
      divider = rest.divider,
      panel = rest.panel,
      children = rest.children,
      disabled = rest.disabled,
      restProps = _objectWithoutPropertiesLoose(rest, ["as", "divider", "panel", "children", "disabled"]);

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

  if (panel) {
    return /*#__PURE__*/React.createElement(Component, _extends({
      ref: ref,
      role: "none presentation",
      className: merge(prefix('panel'), className)
    }, restProps), children);
  }

  if (navbar) {
    var classes = merge(className, withClassPrefix({
      'with-icon': icon,
      disabled: disabled,
      divider: divider,
      panel: panel,
      active: menuitemSelected
    }));
    var dataAttributes = {
      'data-event-key': eventKey
    };

    if (!isNil(eventKey) && typeof eventKey !== 'string') {
      dataAttributes['data-event-key-type'] = typeof eventKey;
    }

    return /*#__PURE__*/React.createElement("li", null, /*#__PURE__*/React.createElement(SafeAnchor, _extends({
      ref: ref,
      className: classes,
      "aria-current": menuitemSelected || undefined
    }, dataAttributes, restProps, {
      onClick: createChainedFunction(handleClickNavbarDropdownItem, restProps.onClick)
    }), icon && /*#__PURE__*/React.cloneElement(icon, {
      className: prefix('menu-icon')
    }), children));
  }

  return /*#__PURE__*/React.createElement(MenuItem, {
    selected: menuitemSelected,
    disabled: disabled,
    onActivate: handleSelectItem
  }, function (_ref2, menuitemRef) {
    var selected = _ref2.selected,
        active = _ref2.active,
        menuitem = _objectWithoutPropertiesLoose(_ref2, ["selected", "active"]);

    var classes = merge(className, withClassPrefix({
      'with-icon': icon,
      active: selected,
      disabled: disabled,
      focus: active,
      divider: divider,
      panel: panel
    }));
    var dataAttributes = {
      'data-event-key': eventKey
    };

    if (!isNil(eventKey) && typeof eventKey !== 'string') {
      dataAttributes['data-event-key-type'] = typeof eventKey;
    }

    return /*#__PURE__*/React.createElement(Component, _extends({
      ref: mergeRefs(ref, menuitemRef),
      className: classes
    }, menuitem, dataAttributes, restProps), icon && /*#__PURE__*/React.cloneElement(icon, {
      className: prefix('menu-icon')
    }), children);
  });
});
DropdownItem.displayName = 'Dropdown.Item';
DropdownItem.defaultProps = defaultProps;
DropdownItem.propTypes = {
  as: PropTypes.elementType,
  divider: PropTypes.bool,
  panel: PropTypes.bool,
  trigger: PropTypes.oneOfType([PropTypes.array, PropTypes.oneOf(['click', 'hover'])]),
  open: deprecatePropType(PropTypes.bool),
  active: PropTypes.bool,
  disabled: PropTypes.bool,
  pullLeft: deprecatePropType(PropTypes.bool),
  submenu: PropTypes.element,
  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 DropdownItem;