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 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 _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 _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 Checkbox from '../../checkbox';
import Radio from '../../radio';
import { func, obj, KEYCODE, htmlId } from '../../util';
import Item from './item';
var noop = {};
var bindCtx = func.bindCtx;
var pickOthers = obj.pickOthers;

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

  function CheckableItem(props) {
    var _this;

    _classCallCheck(this, CheckableItem);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(CheckableItem).call(this, props));
    bindCtx(_assertThisInitialized(_this), ['stopPropagation', 'handleKeyDown', 'handleClick']);
    _this.id = htmlId.escapeForId("checkable-item-".concat(props.id || props._key));
    return _this;
  }

  _createClass(CheckableItem, [{
    key: "stopPropagation",
    value: function stopPropagation(e) {
      e.stopPropagation();
    }
  }, {
    key: "handleCheck",
    value: function handleCheck(e) {
      var _this$props = this.props,
          checkType = _this$props.checkType,
          checked = _this$props.checked,
          onChange = _this$props.onChange;

      if (!(checkType === 'radio' && checked)) {
        onChange(!checked, e);
      }
    }
  }, {
    key: "handleKeyDown",
    value: function handleKeyDown(e) {
      if (e.keyCode === KEYCODE.SPACE && !this.props.checkDisabled) {
        this.handleCheck(e);
      }

      this.props.onKeyDown && this.props.onKeyDown(e);
    }
  }, {
    key: "handleClick",
    value: function handleClick(e) {
      this.handleCheck(e);
      this.props.onClick && this.props.onClick(e);
    }
  }, {
    key: "renderCheck",
    value: function renderCheck() {
      var _this$props2 = this.props,
          root = _this$props2.root,
          checked = _this$props2.checked,
          indeterminate = _this$props2.indeterminate,
          disabled = _this$props2.disabled,
          checkType = _this$props2.checkType,
          checkDisabled = _this$props2.checkDisabled,
          onChange = _this$props2.onChange;
      var labelToggleChecked = root.props.labelToggleChecked;
      var Check = checkType === 'radio' ? Radio : Checkbox;
      var checkProps = {
        tabIndex: '-1',
        checked: checked,
        disabled: disabled || checkDisabled
      };

      if (checkType === 'checkbox') {
        checkProps.indeterminate = indeterminate;
      }

      if (!labelToggleChecked) {
        checkProps.onChange = onChange;
        checkProps.onClick = this.stopPropagation;
      }

      return React.createElement(Check, _extends({
        "aria-labelledby": this.id
      }, checkProps));
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          _key = _this$props3._key,
          root = _this$props3.root,
          checked = _this$props3.checked,
          disabled = _this$props3.disabled,
          onClick = _this$props3.onClick,
          helper = _this$props3.helper,
          children = _this$props3.children;
      var _root$props = root.props,
          prefix = _root$props.prefix,
          labelToggleChecked = _root$props.labelToggleChecked;
      var others = pickOthers(Object.keys(CheckableItem.propTypes), this.props);

      var newProps = _objectSpread({
        _key: _key,
        root: root,
        disabled: disabled,
        type: 'item',
        onClick: onClick,
        onKeyDown: this.handleKeyDown
      }, others);

      if (labelToggleChecked && !disabled) {
        newProps.onClick = this.handleClick;
      }

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

  return CheckableItem;
}(Component);

_defineProperty(CheckableItem, "propTypes", {
  _key: PropTypes.string,
  root: PropTypes.object,
  disabled: PropTypes.bool,
  inlineIndent: PropTypes.number,
  checked: PropTypes.bool,
  indeterminate: PropTypes.bool,
  onChange: PropTypes.func,
  checkType: PropTypes.oneOf(['checkbox', 'radio']),
  checkDisabled: PropTypes.bool,
  helper: PropTypes.node,
  children: PropTypes.node,
  onKeyDown: PropTypes.func,
  onClick: PropTypes.func,
  id: PropTypes.string
});

_defineProperty(CheckableItem, "defaultProps", {
  disabled: false,
  checked: false,
  indeterminate: false,
  checkType: 'checkbox',
  checkDisabled: false,
  onChange: noop
});

export { CheckableItem as default };