'use strict';

exports.__esModule = true;

var _kebabCase2 = require('lodash/kebabCase');

var _kebabCase3 = _interopRequireDefault(_kebabCase2);

var _isUndefined2 = require('lodash/isUndefined');

var _isUndefined3 = _interopRequireDefault(_isUndefined2);

var _isEqual2 = require('lodash/isEqual');

var _isEqual3 = _interopRequireDefault(_isEqual2);

var _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; };

var _react = require('react');

var React = _interopRequireWildcard(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _setStatic = require('recompose/setStatic');

var _setStatic2 = _interopRequireDefault(_setStatic);

var _setDisplayName = require('recompose/setDisplayName');

var _setDisplayName2 = _interopRequireDefault(_setDisplayName);

var _Overlay = require('rsuite-utils/lib/Overlay');

var _DropdownToggle = require('./DropdownToggle');

var _DropdownToggle2 = _interopRequireDefault(_DropdownToggle);

var _DropdownMenu = require('./DropdownMenu');

var _DropdownMenu2 = _interopRequireDefault(_DropdownMenu);

var _DropdownMenuItem = require('./DropdownMenuItem');

var _DropdownMenuItem2 = _interopRequireDefault(_DropdownMenuItem);

var _Icon = require('./Icon');

var _Icon2 = _interopRequireDefault(_Icon);

var _utils = require('./utils');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var Dropdown = function (_React$Component) {
  _inherits(Dropdown, _React$Component);

  function Dropdown(props) {
    _classCallCheck(this, Dropdown);

    var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));

    _this.toggle = function (isOpen) {
      var _this$props = _this.props,
          onOpen = _this$props.onOpen,
          onClose = _this$props.onClose,
          onToggle = _this$props.onToggle;

      var open = (0, _isUndefined3.default)(isOpen) ? !_this.state.open : isOpen;
      var handleToggle = open ? onOpen : onClose;

      _this.setState({ open: open }, function () {
        handleToggle && handleToggle();
      });

      onToggle && onToggle(open);
    };

    _this.handleClick = function (event) {
      event.preventDefault();
      if (_this.props.disabled) {
        return;
      }
      _this.toggle();
    };

    _this.handleOpenChange = function (event) {
      var eventKey = _this.props.eventKey;
      var onOpenChange = _this.context.onOpenChange;

      onOpenChange && onOpenChange(eventKey, event);
    };

    _this.handleToggleChange = function (eventKey, event) {
      var onOpenChange = _this.context.onOpenChange;

      onOpenChange && onOpenChange(eventKey, event);
    };

    _this.handleMouseEnter = function () {
      if (!_this.props.disabled) {
        _this.toggle(true);
      }
    };

    _this.handleMouseLeave = function () {
      if (!_this.props.disabled) {
        _this.toggle(false);
      }
    };

    _this.handleSelect = function (eventKey, event) {
      var onSelect = _this.props.onSelect;

      onSelect && onSelect(eventKey, event);
      _this.toggle(false);
    };

    _this.state = {
      title: null,
      open: props.open
    };
    return _this;
  }

  Dropdown.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    if (!(0, _isEqual3.default)(nextProps.open, this.props.open)) {
      this.setState({
        open: nextProps.open
      });
    }
  };

  Dropdown.prototype.render = function render() {
    var _classNames;

    var _props = this.props,
        title = _props.title,
        children = _props.children,
        className = _props.className,
        menuStyle = _props.menuStyle,
        disabled = _props.disabled,
        renderTitle = _props.renderTitle,
        classPrefix = _props.classPrefix,
        placement = _props.placement,
        activeKey = _props.activeKey,
        tabIndex = _props.tabIndex,
        toggleClassName = _props.toggleClassName,
        trigger = _props.trigger,
        icon = _props.icon,
        onClick = _props.onClick,
        onMouseEnter = _props.onMouseEnter,
        onMouseLeave = _props.onMouseLeave,
        onContextMenu = _props.onContextMenu,
        open = _props.open,
        eventKey = _props.eventKey,
        Component = _props.componentClass,
        props = _objectWithoutProperties(_props, ['title', 'children', 'className', 'menuStyle', 'disabled', 'renderTitle', 'classPrefix', 'placement', 'activeKey', 'tabIndex', 'toggleClassName', 'trigger', 'icon', 'onClick', 'onMouseEnter', 'onMouseLeave', 'onContextMenu', 'open', 'eventKey', 'componentClass']);

    var _context = this.context,
        _context$openKeys = _context.openKeys,
        openKeys = _context$openKeys === undefined ? [] : _context$openKeys,
        sidenav = _context.sidenav,
        expanded = _context.expanded;

    var menuExpanded = openKeys.some(function (key) {
      return (0, _isEqual3.default)(key, eventKey);
    });
    var addPrefix = (0, _utils.prefix)(classPrefix);
    var isOpen = (0, _isUndefined3.default)(open) ? this.state.open : open;
    var collapsible = sidenav && expanded;

    var toggleProps = {
      onClick: (0, _utils.createChainedFunction)(this.handleOpenChange, onClick),
      onContextMenu: onContextMenu
    };

    var dropdownProps = {
      onMouseEnter: onMouseEnter,
      onMouseLeave: onMouseLeave
    };

    /**
     * Bind event of trigger,
     * not used in  in the expanded state of '<Sidenav>'
     */
    if (!collapsible) {
      if ((0, _utils.isOneOf)('click', trigger)) {
        toggleProps.onClick = (0, _utils.createChainedFunction)(this.handleClick, toggleProps.onClick);
      }

      if ((0, _utils.isOneOf)('contextMenu', trigger)) {
        toggleProps.onContextMenu = (0, _utils.createChainedFunction)(this.handleClick, onContextMenu);
      }

      if ((0, _utils.isOneOf)('hover', trigger)) {
        dropdownProps.onMouseEnter = (0, _utils.createChainedFunction)(this.handleMouseEnter, onMouseEnter);
        dropdownProps.onMouseLeave = (0, _utils.createChainedFunction)(this.handleMouseLeave, onMouseLeave);
      }
    }

    var Toggle = React.createElement(
      _DropdownToggle2.default,
      _extends({}, toggleProps, {
        tabIndex: tabIndex,
        className: toggleClassName,
        renderTitle: renderTitle,
        icon: icon
      }),
      this.state.title || title
    );

    var Menu = React.createElement(
      _DropdownMenu2.default,
      {
        expanded: menuExpanded,
        collapsible: collapsible,
        activeKey: activeKey,
        onSelect: this.handleSelect,
        style: menuStyle,
        onToggle: this.handleToggleChange,
        openKeys: openKeys
      },
      children
    );

    if (isOpen) {
      Menu = React.createElement(
        _Overlay.RootCloseWrapper,
        { onRootClose: this.toggle },
        Menu
      );
    }

    var classes = (0, _classnames2.default)(classPrefix, addPrefix('placement-' + (0, _kebabCase3.default)(placement)), className, (_classNames = {}, _classNames[addPrefix('disabled')] = disabled, _classNames[addPrefix('open')] = isOpen, _classNames[addPrefix(menuExpanded ? 'expand' : 'collapse')] = sidenav, _classNames));

    var unhandled = (0, _utils.getUnhandledProps)(Dropdown, props);

    return React.createElement(
      Component,
      _extends({}, unhandled, dropdownProps, { className: classes, role: 'menu' }),
      Menu,
      Toggle
    );
  };

  return Dropdown;
}(React.Component);

