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

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 { EventOverlay, Icon, ListItem } from "./..";

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

  function SubMenu() {
    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.verifyChildrenElements = function (nameArr) {
      var children = _this.props.children;
      var elementCount = 0;
      var childrenLength = 0;
      React.Children.forEach(children, function (child) {
        childrenLength++;

        if (child.type && nameArr.includes(child.type.displayName)) {
          return elementCount++;
        }
      });
      return elementCount === childrenLength;
    };

    _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;
      onClick && onClick(e);
      handleClick && handleClick(e, index, _assertThisInitialized(_assertThisInitialized(_this)));
    };

    return _this;
  }

  var _proto = SubMenu.prototype;

  _proto.componentDidMount = function componentDidMount() {
    var children = this.props.children;

    if (!children) {
      throw new Error("[@collab-ui/react] SubMenu: children are required for this component.");
    } else if (!this.verifyChildrenElements(['MenuItem', 'SubMenu'])) {
      throw new Error("[@collab-ui/react] SubMenu: children must be SubMenu or MenuItem");
    }
  };

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

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

    var otherProps = omit(Object.assign({}, props), ['customNode', '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,
      focusOnLoad: true,
      isReadOnly: isHeader,
      onClick: this.handleClick,
      onKeyDown: this.handleKeyDown,
      ref: function ref(_ref) {
        return !_this2.state.anchorRef && _this2.setState({
          anchorRef: _ref
        });
      },
      role: "menuitem"
    }, otherProps), customNode ? customNode : [React.createElement("div", {
      className: "cui-menu-item__content",
      key: "content-0"
    }, content || label), React.createElement("div", {
      className: "cui-menu-item__selected-value",
      title: selectedValue,
      key: "content-1"
    }, children && selectedValue), React.createElement("div", {
      className: "cui-menu-item__arrow",
      key: "content-2"
    }, children && React.createElement(Icon, {
      name: "arrow-right_16"
    }))]), isOpen && React.createElement(EventOverlay, {
      anchorNode: this.state.anchorRef,
      isOpen: isOpen,
      direction: "right-top",
      closeOnClick: false,
      isContained: true
    }, React.createElement("div", {
      "aria-label": label,
      role: "menu",
      className: "cui-menu-item-container"
    }, children)));
  };

  return SubMenu;
}(React.Component);

SubMenu.displayName = 'SubMenu';
SubMenu.contextTypes = {
  handleClick: PropTypes.func,
  handleKeyDown: PropTypes.func
};
SubMenu.propTypes = {
  children: PropTypes.node,
  className: PropTypes.string,
  content: PropTypes.element,
  customNode: PropTypes.node,
  index: PropTypes.array,
  isHeader: PropTypes.bool,
  isOpen: PropTypes.bool,
  keepMenuOpen: PropTypes.bool,
  label: PropTypes.string,
  onClick: PropTypes.func,
  selectedValue: PropTypes.string
};
SubMenu.defaultProps = {
  children: null,
  className: '',
  content: null,
  customNode: null,
  isHeader: false,
  isOpen: false,
  keepMenuOpen: false,
  label: '',
  onClick: null,
  selectedValue: ''
};
export default SubMenu;