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 isNil from 'lodash/isNil';
import kebabCase from 'lodash/kebabCase';
import omit from 'lodash/omit';
import DropdownToggle from '../Dropdown/DropdownToggle';
import { useClassNames, placementPolyfill, PLACEMENT_8, mergeRefs } from '../utils';
import { SidenavContext } from './Sidenav';
import deprecatePropType from '../utils/deprecatePropType';
import SidenavDropdownCollapse from './SidenavDropdownCollapse';
import Disclosure from '../Disclosure/Disclosure';
var defaultProps = {
  as: 'div',
  classPrefix: 'dropdown',
  placement: 'bottomStart'
};
var SidenavDropdown = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var Component = props.as,
      title = props.title,
      children = props.children,
      className = props.className,
      menuStyle = props.menuStyle,
      disabled = props.disabled,
      renderTitle = props.renderTitle,
      classPrefix = props.classPrefix,
      placement = props.placement,
      toggleClassName = props.toggleClassName,
      icon = props.icon,
      eventKey = props.eventKey,
      toggleAs = props.toggleAs,
      noCaret = props.noCaret,
      style = props.style,
      onOpen = props.onOpen,
      onClose = props.onClose,
      openProp = props.open,
      onToggle = props.onToggle,
      rest = _objectWithoutPropertiesLoose(props, ["as", "title", "children", "className", "menuStyle", "disabled", "renderTitle", "classPrefix", "placement", "toggleClassName", "icon", "eventKey", "toggleAs", "noCaret", "style", "onOpen", "onClose", "open", "onToggle"]);

  var _useContext = useContext(SidenavContext),
      _useContext$openKeys = _useContext.openKeys,
      openKeys = _useContext$openKeys === void 0 ? [] : _useContext$openKeys,
      onOpenChange = _useContext.onOpenChange;

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

  var handleToggleDisclosure = useCallback(function (open, event) {
    if (open) {
      onClose === null || onClose === void 0 ? void 0 : onClose();
    } else {
      onOpen === null || onOpen === void 0 ? void 0 : onOpen();
    }

    onToggle === null || onToggle === void 0 ? void 0 : onToggle(open);
    if (isNil(eventKey)) return;
    onOpenChange === null || onOpenChange === void 0 ? void 0 : onOpenChange(eventKey, event);
  }, [onClose, onOpen, onToggle, eventKey, onOpenChange]);
  return /*#__PURE__*/React.createElement(Disclosure, {
    open: openProp !== null && openProp !== void 0 ? openProp : !isNil(eventKey) && openKeys.includes(eventKey),
    onToggle: handleToggleDisclosure
  }, function (_ref, containerRef) {
    var _withClassPrefix;

    var open = _ref.open;
    var classes = merge(className, withClassPrefix((_withClassPrefix = {}, _withClassPrefix["placement-" + kebabCase(placementPolyfill(placement))] = placement, _withClassPrefix[open ? 'expand' : 'collapse'] = true, _withClassPrefix.disabled = disabled, _withClassPrefix['no-caret'] = noCaret, _withClassPrefix)));
    return /*#__PURE__*/React.createElement(Component, _extends({
      ref: mergeRefs(ref, containerRef),
      style: style,
      className: classes
    }, rest, {
      "data-event-key": eventKey
    }), /*#__PURE__*/React.createElement(Disclosure.Button, null, function (buttonProps, buttonRef) {
      return /*#__PURE__*/React.createElement(DropdownToggle, _extends({
        ref: buttonRef,
        as: renderTitle ? 'span' : toggleAs,
        noCaret: noCaret,
        className: toggleClassName,
        renderTitle: renderTitle,
        icon: icon,
        placement: placement
      }, omit(buttonProps, ['open'])), title);
    }), /*#__PURE__*/React.createElement(Disclosure.Content, null, function (_ref2) {
      var open = _ref2.open;
      return /*#__PURE__*/React.createElement(SidenavDropdownCollapse, {
        open: open,
        style: menuStyle
      }, children);
    }));
  });
});
SidenavDropdown.displayName = 'Sidenav.Dropdown';
SidenavDropdown.defaultProps = defaultProps;
SidenavDropdown.propTypes = {
  activeKey: PropTypes.any,
  classPrefix: PropTypes.string,
  placement: PropTypes.oneOf(PLACEMENT_8),
  title: PropTypes.node,
  disabled: PropTypes.bool,
  icon: PropTypes.node,
  menuStyle: PropTypes.object,
  className: PropTypes.string,
  toggleClassName: PropTypes.string,
  children: PropTypes.node,
  tabIndex: PropTypes.number,
  open: deprecatePropType(PropTypes.bool),
  eventKey: PropTypes.any,
  as: PropTypes.elementType,
  toggleAs: PropTypes.elementType,
  noCaret: PropTypes.bool,
  style: PropTypes.object,
  onClose: PropTypes.func,
  onOpen: PropTypes.func,
  onToggle: PropTypes.func,
  onMouseEnter: PropTypes.func,
  onMouseLeave: PropTypes.func,
  onContextMenu: PropTypes.func,
  onClick: PropTypes.func,
  renderTitle: PropTypes.func
};
export default SidenavDropdown;