import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import React, { Component, cloneElement } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Tooltip from '../tooltip';

var DropdownMenu =
/*#__PURE__*/
function (_Component) {
  _inherits(DropdownMenu, _Component);

  _createClass(DropdownMenu, null, [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, state) {
      if (props.defaultIsOpen !== state.prevIsOpen) {
        return {
          isOpen: props.defaultIsOpen,
          prevIsOpen: props.defaultIsOpen
        };
      }

      return null;
    }
  }]);

  function DropdownMenu(props) {
    var _this;

    _classCallCheck(this, DropdownMenu);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(DropdownMenu).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "onOpen", function () {
      var onOpenChange = _this.props.onOpenChange;

      if (!_this.isControlled) {
        _this.setState({
          isOpen: true
        });
      }

      onOpenChange && onOpenChange(true);
    });

    _defineProperty(_assertThisInitialized(_this), "onClose", function () {
      var onOpenChange = _this.props.onOpenChange;

      if (!_this.isControlled) {
        _this.setState({
          isOpen: false
        });
      }

      onOpenChange && onOpenChange(false);
    });

    _defineProperty(_assertThisInitialized(_this), "close", function () {
      _this.setState({
        isOpen: false
      });
    });

    var isOpen = props.isOpen,
        defaultIsOpen = props.defaultIsOpen;
    _this.isControlled = isOpen != null;

    if (!_this.isControlled) {
      _this.state = {
        prevIsOpen: defaultIsOpen,
        isOpen: defaultIsOpen
      };
    }

    return _this;
  }

  _createClass(DropdownMenu, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          defaultIsOpen = _this$props.defaultIsOpen,
          prefixCls = _this$props.prefixCls,
          className = _this$props.className,
          placeholder = _this$props.placeholder,
          children = _this$props.children,
          defaultValue = _this$props.defaultValue,
          disabled = _this$props.disabled,
          trigger = _this$props.trigger,
          anchorEl = _this$props.anchorEl,
          _onSelect = _this$props.onSelect,
          onChange = _this$props.onChange,
          propIsOpen = _this$props.isOpen,
          noClose = _this$props.noClose,
          other = _objectWithoutProperties(_this$props, ["defaultIsOpen", "prefixCls", "className", "placeholder", "children", "defaultValue", "disabled", "trigger", "anchorEl", "onSelect", "onChange", "isOpen", "noClose"]);

      var isOpen = this.state.isOpen;
      var menu = cloneElement(children, {
        onSelect: function onSelect() {
          var menuSelect = _onSelect || children.props.onSelect;
          menuSelect && menuSelect.apply(void 0, arguments);
          !noClose && _this2.close();
        },
        onChange: onChange || children.props && children.props.onChange,
        defaultValue: defaultValue || children.props && children.props.defaultValue
      });
      var classes = classNames(prefixCls, className);
      return React.createElement(Tooltip, _extends({
        content: anchorEl,
        isOpen: this.isControlled ? propIsOpen : isOpen,
        trigger: !disabled ? trigger : 'none',
        prefixCls: prefixCls,
        className: classes,
        onOpen: this.onOpen,
        onClose: this.onClose,
        autoAdjustPosition: true
      }, other), menu);
    }
  }]);

  return DropdownMenu;
}(Component);

_defineProperty(DropdownMenu, "displayName", 'NeatUI.DropdownMenu');

_defineProperty(DropdownMenu, "propTypes", {
  prefixCls: PropTypes.string,
  className: PropTypes.string,

  /** 默认值 */
  defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]),

  /** 是否显示菜单 */
  defaultIsOpen: PropTypes.bool,

  /** 显示状态改变时回调 function(isOpen) */
  onOpenChange: PropTypes.func,

  /** 挂载点 */
  anchorEl: PropTypes.element,
  // onChange: PropTypes.func,

  /** 选中时菜单项时回调 function(event, value) */
  onSelect: PropTypes.func,

  /** 弹出位置 */
  position: PropTypes.string,

  /** 选中后是否自动关闭 */
  noClose: PropTypes.bool,

  /** 禁止操作 */
  disabled: PropTypes.bool,

  /** 触发方式 */
  trigger: PropTypes.oneOf(['click', 'hover', 'focus']),

  /** 根据挂载点宽度设置菜单宽度 */
  autoWidth: PropTypes.bool
});

_defineProperty(DropdownMenu, "defaultProps", {
  prefixCls: 'ne-drop-down-menu',
  defaultIsOpen: false,
  trigger: 'focus',
  position: 'bottomLeft',
  noClose: false,
  disabled: false,
  autoWidth: false
});

export default DropdownMenu;