"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.withFocusedState = 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 withFocusedState = function withFocusedState() {
  return function (BaseComponent) {
    var ComponentWithFocusedState =
    /*#__PURE__*/
    function (_PureComponent) {
      (0, _inherits2["default"])(ComponentWithFocusedState, _PureComponent);

      function ComponentWithFocusedState() {
        var _getPrototypeOf2;

        var _this;

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

        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"])(ComponentWithFocusedState)).call.apply(_getPrototypeOf2, [this].concat(args)));
        _this.state = {
          focused: _this.props.focused
        };

        _this.handleFocus = function (event) {
          var onFocus = _this.props.onFocus ? _this.props.onFocus : null;

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

          if (onFocus) {
            onFocus(event);
          }
        };

        _this.handleBlur = function (event) {
          var onBlur = _this.props.onBlur ? _this.props.onBlur : null;

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

          if (onBlur) {
            onBlur(event);
          }
        };

        return _this;
      }

      (0, _createClass2["default"])(ComponentWithFocusedState, [{
        key: "render",
        value: function render() {
          return (0, _core.jsx)(BaseComponent, (0, _extends2["default"])({}, this.props, {
            focused: this.state.focused,
            onFocus: this.handleFocus,
            onBlur: this.handleBlur
          }));
        }
      }]);
      return ComponentWithFocusedState;
    }(_react.PureComponent);

    ComponentWithFocusedState.defaultProps = {
      focused: false
    };
    return ComponentWithFocusedState;
  };
};

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