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 } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import Icon from '../../icon';
import { func, obj, KEYCODE } from '../../util';
import Item from './item';
var bindCtx = func.bindCtx;
var pickOthers = obj.pickOthers;
/**
 * Menu.Item
 * @order 0
 */

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

  function SelectableItem(props) {
    var _this;

    _classCallCheck(this, SelectableItem);

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

  _createClass(SelectableItem, [{
    key: "getSelected",
    value: function getSelected() {
      var _this$props = this.props,
          _key = _this$props._key,
          root = _this$props.root,
          selected = _this$props.selected;
      var selectMode = root.props.selectMode;
      var selectedKeys = root.state.selectedKeys;
      return selected || !!selectMode && selectedKeys.indexOf(_key) > -1;
    }
  }, {
    key: "handleSelect",
    value: function handleSelect(e) {
      var _this$props2 = this.props,
          _key = _this$props2._key,
          root = _this$props2.root,
          onSelect = _this$props2.onSelect;

      if (onSelect) {
        onSelect(!this.getSelected(), this, e);
      } else {
        root.handleSelect(_key, !this.getSelected(), this);
      }
    }
  }, {
    key: "handleKeyDown",
    value: function handleKeyDown(e) {
      if (e.keyCode === KEYCODE.SPACE && !this.props.disabled) {
        this.handleSelect(e);
      }

      this.props.onKeyDown && this.props.onKeyDown(e);
    }
  }, {
    key: "handleClick",
    value: function handleClick(e) {
      this.handleSelect(e);
      this.props.onClick && this.props.onClick(e);
    }
  }, {
    key: "renderSelectedIcon",
    value: function renderSelectedIcon(selected) {
      var _cx;

      var _this$props3 = this.props,
          root = _this$props3.root,
          inlineIndent = _this$props3.inlineIndent,
          needIndent = _this$props3.needIndent,
          hasSelectedIcon = _this$props3.hasSelectedIcon,
          isSelectIconRight = _this$props3.isSelectIconRight,
          type = _this$props3.type;
      var _root$props = root.props,
          prefix = _root$props.prefix,
          rootSelectedIcon = _root$props.hasSelectedIcon,
          rootSelectIconRight = _root$props.isSelectIconRight;
      var cls = cx((_cx = {}, _defineProperty(_cx, "".concat(prefix, "menu-icon-selected"), true), _defineProperty(_cx, "".concat(prefix, "menu-icon-right"), ('isSelectIconRight' in this.props ? isSelectIconRight : rootSelectIconRight) && type !== 'submenu'), _cx));
      return ('hasSelectedIcon' in this.props ? hasSelectedIcon : rootSelectedIcon) && selected ? React.createElement(Icon, {
        style: needIndent && inlineIndent > 0 ? {
          left: "".concat(inlineIndent, "px")
        } : null,
        className: cls,
        type: "tick"
      }) : null;
    }
  }, {
    key: "render",
    value: function render() {
      var _cx2;

      var _this$props4 = this.props,
          _key = _this$props4._key,
          root = _this$props4.root,
          className = _this$props4.className,
          disabled = _this$props4.disabled,
          helper = _this$props4.helper,
          children = _this$props4.children,
          needIndent = _this$props4.needIndent;
      var prefix = root.props.prefix;
      var others = pickOthers(Object.keys(SelectableItem.propTypes), this.props);
      var selected = this.getSelected();

      var newProps = _objectSpread({
        _key: _key,
        root: root,
        disabled: disabled,
        type: 'item',
        className: cx((_cx2 = {}, _defineProperty(_cx2, "".concat(prefix, "selected"), selected), _defineProperty(_cx2, className, !!className), _defineProperty(_cx2, "".concat(prefix, "select-menu-item"), 'selectMode' in root.props), _cx2)),
        onKeyDown: this.handleKeyDown,
        onClick: !disabled ? this.handleClick : this.props.onClick,
        needIndent: needIndent
      }, others);

      var textProps = {};

      if ('selectMode' in root.props) {
        textProps['aria-selected'] = selected;
      }

      return React.createElement(Item, newProps, this.renderSelectedIcon(selected), React.createElement("span", _extends({
        className: "".concat(prefix, "menu-item-text")
      }, textProps), children), helper ? React.createElement("div", {
        className: "".concat(prefix, "menu-item-helper")
      }, helper) : null);
    }
  }]);

  return SelectableItem;
}(Component);

_defineProperty(SelectableItem, "menuChildType", 'item');

_defineProperty(SelectableItem, "propTypes", {
  _key: PropTypes.string,
  root: PropTypes.object,
  selected: PropTypes.bool,
  onSelect: PropTypes.func,
  inlineIndent: PropTypes.number,

  /**
   * 是否禁用
   */
  disabled: PropTypes.bool,

  /**
   * 帮助文本
   */
  helper: PropTypes.node,

  /**
   * 菜单项标签内容
   */
  children: PropTypes.node,
  className: PropTypes.string,
  onKeyDown: PropTypes.func,
  onClick: PropTypes.func,
  needIndent: PropTypes.bool,
  hasSelectedIcon: PropTypes.bool,
  isSelectIconRight: PropTypes.bool
});

_defineProperty(SelectableItem, "defaultProps", {
  disabled: false,
  needIndent: true
});

export { SelectableItem as default };