'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

exports.default = committable;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _pathHelpers = require('../helpers/pathHelpers');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

/**
 * Returns an enhanced component that detects that a change to the value of the base component has
 * been committed, and executes a callback with the new value. A change is considered to be
 * committed when the base component loses focus, or the enter key is pressed while it has focus.
 * @param {string|function} BaseComponent - The component to enhance. This component must accept
 * onBlur and onKeyPress props.
 * @returns {function} The enhanced component, which accepts an onCommit prop containing a function
 * to be called with the path to the input, and its new value.
 */
function committable(BaseComponent) {
  var baseComponentName = BaseComponent.displayName || BaseComponent.name || 'Component';

  var propTypes = _extends({}, BaseComponent.propTypes, {
    commitUnchanged: _react.PropTypes.bool,
    onBlur: _react.PropTypes.func,
    onCommit: _react.PropTypes.func,
    onKeyPress: _react.PropTypes.func
  });

  var defaultProps = {
    commitUnchanged: false
  };

  var Committable = function (_Component) {
    _inherits(Committable, _Component);

    function Committable(props) {
      _classCallCheck(this, Committable);

      var _this = _possibleConstructorReturn(this, (Committable.__proto__ || Object.getPrototypeOf(Committable)).call(this, props));

      _this.handleBlur = _this.handleBlur.bind(_this);
      _this.handleKeyPress = _this.handleKeyPress.bind(_this);
      return _this;
    }

    _createClass(Committable, [{
      key: 'handleBlur',
      value: function handleBlur(event) {
        var onBlur = this.props.onBlur;


        var value = event.target.value;

        if (onBlur) {
          onBlur(event);
        }

        this.commit(value);
      }
    }, {
      key: 'handleKeyPress',
      value: function handleKeyPress(event) {
        if (event.key === 'Enter') {
          this.commit(event.target.value);
        }

        var onKeyPress = this.props.onKeyPress;


        if (onKeyPress) {
          onKeyPress(event);
        }
      }
    }, {
      key: 'commit',
      value: function commit(value) {
        var _props = this.props,
            commitUnchanged = _props.commitUnchanged,
            initialValue = _props.value,
            onCommit = _props.onCommit;


        if (onCommit && ((value || initialValue) && value !== initialValue || commitUnchanged)) {
          onCommit((0, _pathHelpers.getPath)(this.props), value);
        }
      }
    }, {
      key: 'render',
      value: function render() {
        var _props2 = this.props,
            commitUnchanged = _props2.commitUnchanged,
            onCommit = _props2.onCommit,
            onKeyPress = _props2.onKeyPress,
            remainingProps = _objectWithoutProperties(_props2, ['commitUnchanged', 'onCommit', 'onKeyPress']);

        return _react2.default.createElement(BaseComponent, _extends({}, remainingProps, {
          onBlur: this.handleBlur,
          onKeyPress: this.handleKeyPress
        }));
      }
    }]);

    return Committable;
  }(_react.Component);

  Committable.propTypes = propTypes;
  Committable.defaultProps = defaultProps;
  Committable.displayName = 'committable(' + baseComponentName + ')';

  return Committable;
}