"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _icon = _interopRequireDefault(require("../../icon"));

var _util = require("../../util");

var _item = _interopRequireDefault(require("./item"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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; }

var bindCtx = _util.func.bindCtx;
var pickOthers = _util.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 === _util.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 = (0, _classnames["default"])((_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["default"].createElement(_icon["default"], {
        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: (0, _classnames["default"])((_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["default"].createElement(_item["default"], newProps, this.renderSelectedIcon(selected), _react["default"].createElement("span", _extends({
        className: "".concat(prefix, "menu-item-text")
      }, textProps), children), helper ? _react["default"].createElement("div", {
        className: "".concat(prefix, "menu-item-helper")
      }, helper) : null);
    }
  }]);

  return SelectableItem;
}(_react.Component);

exports["default"] = SelectableItem;

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

_defineProperty(SelectableItem, "propTypes", {
  _key: _propTypes["default"].string,
  root: _propTypes["default"].object,
  selected: _propTypes["default"].bool,
  onSelect: _propTypes["default"].func,
  inlineIndent: _propTypes["default"].number,

  /**
   * 是否禁用
   */
  disabled: _propTypes["default"].bool,

  /**
   * 帮助文本
   */
  helper: _propTypes["default"].node,

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

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