'use strict';

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

exports.__esModule = true;
exports.isIndeterminate = isIndeterminate;
exports.FormControl = void 0;

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

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

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

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

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _lodash = _interopRequireDefault(require("lodash"));

function isIndeterminate(checked) {
  return null === checked; // do not supply undefined
}

var FormControl =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(FormControl, _React$Component);

  function FormControl() {
    var _this;

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

    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "focus", function () {
      var node = _this.getInputDOMNode();

      if (node) {
        node.focus();
      }
    });
    return _this;
  }

  var _proto = FormControl.prototype;

  _proto.componentDidUpdate = function componentDidUpdate() {
    var props = this.props;
    var context = this.context;
    var form = context.form;
    var isNeededAutoFocus = _lodash.default.isFunction(context.getNeededAutoFocus) && context.getNeededAutoFocus();
    var hasError = form && props.name in form.value && !!form.errors[props.name];

    if (isNeededAutoFocus && hasError) {
      if (_lodash.default.isFunction(context.setNeededAutoFocus)) {
        context.setNeededAutoFocus(false);
      }

      this.focus();
    }
  };

  _proto.getInputDOMNode = function getInputDOMNode() {
    return _reactDom.default.findDOMNode(this.refs.input);
  };

  _proto.click = function click() {
    var node = this.getInputDOMNode();

    if (node) {
      node.click();
    }
  };

  _proto.getValue = function getValue() {
    var node = this.getInputDOMNode();

    if (node) {
      return node.value;
    }

    return null;
  };

  _proto.outputConverter = function outputConverter(e) {
    return e && e.target ? e.target.value : e;
  };

  _proto.inputConverter = function inputConverter(value) {
    return {
      value: value
    };
  };

  _proto.getPropsFromContext = function getPropsFromContext() {
    var _this2 = this;

    var props = this.props;
    var form = this.context.form;

    if (form && props.name in form.value) {
      var errors = form.errors[props.name];
      var showHelpBlock = errors && props.hasFeedbackBlock; //FIXME errors could be an array

      props = _lodash.default.assign({
        help: showHelpBlock ? errors : null,
        bsStyle: errors ? 'error' : undefined,
        readOnly: form.readOnly,
        onChange: function onChange(e) {
          var _form$setValue;

          return form.setValue((_form$setValue = {}, _form$setValue[props.name] = _this2.outputConverter(e), _form$setValue));
        }
      }, this.inputConverter(form.value[props.name]), props);
    }

    return props;
  }
  /**
   * Removes FormGroup stuff from FormControl props
   * @param {object} props
   * @return {object}
   */
  ;

  _proto.getSafeProps = function getSafeProps(props) {
    var label = props.label,
        validationState = props.validationState,
        bsStyle = props.bsStyle,
        controlId = props.controlId,
        controlLabel = props.controlLabel,
        help = props.help,
        hiddenLabel = props.hiddenLabel,
        labelClassName = props.labelClassName,
        wrapperClassName = props.wrapperClassName,
        groupClassName = props.groupClassName,
        hasFormGroupContainer = props.hasFormGroupContainer,
        hasFeedbackBlock = props.hasFeedbackBlock,
        readOnly = props.readOnly,
        testAutomationId = props.testAutomationId,
        isSingleSelect = props.isSingleSelect,
        minValue = props.minValue,
        maxValue = props.maxValue,
        children = props.children,
        dangerouslySetInnerHTML = props.dangerouslySetInnerHTML,
        noOffset = props.noOffset,
        ariaDescId = props.ariaDescId,
        ariaLabelledById = props.ariaLabelledById,
        rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["label", "validationState", "bsStyle", "controlId", "controlLabel", "help", "hiddenLabel", "labelClassName", "wrapperClassName", "groupClassName", "hasFormGroupContainer", "hasFeedbackBlock", "readOnly", "testAutomationId", "isSingleSelect", "minValue", "maxValue", "children", "dangerouslySetInnerHTML", "noOffset", "ariaDescId", "ariaLabelledById"]);
    return rest;
  };

  return FormControl;
}(_react.default.Component);

exports.FormControl = FormControl;
(0, _defineProperty2.default)(FormControl, "contextTypes", {
  form: _propTypes.default.object,
  getNeededAutoFocus: _propTypes.default.func,
  setNeededAutoFocus: _propTypes.default.func
});
(0, _defineProperty2.default)(FormControl, "propTypes", {
  name: _propTypes.default.string.isRequired,
  hasFormGroupContainer: _propTypes.default.bool,
  hasFeedbackBlock: _propTypes.default.bool,
  testAutomationId: _propTypes.default.string
});
(0, _defineProperty2.default)(FormControl, "defaultProps", {
  hasFormGroupContainer: false,
  hasFeedbackBlock: true,
  testAutomationId: null
});