"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.KeyDownable = void 0;

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

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

var _lodash = _interopRequireDefault(require("lodash"));

var _constants = require("../../utils/constants");

var KeyDownable =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(KeyDownable, _React$Component);

  function KeyDownable() {
    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;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onClick", function (e) {
      var onClick = _this.props.onClick;

      if (_lodash.default.isFunction(onClick)) {
        onClick(e);
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onKeyDown", function (e) {
      var _this$props = _this.props,
          preventDefaultKeyDown = _this$props.preventDefaultKeyDown,
          stopPropagation = _this$props.stopPropagation,
          onKeyDown = _this$props.onKeyDown;

      if (_lodash.default.isFunction(onKeyDown)) {
        return onKeyDown(e);
      }

      if (e.keyCode === _constants.KeyCodes.SPACE || e.keyCode === _constants.KeyCodes.ENTER) {
        if (preventDefaultKeyDown) {
          e.preventDefault();
        }

        if (stopPropagation) {
          e.stopPropagation();
        }

        _this.onClick(e);
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onMouseDown", function (e) {
      var _this$props2 = _this.props,
          preventDefaultMouseDown = _this$props2.preventDefaultMouseDown,
          onMouseDown = _this$props2.onMouseDown;

      if (preventDefaultMouseDown) {
        e.preventDefault();
      }

      if (_lodash.default.isFunction(onMouseDown)) {
        onMouseDown(e);
      }
    });
    return _this;
  }

  var _proto = KeyDownable.prototype;

  _proto.render = function render() {
    var renderChild = this.props.children;
    return renderChild({
      tabIndex: this.props.tabIndex,
      onClick: this.onClick,
      onKeyDown: this.onKeyDown,
      onMouseDown: this.onMouseDown
    });
  };

  return KeyDownable;
}(_react.default.Component);

exports.KeyDownable = KeyDownable;
(0, _defineProperty2.default)(KeyDownable, "propTypes", {
  children: _propTypes.default.func.isRequired,
  preventDefaultKeyDown: _propTypes.default.bool,
  preventDefaultMouseDown: _propTypes.default.bool,
  tabIndex: _propTypes.default.number,
  stopPropagation: _propTypes.default.bool,
  onClick: _propTypes.default.func,
  onKeyDown: _propTypes.default.func,
  onMouseDown: _propTypes.default.func
});
(0, _defineProperty2.default)(KeyDownable, "defaultProps", {
  preventDefaultKeyDown: true,
  preventDefaultMouseDown: true,
  tabIndex: 0,
  stopPropagation: false,
  onClick: null,
  onKeyDown: null,
  onMouseDown: null
});