'use strict';

exports.__esModule = true;

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 _recompose = require('recompose');

var _SafeAnchor = require('./SafeAnchor');

var _SafeAnchor2 = _interopRequireDefault(_SafeAnchor);

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

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

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

    _this.toggle = function (event, isOpen) {
      var open = (0, _isUndefined3.default)(isOpen) ? !_this.state.open : isOpen;
      _this.setState({ open: open });
    };

    _this.handleClick = function (event) {
      var _this$props = _this.props,
          onSelect = _this$props.onSelect,
          eventKey = _this$props.eventKey,
          disabled = _this$props.disabled,
          onClick = _this$props.onClick;


      if (disabled) {
        event.preventDefault();
        return;
      }

      onSelect && onSelect(eventKey, event);
      onClick && onClick(event);
    };

    _this.handleMouseOver = function (event) {
      _this.toggle(event, true);
    };

    _this.handleMouseOut = function (event) {
      _this.toggle(event, false);
    };

    _this.state = {
      open: props.open
    };
    return _this;
  }

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

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

    var _props = this.props,
        children = _props.children,
        divider = _props.divider,
        panel = _props.panel,
        onSelect = _props.onSelect,
        active = _props.active,
        disabled = _props.disabled,
        className = _props.className,
        eventKey = _props.eventKey,
        submenu = _props.submenu,
        style = _props.style,
        classPrefix = _props.classPrefix,
        tabIndex = _props.tabIndex,
        pullLeft = _props.pullLeft,
        icon = _props.icon,
        open = _props.open,
        trigger = _props.trigger,
        expanded = _props.expanded,
        Component = _props.componentClass,
        props = _objectWithoutProperties(_props, ['children', 'divider', 'panel', 'onSelect', 'active', 'disabled', 'className', 'eventKey', 'submenu', 'style', 'classPrefix', 'tabIndex', 'pullLeft', 'icon', 'open', 'trigger', 'expanded', 'componentClass']);

    var addPrefix = (0, _utils.prefix)(classPrefix);
    var classes = (0, _classnames2.default)(classPrefix, className, (_classNames = {}, _classNames[addPrefix(expanded ? 'expand' : 'collapse')] = submenu && this.context.sidenav, _classNames[addPrefix('submenu')] = submenu, _classNames[addPrefix('open')] = (0, _isUndefined3.default)(open) ? this.state.open : open, _classNames[addPrefix('active')] = active, _classNames[addPrefix('disabled')] = disabled, _classNames[addPrefix('pull-' + (pullLeft ? 'left' : 'right'))] = pullLeft, _classNames));

    var itemProps = {};
    var itemToggleProps = {
      onClick: this.handleClick
    };

    if ((0, _utils.isOneOf)('hover', trigger) && submenu && !this.context.expanded) {
      itemProps.onMouseOver = this.handleMouseOver;
      itemProps.onMouseOut = this.handleMouseOut;
    }

    if ((0, _utils.isOneOf)('click', trigger) && submenu) {
      itemToggleProps.onClick = (0, _utils.createChainedFunction)(this.handleClick, this.toggle);
    }

    if (divider) {
      return React.createElement('li', {
        role: 'separator',
        style: style,
        className: (0, _classnames2.default)(addPrefix('divider'), className)
      });
    }

    if (panel) {
      return React.createElement(
        'li',
        { style: style, className: (0, _classnames2.default)(addPrefix('panel'), className) },
        children
      );
    }

    return React.createElement(
      'li',
      _extends({}, itemProps, { style: style, role: 'presentation', className: classes }),
      React.createElement(
        Component,
        _extends({}, props, itemToggleProps, {
          className: addPrefix('content'),
          tabIndex: tabIndex
        }),
        icon,
        children
      )
    );
  };

  return DropdownMenuItem;
}(React.Component);

DropdownMenuItem.defaultProps = {
  tabIndex: -1,
  trigger: 'hover'
};
DropdownMenuItem.contextTypes = {
  sidenav: _propTypes2.default.bool,
  expanded: _propTypes2.default.bool
};
DropdownMenuItem.handledProps = ['active', 'children', 'className', 'classPrefix', 'componentClass', 'disabled', 'divider', 'eventKey', 'expanded', 'icon', 'onClick', 'onSelect', 'open', 'panel', 'pullLeft', 'style', 'submenu', 'tabIndex', 'trigger'];
DropdownMenuItem.propTypes = {
  divider: _propTypes2.default.bool,
  panel: _propTypes2.default.bool,
  trigger: _propTypes2.default.oneOfType([_propTypes2.default.oneOf(['click', 'hover']), _propTypes2.default.arrayOf(_propTypes2.default.oneOf(['click', 'hover']))]),
  open: _propTypes2.default.bool,
  expanded: _propTypes2.default.bool,
  active: _propTypes2.default.bool,
  disabled: _propTypes2.default.bool,
  pullLeft: _propTypes2.default.bool,
  submenu: _propTypes2.default.bool,
  onSelect: _propTypes2.default.func,
  onClick: _propTypes2.default.func,
  icon: _propTypes2.default.node,
  eventKey: _propTypes2.default.any,
  className: _propTypes2.default.string,
  style: _propTypes2.default.object,
  children: _propTypes2.default.node,
  classPrefix: _propTypes2.default.string,
  tabIndex: _propTypes2.default.number,
  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);
  }
};
exports.default = (0, _recompose.setDisplayName)('DropdownMenuItem')((0, _utils.defaultProps)({
  classPrefix: 'dropdown-item',
  componentClass: _SafeAnchor2.default
})(DropdownMenuItem));