function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React from 'react';
import { findDOMNode } from 'react-dom';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import Button from '../button';
import Icon from '../icon';
import Menu from '../menu';
import Overlay from '../overlay';
import { obj, func } from '../util';
var Popup = Overlay.Popup;
/**
 * MenuButton
 */

var MenuButton =
/*#__PURE__*/
function (_React$Component) {
  _inherits(MenuButton, _React$Component);

  function MenuButton(props, context) {
    var _this;

    _classCallCheck(this, MenuButton);

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

    _defineProperty(_assertThisInitialized(_this), "clickMenuItem", function (key) {
      var _this$props;

      var selectMode = _this.props.selectMode;

      for (var _len = arguments.length, others = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
        others[_key - 1] = arguments[_key];
      }

      (_this$props = _this.props).onItemClick.apply(_this$props, [key].concat(others));

      if (selectMode === 'multiple') {
        return;
      }

      _this.onPopupVisibleChange(false, 'menuSelect');
    });

    _defineProperty(_assertThisInitialized(_this), "selectMenu", function (keys) {
      var _this$props2;

      if (!('selectedKeys' in _this.props)) {
        _this.setState({
          selectedKeys: keys
        });
      }

      for (var _len2 = arguments.length, others = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
        others[_key2 - 1] = arguments[_key2];
      }

      (_this$props2 = _this.props).onSelect.apply(_this$props2, [keys].concat(others));
    });

    _defineProperty(_assertThisInitialized(_this), "onPopupOpen", function () {
      var button = findDOMNode(_assertThisInitialized(_this));

      if (_this.props.autoWidth && button && _this.menu) {
        _this.menu.style.width = "".concat(button.offsetWidth, "px");
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onPopupVisibleChange", function (visible, type) {
      if (!('visible' in _this.props)) {
        _this.setState({
          visible: visible
        });
      }

      _this.props.onVisibleChange(visible, type);
    });

    _defineProperty(_assertThisInitialized(_this), "_menuRefHandler", function (ref) {
      _this.menu = findDOMNode(ref);
      var refFn = _this.props.menuProps.ref;

      if (typeof refFn === 'function') {
        refFn(ref);
      }
    });

    _this.state = {
      selectedKeys: props.selectedKeys || props.defaultSelectedKeys,
      visible: props.visible || props.defaultVisible
    };
    return _this;
  }

  _createClass(MenuButton, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if ('visible' in nextProps) {
        this.setState({
          visible: nextProps.visible
        });
      }

      if ('selectedKeys' in nextProps) {
        this.setState({
          selectedKeys: nextProps.selectedKeys
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _classnames;

      var _this$props3 = this.props,
          prefix = _this$props3.prefix,
          size = _this$props3.size,
          style = _this$props3.style,
          className = _this$props3.className,
          label = _this$props3.label,
          popupTriggerType = _this$props3.popupTriggerType,
          popupContainer = _this$props3.popupContainer,
          popupStyle = _this$props3.popupStyle,
          popupClassName = _this$props3.popupClassName,
          popupProps = _this$props3.popupProps,
          followTrigger = _this$props3.followTrigger,
          selectMode = _this$props3.selectMode,
          menuProps = _this$props3.menuProps,
          children = _this$props3.children,
          others = _objectWithoutProperties(_this$props3, ["prefix", "size", "style", "className", "label", "popupTriggerType", "popupContainer", "popupStyle", "popupClassName", "popupProps", "followTrigger", "selectMode", "menuProps", "children"]);

      var state = this.state;
      var classNames = classnames((_classnames = {}, _defineProperty(_classnames, "".concat(prefix, "menu-btn"), true), _defineProperty(_classnames, "".concat(prefix, "expand"), state.visible), _defineProperty(_classnames, "opened", state.visible), _classnames), className);
      var popupClassNames = classnames(_defineProperty({}, "".concat(prefix, "menu-btn-popup"), true), popupClassName);
      var trigger = React.createElement(Button, _extends({
        style: style,
        className: classNames,
        size: size
      }, obj.pickOthers(MenuButton.propTypes, others)), label, " ", React.createElement(Icon, {
        type: "chevron-down-s",
        className: "".concat(prefix, "menu-btn-arrow")
      }));
      return React.createElement(Popup, _extends({
        animation: {
          "in": 'fadeInDown',
          out: 'fadeOutUp'
        }
      }, popupProps, {
        followTrigger: followTrigger,
        visible: state.visible,
        onVisibleChange: this.onPopupVisibleChange,
        trigger: trigger,
        triggerType: popupTriggerType,
        container: popupContainer,
        onOpen: this.onPopupOpen,
        offset: [0, 4],
        style: popupStyle,
        className: popupClassNames
      }), React.createElement(Menu, _extends({}, menuProps, {
        className: "".concat(prefix, "btn-menu"),
        ref: this._menuRefHandler,
        selectedKeys: state.selectedKeys,
        selectMode: selectMode,
        onSelect: this.selectMenu,
        onItemClick: this.clickMenuItem
      }), children));
    }
  }]);

  return MenuButton;
}(React.Component);

_defineProperty(MenuButton, "propTypes", {
  prefix: PropTypes.string,
  size: PropTypes.oneOf(['small', 'medium']),

  /**
   * 按钮上的文本内容
   */
  label: PropTypes.node,

  /**
   * 弹层是否与按钮宽度相同
   */
  autoWidth: PropTypes.bool,

  /**
   * 弹层触发方式
   */
  popupTriggerType: PropTypes.oneOf(['click', 'hover']),

  /**
   * 弹层容器
   */
  popupContainer: PropTypes.func,

  /**
   * 弹层展开状态
   */
  visible: PropTypes.bool,

  /**
   * 弹层默认是否展开
   */
  defaultVisible: PropTypes.bool,

  /**
   * 弹层在显示和隐藏触发的事件
   */
  onVisibleChange: PropTypes.func,

  /**
   * 弹层自定义样式
   */
  popupStyle: PropTypes.object,

  /**
   * 弹层自定义样式类
   */
  popupClassName: PropTypes.string,

  /**
   * 弹层属性透传
   */
  popupProps: PropTypes.object,

  /**
   * 是否跟随滚动
   */
  followTrigger: PropTypes.bool,

  /**
   * 默认激活的菜单项（用法同 Menu 非受控）
   */
  defaultSelectedKeys: PropTypes.array,

  /**
   * 激活的菜单项（用法同 Menu 受控）
   */
  selectedKeys: PropTypes.array,

  /**
   * 菜单的选择模式，同 Menu
   */
  selectMode: PropTypes.oneOf(['single', 'multiple']),

  /**
   * 点击菜单项后的回调，同 Menu
   */
  onItemClick: PropTypes.func,

  /**
   * 选择菜单后的回调，同 Menu
   */
  onSelect: PropTypes.func,

  /**
   * 菜单属性透传
   */
  menuProps: PropTypes.object,
  style: PropTypes.object,
  className: PropTypes.string,
  children: PropTypes.any
});

_defineProperty(MenuButton, "defaultProps", {
  prefix: 'next-',
  size: 'medium',
  autoWidth: true,
  popupTriggerType: 'click',
  onVisibleChange: func.noop,
  onItemClick: func.noop,
  onSelect: func.noop,
  defaultSelectedKeys: [],
  menuProps: {}
});

MenuButton.Item = Menu.Item;
MenuButton.Group = Menu.Group;
MenuButton.Divider = Menu.Divider;
export default MenuButton;