'use strict';

exports.__esModule = true;
exports.withFormField = undefined;

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; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _grommet = require('grommet');

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; }

var withFormField = function withFormField(WrappedField) {
  var _class, _temp2;

  return _temp2 = _class = function (_React$Component) {
    _inherits(GrommetField, _React$Component);

    function GrommetField() {
      var _temp, _this, _ret;

      _classCallCheck(this, GrommetField);

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

      return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.onChange = function (_ref) {
        var value = _ref.target.value;
        var name = _this.props.name;
        var onFieldChange = _this.context.form.onFieldChange;

        onFieldChange(name, value);
      }, _temp), _possibleConstructorReturn(_this, _ret);
    }

    GrommetField.prototype.componentDidMount = function componentDidMount() {
      var _props = this.props,
          validation = _props.validation,
          label = _props.label,
          description = _props.description,
          name = _props.name;

      this.context.form.attachToForm(name, { label: label, description: description, validation: validation });
    };

    GrommetField.prototype.componentWillUnmount = function componentWillUnmount() {
      var name = this.props.name;

      this.context.form.detachFromForm(name);
    };

    GrommetField.prototype.getValue = function getValue() {
      var name = this.props.name;

      var value = this.context.form.getFieldValue(name);
      return value === undefined ? '' : value;
    };

    GrommetField.prototype.render = function render() {
      var _props2 = this.props,
          name = _props2.name,
          validation = _props2.validation,
          inField = _props2.inField,
          label = _props2.label,
          controlLabel = _props2.controlLabel,
          other = _objectWithoutProperties(_props2, ['name', 'validation', 'inField', 'label', 'controlLabel']);

      var errors = this.context.form.getFieldErrors(name);
      var error = Array.isArray(errors) ? errors[0] : errors;
      if (typeof error === 'function') {
        error = error(label || name);
      }
      var field = _react2.default.createElement(WrappedField, _extends({
        plain: true,
        id: name,
        name: name,
        label: inField ? controlLabel : label,
        value: this.getValue(),
        onChange: this.onChange
      }, other));
      if (!inField) {
        return field;
      }
      return _react2.default.createElement(
        _grommet.FormField,
        { htmlFor: name, label: label || controlLabel === true && name, error: error },
        field
      );
    };

    return GrommetField;
  }(_react2.default.Component), _class.defaultProps = {
    validation: undefined,
    inField: true,
    controlLabel: undefined
  }, _class.propTypes = {
    validation: _propTypes2.default.oneOfType([_propTypes2.default.arrayOf(_propTypes2.default.func), _propTypes2.default.func]),
    name: _propTypes2.default.string.isRequired
  }, _class.contextTypes = {
    form: _propTypes2.default.object
  }, _temp2;
};
exports.withFormField = withFormField;