"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.withValueState = void 0;

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 = _interopRequireWildcard(require("react"));

var withValueState = function withValueState() {
  return function (BaseComponent) {
    var WithValueState =
    /*#__PURE__*/
    function (_React$PureComponent) {
      (0, _inherits2["default"])(WithValueState, _React$PureComponent);

      function WithValueState() {
        var _getPrototypeOf2;

        var _this;

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

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

        _this.handleChange = function (event) {
          var onChange = _this.props.onChange;
          var newValue = event.target.value;

          if (onChange) {
            onChange(event);
          }

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

        return _this;
      }

      (0, _createClass2["default"])(WithValueState, [{
        key: "render",
        value: function render() {
          var value = this.state.value;
          return React.createElement(BaseComponent, (0, _objectSpread2["default"])({}, this.props, {
            value: value,
            onChange: this.handleChange
          }));
        }
      }]);
      return WithValueState;
    }(React.PureComponent);

    return WithValueState;
  };
};

exports.withValueState = withValueState;