"use strict";

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

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

var _core = require("@emotion/core");

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

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

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

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

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

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

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

var _react = require("react");

var withHoveredState = function withHoveredState() {
  return function (BaseComponent) {
    var ComponentWithHoveredState =
    /*#__PURE__*/
    function (_PureComponent) {
      (0, _inherits2["default"])(ComponentWithHoveredState, _PureComponent);

      function ComponentWithHoveredState() {
        var _getPrototypeOf2;

        var _this;

        (0, _classCallCheck2["default"])(this, ComponentWithHoveredState);

        for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
          args[_key] = arguments[_key];
        }

        _this = (0, _possibleConstructorReturn2["default"])(this, (_getPrototypeOf2 = (0, _getPrototypeOf3["default"])(ComponentWithHoveredState)).call.apply(_getPrototypeOf2, [this].concat(args)));
        _this.state = {
          hovered: _this.props.hovered
        };

        _this.handleMouseLeave = function (event) {
          var onMouseLeave = _this.props.onMouseLeave ? _this.props.onMouseLeave : null;

          _this.setState(function (state) {
            return (0, _objectSpread2["default"])({}, state, {
              hovered: false
            });
          });

          if (onMouseLeave) {
            onMouseLeave(event);
          }
        };

        _this.handleMouseEnter = function (event) {
          var onMouseEnter = _this.props.onMouseEnter ? _this.props.onMouseEnter : null;

          _this.setState(function (state) {
            return (0, _objectSpread2["default"])({}, state, {
              hovered: true
            });
          });

          if (onMouseEnter) {
            onMouseEnter(event);
          }
        };

        return _this;
      }

      (0, _createClass2["default"])(ComponentWithHoveredState, [{
        key: "render",
        value: function render() {
          return (0, _core.jsx)(BaseComponent, (0, _extends2["default"])({}, this.props, {
            hovered: this.state.hovered,
            onMouseEnter: this.handleMouseEnter,
            onMouseLeave: this.handleMouseLeave
          }));
        }
      }]);
      return ComponentWithHoveredState;
    }(_react.PureComponent);

    ComponentWithHoveredState.defaultProps = {
      hovered: false
    };
    return ComponentWithHoveredState;
  };
};

exports.withHoveredState = withHoveredState;
var _default = withHoveredState;
exports["default"] = _default;