import "core-js/modules/web.dom.iterable";
import "core-js/modules/es6.array.iterator";
import "core-js/modules/es6.object.keys";
import "core-js/modules/es6.object.assign";

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 _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 _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }

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

import React from 'react';
import PropTypes from 'prop-types';
import { omit } from 'lodash';
import { ListItem } from "./..";

var MenuItem =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(MenuItem, _React$Component);

  function MenuItem() {
    var _this;

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

    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _this.state = {
      anchorRef: null
    };

    _this.handleKeyDown = function (e) {
      var _this$props = _this.props,
          onClick = _this$props.onClick,
          index = _this$props.index;
      var _this$context = _this.context,
          handleKeyDown = _this$context.handleKeyDown,
          handleClick = _this$context.handleClick;

      if (e.which === 32 || e.which === 13 || e.charCode === 32 || e.charCode === 13) {
        onClick && onClick(e);
        handleClick && handleClick(e, index, _assertThisInitialized(_assertThisInitialized(_this)));
        e.preventDefault();
      } else {
        handleKeyDown && handleKeyDown(e, index, _assertThisInitialized(_assertThisInitialized(_this)));
      }
    };

    _this.handleClick = function (e) {
      var handleClick = _this.context.handleClick;
      var _this$props2 = _this.props,
          index = _this$props2.index,
          onClick = _this$props2.onClick,
          value = _this$props2.value,
          label = _this$props2.label;
      onClick && onClick(e, {
        value: value,
        label: label
      });
      handleClick && handleClick(e, index, _assertThisInitialized(_assertThisInitialized(_this)));
    };

    return _this;
  }

  var _proto = MenuItem.prototype;

  _proto.render = function render() {
    var _this2 = this;

    var _this$props3 = this.props,
        children = _this$props3.children,
        className = _this$props3.className,
        isHeader = _this$props3.isHeader,
        isOpen = _this$props3.isOpen,
        label = _this$props3.label,
        props = _objectWithoutPropertiesLoose(_this$props3, ["children", "className", "isHeader", "isOpen", "label"]);

    var otherProps = omit(Object.assign({}, props), ['index', 'keepMenuOpen', 'onClick']);
    return React.createElement("div", {
      className: 'cui-menu-item' + ("" + (className && " " + className || '')),
      "aria-expanded": isOpen,
      "aria-haspopup": !!children
    }, React.createElement(ListItem, _extends({
      active: isOpen,
      className: "" + (isHeader && "cui-menu-item__header" || ''),
      focusOnLoad: true,
      isReadOnly: isHeader
    }, !isHeader && {
      onClick: this.handleClick
    }, !isHeader && {
      onKeyDown: this.handleKeyDown
    }, {
      ref: function ref(_ref) {
        return !_this2.state.anchorRef && _this2.setState({
          anchorRef: _ref
        });
      },
      role: "menuitem"
    }, otherProps), label || children));
  };

  return MenuItem;
}(React.Component);

MenuItem.propTypes = {
  /** @prop Children nodes to render inside MenuItem | null */
  children: PropTypes.node,

  /** @prop Optional css class name | '' */
  className: PropTypes.string,

  /** @prop Index value of MenuItem | null */
  index: PropTypes.array,

  /** @prop Determines if MenuItem is the header | false */
  isHeader: PropTypes.bool,

  /** @prop Determines if MenuItem is open | false */
  isOpen: PropTypes.bool,

  /** @prop Set to keep the MenuItem open | false */
  keepMenuOpen: PropTypes.bool,

  /** @prop label text of MenuItem | '' */
  label: PropTypes.string,

  /** @prop Callback function invoked when user taps on MenutItem | null */
  onClick: PropTypes.func,

  /** @prop MenuItem value | '' */
  value: PropTypes.string
};
MenuItem.defaultProps = {
  children: null,
  className: '',
  index: null,
  isHeader: false,
  isOpen: false,
  keepMenuOpen: false,
  label: '',
  onClick: null,
  value: ''
};
MenuItem.contextTypes = {
  handleClick: PropTypes.func,
  handleKeyDown: PropTypes.func
};
MenuItem.displayName = 'MenuItem';
export default MenuItem;