'use strict';

import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import _ from 'lodash';
export function isIndeterminate(checked) {
  return null === checked; // do not supply undefined
}
export var FormControl =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(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;

    _defineProperty(_assertThisInitialized(_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 = _.isFunction(context.getNeededAutoFocus) && context.getNeededAutoFocus();
    var hasError = form && props.name in form.value && !!form.errors[props.name];

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

      this.focus();
    }
  };

  _proto.getInputDOMNode = function getInputDOMNode() {
    return ReactDOM.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 = _.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 = _objectWithoutPropertiesLoose(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.Component);

_defineProperty(FormControl, "contextTypes", {
  form: PropTypes.object,
  getNeededAutoFocus: PropTypes.func,
  setNeededAutoFocus: PropTypes.func
});

_defineProperty(FormControl, "propTypes", {
  name: PropTypes.string.isRequired,
  hasFormGroupContainer: PropTypes.bool,
  hasFeedbackBlock: PropTypes.bool,
  testAutomationId: PropTypes.string
});

_defineProperty(FormControl, "defaultProps", {
  hasFormGroupContainer: false,
  hasFeedbackBlock: true,
  testAutomationId: null
});