import * as React from 'react';
import classNames from 'classnames';
import RcDropdown from '../components/rc-dropdown';
import { cloneElement, tuple as tupleString } from '../utils/ReactNodeValidate';
import { ConfigContext } from '../config-provider/context';
import { Icons } from '../components';
var RightLine = Icons.RightLine;
var Placements = tupleString('topLeft', 'topCenter', 'topRight', 'bottomLeft', 'bottomCenter', 'bottomRight');

var Dropdown = function Dropdown(props) {
  var _React$useContext = React.useContext(ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName,
      getCtxPopupContainer = _React$useContext.getPopupContainer;

  var dropdownPrefix = getPrefixClassName('dropdown');

  var getTransitionName = function getTransitionName() {
    var transitionName = props.transitionName;

    if (transitionName !== undefined) {
      return transitionName;
    } // if (placement.indexOf('top') >= 0) {
    //   return `${dropdownPrefix}-slide-down`;
    // }
    // return `${dropdownPrefix}-slide-up`;

  };

  var renderOverlay = function renderOverlay() {
    var overlay = props.overlay;
    var overlayNode;

    if (typeof overlay === 'function') {
      overlayNode = overlay();
    } else {
      overlayNode = overlay;
    }

    overlayNode = React.Children.only(typeof overlayNode === 'string' ? /*#__PURE__*/React.createElement("span", null, overlayNode) : overlayNode);
    var overlayProps = overlayNode.props;
    var _overlayProps$selecta = overlayProps.selectable,
        selectable = _overlayProps$selecta === void 0 ? false : _overlayProps$selecta,
        _overlayProps$focusab = overlayProps.focusable,
        focusable = _overlayProps$focusab === void 0 ? true : _overlayProps$focusab,
        expandIcon = overlayProps.expandIcon;
    var overlayNodeExpandIcon = typeof expandIcon !== 'undefined' && /*#__PURE__*/React.isValidElement(expandIcon) ? expandIcon : /*#__PURE__*/React.createElement("span", {
      className: "".concat(dropdownPrefix, "-menu-submenu-arrow")
    }, /*#__PURE__*/React.createElement(RightLine, {
      className: "".concat(dropdownPrefix, "-menu-submenu-arrow-icon")
    }));
    var fixedModeOverlay = typeof overlayNode.type === 'string' ? overlayNode : cloneElement(overlayNode, {
      mode: 'vertical',
      selectable: selectable,
      focusable: focusable,
      expandIcon: overlayNodeExpandIcon
    });
    return fixedModeOverlay;
  };

  var getPlacement = function getPlacement() {
    var placement = props.placement;

    if (placement !== undefined) {
      return placement;
    } // return direction === 'rtl' ? ('bottomRight' as Placement) : ('bottomLeft' as Placement);


    return 'bottomLeft';
  };

  var arrow = props.arrow,
      children = props.children,
      trigger = props.trigger,
      disabled = props.disabled,
      getPopupContainer = props.getPopupContainer,
      overlayClassName = props.overlayClassName;
  var child = React.Children.only(children);
  var dropdownTrigger = cloneElement(child, {
    className: classNames("".concat(dropdownPrefix, "-trigger"), child.props.className),
    disabled: disabled
  });
  var overlayClassNameCustomized = classNames(overlayClassName, {// [`${prefixCls}-rtl`]: direction === 'rtl',
  });
  var triggerActions = disabled ? [] : trigger;
  var alignPoint;

  if (triggerActions && triggerActions.indexOf('contextMenu') !== -1) {
    alignPoint = true;
  }

  return /*#__PURE__*/React.createElement(RcDropdown, Object.assign({
    arrow: arrow,
    alignPoint: alignPoint
  }, props, {
    overlayClassName: overlayClassNameCustomized,
    prefixCls: dropdownPrefix,
    getPopupContainer: getPopupContainer || getCtxPopupContainer,
    transitionName: getTransitionName(),
    trigger: triggerActions,
    overlay: function overlay() {
      return renderOverlay();
    },
    placement: getPlacement()
  }), dropdownTrigger);
}; // Dropdown.Button = DropdownButton;


Dropdown.displayName = 'Dropdown';
Dropdown.defaultProps = {
  mouseEnterDelay: 0.15,
  mouseLeaveDelay: 0.1,
  trigger: ['click']
};
export default Dropdown;