'use strict';

exports.__esModule = true;

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

var _isUndefined3 = _interopRequireDefault(_isUndefined2);

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

var _isEqual3 = _interopRequireDefault(_isEqual2);

var _get2 = require('lodash/get');

var _get3 = _interopRequireDefault(_get2);

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 _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _Collapse = require('rsuite-utils/lib/Animation/Collapse');

var _Collapse2 = _interopRequireDefault(_Collapse);

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

var _setDisplayName2 = _interopRequireDefault(_setDisplayName);

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

var _DropdownMenuItem2 = _interopRequireDefault(_DropdownMenuItem);

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

var _Icon2 = _interopRequireDefault(_Icon);

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

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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 DropdownMenu = function (_React$Component) {
  _inherits(DropdownMenu, _React$Component);

  function DropdownMenu() {
    var _temp, _this, _ret;

    _classCallCheck(this, DropdownMenu);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.handleToggleChange = function (eventKey, event) {
      var onToggle = _this.props.onToggle;

      onToggle && onToggle(eventKey, event);
    }, _this.addPrefix = function (name) {
      return (0, _utils.prefix)(_this.props.classPrefix)(name);
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  DropdownMenu.prototype.getMenuItemsAndStatus = function getMenuItemsAndStatus(children) {
    var _this2 = this;

    var hasActiveItem = void 0;

    var _props = this.props,
        activeKey = _props.activeKey,
        onSelect = _props.onSelect,
        classPrefix = _props.classPrefix,
        _props$openKeys = _props.openKeys,
        openKeys = _props$openKeys === undefined ? [] : _props$openKeys;

    var items = React.Children.map(children, function (item, index) {
      var displayName = (0, _get3.default)(item, ['type', 'displayName']);
      var active = void 0;

      if (displayName === 'DropdownMenuItem' || displayName === 'DropdownMenu') {
        active = _this2.isActive(item.props, activeKey);
        if (active) {
          hasActiveItem = true;
        }
      }

      if (displayName === 'DropdownMenuItem') {
        var onItemSelect = item.props.onSelect;

        return React.cloneElement(item, {
          key: index,
          active: active,
          onSelect: (0, _utils.createChainedFunction)(onSelect, onItemSelect)
        });
      } else if (displayName === 'DropdownMenu') {
        var itemsAndStatus = _this2.getMenuItemsAndStatus(item.props.children);
        var _item$props = item.props,
            _icon = _item$props.icon,
            _open = _item$props.open,
            _trigger = _item$props.trigger,
            _pullLeft = _item$props.pullLeft,
            _eventKey = _item$props.eventKey,
            _title = _item$props.title;

        var _expanded = openKeys.some(function (key) {
          return (0, _isEqual3.default)(key, _eventKey);
        });

        return React.createElement(
          _DropdownMenuItem2.default,
          {
            icon: _icon,
            open: _open,
            trigger: _trigger,
            expanded: _expanded,
            active: _this2.isActive(item.props, activeKey),
            className: _this2.addPrefix('pull-' + (_pullLeft ? 'left' : 'right')),
            pullLeft: _pullLeft,
            componentClass: 'div',
            submenu: true
          },
          React.createElement(
            'div',
            {
              className: _this2.addPrefix('toggle'),
              onClick: function onClick(event) {
                _this2.handleToggleChange(_eventKey, event);
              },
              role: 'menu',
              tabIndex: -1
            },
            React.createElement(
              'span',
              null,
              _title
            ),
            React.createElement(_Icon2.default, { icon: _pullLeft ? 'angle-left' : 'angle-right' })
          ),
          _this2.renderCollapse(React.createElement(
            'ul',
            { role: 'menu', className: classPrefix },
            itemsAndStatus.items
          ), _expanded)
        );
      }

      return item;
    });

    return {
      items: items,
      active: hasActiveItem
    };
  };

  DropdownMenu.prototype.isActive = function isActive(props, activeKey) {
    var _this3 = this;

    if (props.active || !(0, _isUndefined3.default)(activeKey) && (0, _isEqual3.default)(props.eventKey, activeKey)) {
      return true;
    }

    if (_utils.ReactChildren.some(props.children, function (child) {
      return _this3.isActive(child.props, activeKey);
    })) {
      return true;
    }

    return props.active;
  };

  DropdownMenu.prototype.renderCollapse = function renderCollapse(children, expanded) {
    return this.props.collapsible ? React.createElement(
      _Collapse2.default,
      {
        'in': expanded,
        exitedClassName: this.addPrefix('collapse-out'),
        exitingClassName: this.addPrefix('collapsing'),
        enteredClassName: this.addPrefix('collapse-in'),
        enteringClassName: this.addPrefix('collapsing')
      },
      children
    ) : children;
  };

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

    var _props2 = this.props,
        pullLeft = _props2.pullLeft,
        children = _props2.children,
        className = _props2.className,
        onSelect = _props2.onSelect,
        classPrefix = _props2.classPrefix,
        activeKey = _props2.activeKey,
        openKeys = _props2.openKeys,
        expanded = _props2.expanded,
        props = _objectWithoutProperties(_props2, ['pullLeft', 'children', 'className', 'onSelect', 'classPrefix', 'activeKey', 'openKeys', 'expanded']);

    var _getMenuItemsAndStatu = this.getMenuItemsAndStatus(children),
        items = _getMenuItemsAndStatu.items,
        active = _getMenuItemsAndStatu.active;

    var unhandled = (0, _utils.getUnhandledProps)(DropdownMenu, props);
    var classes = (0, _classnames2.default)(classPrefix, className, (_classNames = {}, _classNames[this.addPrefix('active')] = active, _classNames));

    return this.renderCollapse(React.createElement(
      'ul',
      _extends({}, unhandled, { className: classes, role: 'menu' }),
      items
    ), expanded);
  };

  return DropdownMenu;
}(React.Component);

DropdownMenu.handledProps = ['activeKey', 'children', 'className', 'classPrefix', 'collapsible', 'eventKey', 'expanded', 'icon', 'onSelect', 'onToggle', 'open', 'openKeys', 'pullLeft', 'title', 'trigger'];
DropdownMenu.propTypes = {
  activeKey: _propTypes2.default.any,
  className: _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);
  },
  icon: _propTypes2.default.node,
  classPrefix: _propTypes2.default.string,
  pullLeft: _propTypes2.default.bool,
  onSelect: _propTypes2.default.func,
  title: _propTypes2.default.node,
  open: _propTypes2.default.bool,
  trigger: _propTypes2.default.oneOfType([_propTypes2.default.oneOf(['click', 'hover']), _propTypes2.default.arrayOf(_propTypes2.default.oneOf(['click', 'hover']))]),
  eventKey: _propTypes2.default.any,
  onToggle: _propTypes2.default.func,
  openKeys: _propTypes2.default.arrayOf(_propTypes2.default.any),
  expanded: _propTypes2.default.bool,
  collapsible: _propTypes2.default.bool
};


var EnhancedDropdownMenu = (0, _utils.defaultProps)({
  classPrefix: 'dropdown-menu'
})(DropdownMenu);

exports.default = (0, _setDisplayName2.default)('DropdownMenu')(EnhancedDropdownMenu);