import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';
import { KeyCodes } from '../../utils/constants';
export var KeyDownable =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(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;

    _defineProperty(_assertThisInitialized(_this), "onClick", function (e) {
      var onClick = _this.props.onClick;

      if (_.isFunction(onClick)) {
        onClick(e);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (e) {
      var _this$props = _this.props,
          preventDefaultKeyDown = _this$props.preventDefaultKeyDown,
          stopPropagation = _this$props.stopPropagation,
          onKeyDown = _this$props.onKeyDown;

      if (_.isFunction(onKeyDown)) {
        return onKeyDown(e);
      }

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

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

        _this.onClick(e);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onMouseDown", function (e) {
      var _this$props2 = _this.props,
          preventDefaultMouseDown = _this$props2.preventDefaultMouseDown,
          onMouseDown = _this$props2.onMouseDown;

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

      if (_.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.Component);

_defineProperty(KeyDownable, "propTypes", {
  children: PropTypes.func.isRequired,
  preventDefaultKeyDown: PropTypes.bool,
  preventDefaultMouseDown: PropTypes.bool,
  tabIndex: PropTypes.number,
  stopPropagation: PropTypes.bool,
  onClick: PropTypes.func,
  onKeyDown: PropTypes.func,
  onMouseDown: PropTypes.func
});

_defineProperty(KeyDownable, "defaultProps", {
  preventDefaultKeyDown: true,
  preventDefaultMouseDown: true,
  tabIndex: 0,
  stopPropagation: false,
  onClick: null,
  onKeyDown: null,
  onMouseDown: null
});