function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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

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

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React, { Component, Children, isValidElement } from 'react';
import { findDOMNode } from 'react-dom';
import PropTypes from 'prop-types';
import cx from 'classnames';
import { func, obj, KEYCODE } from '../../util';
var bindCtx = func.bindCtx;
var pickOthers = obj.pickOthers;

var Item =
/*#__PURE__*/
function (_Component) {
  _inherits(Item, _Component);

  function Item(props) {
    var _this;

    _classCallCheck(this, Item);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Item).call(this, props));
    bindCtx(_assertThisInitialized(_this), ['handleClick', 'handleKeyDown']);
    return _this;
  }

  _createClass(Item, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.itemNode = findDOMNode(this);
      var _this$props = this.props,
          parentMode = _this$props.parentMode,
          root = _this$props.root,
          menu = _this$props.menu;

      if (menu) {
        this.menuNode = findDOMNode(menu);
      } else if (parentMode === 'popup') {
        this.menuNode = this.itemNode.parentNode;
      } else {
        this.menuNode = findDOMNode(root);
        var _root$props = root.props,
            prefix = _root$props.prefix,
            header = _root$props.header,
            footer = _root$props.footer;

        if (header || footer) {
          this.menuNode = this.menuNode.querySelector(".".concat(prefix, "menu-content"));
        }
      }

      this.setFocus();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      this.setFocus();
    }
  }, {
    key: "focusable",
    value: function focusable() {
      var _this$props2 = this.props,
          root = _this$props2.root,
          type = _this$props2.type,
          disabled = _this$props2.disabled;
      var focusable = root.props.focusable;
      return focusable && (type === 'submenu' || !disabled);
    }
  }, {
    key: "getFocused",
    value: function getFocused() {
      var _this$props3 = this.props,
          _key = _this$props3._key,
          root = _this$props3.root;
      var focusedKey = root.state.focusedKey;
      return focusedKey === _key;
    }
  }, {
    key: "setFocus",
    value: function setFocus() {
      var focused = this.getFocused();

      if (focused) {
        if (this.focusable()) {
          this.itemNode.focus({
            preventScroll: true
          });
        }

        if (this.menuNode && this.menuNode.scrollHeight > this.menuNode.clientHeight) {
          var scrollBottom = this.menuNode.clientHeight + this.menuNode.scrollTop;
          var itemBottom = this.itemNode.offsetTop + this.itemNode.offsetHeight;

          if (itemBottom > scrollBottom) {
            this.menuNode.scrollTop = itemBottom - this.menuNode.clientHeight;
          } else if (this.itemNode.offsetTop < this.menuNode.scrollTop) {
            this.menuNode.scrollTop = this.itemNode.offsetTop;
          }
        }
      }
    }
  }, {
    key: "handleClick",
    value: function handleClick(e) {
      e.stopPropagation();
      var _this$props4 = this.props,
          _key = _this$props4._key,
          root = _this$props4.root,
          disabled = _this$props4.disabled;

      if (!disabled) {
        root.handleItemClick(_key, this, e);
        this.props.onClick && this.props.onClick(e);
      } else {
        e.preventDefault();
      }
    }
  }, {
    key: "handleKeyDown",
    value: function handleKeyDown(e) {
      var _this$props5 = this.props,
          _key = _this$props5._key,
          root = _this$props5.root,
          type = _this$props5.type;

      if (this.focusable()) {
        root.handleItemKeyDown(_key, type, this, e);

        switch (e.keyCode) {
          case KEYCODE.ENTER:
            {
              if (!(type === 'submenu')) {
                this.handleClick(e);
              }

              break;
            }
        }
      }

      this.props.onKeyDown && this.props.onKeyDown(e);
    }
  }, {
    key: "getTitle",
    value: function getTitle(children) {
      var labelString = '';

      var loop = function loop(children) {
        Children.forEach(children, function (child) {
          if (isValidElement(child) && child.props.children) {
            loop(child.props.children);
          } else if (typeof child === 'string') {
            labelString += child;
          }
        });
      };

      loop(children);
      return labelString;
    }
  }, {
    key: "render",
    value: function render() {
      var _cx;

      var _this$props6 = this.props,
          level = _this$props6.level,
          root = _this$props6.root,
          replaceClassName = _this$props6.replaceClassName,
          groupIndent = _this$props6.groupIndent,
          component = _this$props6.component,
          disabled = _this$props6.disabled,
          className = _this$props6.className,
          children = _this$props6.children,
          needIndent = _this$props6.needIndent,
          parentMode = _this$props6.parentMode,
          _key = _this$props6._key,
          dangerous = _this$props6.dangerous;
      var others = pickOthers(Object.keys(Item.propTypes), this.props);
      var _root$props2 = root.props,
          prefix = _root$props2.prefix,
          focusable = _root$props2.focusable,
          inlineIndent = _root$props2.inlineIndent,
          itemClassName = _root$props2.itemClassName,
          rtl = _root$props2.rtl;
      var focused = this.getFocused();
      var newClassName = replaceClassName ? className : cx((_cx = {}, _defineProperty(_cx, "".concat(prefix, "menu-item"), true), _defineProperty(_cx, "".concat(prefix, "disabled"), disabled), _defineProperty(_cx, "".concat(prefix, "focused"), !focusable && focused), _defineProperty(_cx, "".concat(prefix, "menu-dangerous-item"), !!dangerous), _defineProperty(_cx, itemClassName, !!itemClassName), _defineProperty(_cx, className, !!className), _cx));

      if (disabled) {
        others['aria-disabled'] = true;
        others['aria-hidden'] = true;
      }

      others.tabIndex = root.tabbableKey === _key ? '0' : '-1';

      if (parentMode === 'inline' && level > 1 && inlineIndent > 0 && needIndent) {
        var paddingProp = rtl ? 'paddingRight' : 'paddingLeft';
        others.style = _objectSpread({}, others.style || {}, _defineProperty({}, paddingProp, "".concat(level * inlineIndent - (groupIndent || 0) * 0.4 * inlineIndent, "px")));
      }

      var TagName = component;
      var role = 'menuitem';

      if ('selectMode' in root.props) {
        role = 'listitem';
      }

      return React.createElement(TagName, _extends({
        role: role
      }, others, {
        className: newClassName,
        onClick: this.handleClick,
        onKeyDown: this.handleKeyDown
      }), React.createElement("div", {
        className: "".concat(prefix, "menu-item-inner")
      }, children));
    }
  }]);

  return Item;
}(Component);

_defineProperty(Item, "propTypes", {
  _key: PropTypes.string,
  level: PropTypes.number,
  groupIndent: PropTypes.number,
  root: PropTypes.object,
  menu: PropTypes.any,
  parent: PropTypes.object,
  parentMode: PropTypes.oneOf(['inline', 'popup']),
  type: PropTypes.oneOf(['submenu', 'item']),
  component: PropTypes.string,
  disabled: PropTypes.bool,
  className: PropTypes.string,
  onClick: PropTypes.func,
  onKeyDown: PropTypes.func,
  needIndent: PropTypes.bool,
  replaceClassName: PropTypes.bool,
  dangerous: PropTypes.bool
});

_defineProperty(Item, "defaultProps", {
  component: 'li',
  groupIndent: 0,
  replaceClassName: false,
  needIndent: true,
  dangerous: false
});

export { Item as default };