Dropdown.defaultProps = {
  placement: 'bottomLeft',
  trigger: 'click',
  tabIndex: 0
};
Dropdown.contextTypes = {
  sidenav: _propTypes2.default.bool,
  expanded: _propTypes2.default.bool,
  openKeys: _propTypes2.default.array,
  onOpenChange: _propTypes2.default.func
};
Dropdown.handledProps = ['activeKey', 'children', 'className', 'classPrefix', 'componentClass', 'disabled', 'eventKey', 'icon', 'menuStyle', 'onClick', 'onClose', 'onContextMenu', 'onMouseEnter', 'onMouseLeave', 'onOpen', 'onSelect', 'onToggle', 'open', 'placement', 'renderTitle', 'tabIndex', 'title', 'toggleClassName', 'trigger'];
Dropdown.propTypes = {
  activeKey: _propTypes2.default.any,
  classPrefix: _propTypes2.default.string.isRequired,
  trigger: _propTypes2.default.oneOfType([_propTypes2.default.oneOf(['click', 'hover', 'contextMenu']), _propTypes2.default.arrayOf(_propTypes2.default.oneOf(['click', 'hover', 'contextMenu']))]),
  placement: _propTypes2.default.oneOf(['bottomLeft', 'bottomRight', 'topLeft', 'topRight', 'leftTop', 'rightTop', 'leftBottom', 'rightBottom']).isRequired,
  title: _propTypes2.default.node,
  disabled: _propTypes2.default.bool,
  icon: _propTypes2.default.node,
  onClose: _propTypes2.default.func,
  onOpen: _propTypes2.default.func,
  onToggle: _propTypes2.default.func,
  onSelect: _propTypes2.default.func,
  onMouseEnter: _propTypes2.default.func,
  onMouseLeave: _propTypes2.default.func,
  onContextMenu: _propTypes2.default.func,
  onClick: _propTypes2.default.func,
  menuStyle: _propTypes2.default.object,
  className: _propTypes2.default.string,
  toggleClassName: _propTypes2.default.string,
  children: function children() {
    return (typeof (React.ChildrenArray == null ? {} : React.ChildrenArray) === 'function' ? _propTypes2.default.instanceOf(React.ChildrenArray == null ? {} : React.ChildrenArray) : _propTypes2.default.any).apply(this, arguments);
  },
  renderTitle: _propTypes2.default.func,
  tabIndex: _propTypes2.default.number,
  open: _propTypes2.default.bool,
  eventKey: _propTypes2.default.any,
  componentClass: function componentClass() {
    return (typeof (React.ElementType == null ? {} : React.ElementType) === 'function' ? _propTypes2.default.instanceOf(React.ElementType == null ? {} : React.ElementType).isRequired : _propTypes2.default.any.isRequired).apply(this, arguments);
  }
};


var EnhancedDropdown = (0, _utils.defaultProps)({
  componentClass: 'div',
  classPrefix: 'dropdown'
})(Dropdown);

(0, _setStatic2.default)('Item', _DropdownMenuItem2.default)(EnhancedDropdown);
(0, _setStatic2.default)('Menu', _DropdownMenu2.default)(EnhancedDropdown);

exports.default = (0, _setDisplayName2.default)('Dropdown')(EnhancedDropdown);