import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/esm/inherits";
var _excluded = ["component", "formik", "render", "children", "name"];
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import * as React from 'react';
import { getIn, isFunction } from './utils';
import connect from './connect';
var ErrorMessageImpl = /*#__PURE__*/function (_React$Component) {
  function ErrorMessageImpl() {
    _classCallCheck(this, ErrorMessageImpl);
    return _callSuper(this, ErrorMessageImpl, arguments);
  }
  _inherits(ErrorMessageImpl, _React$Component);
  return _createClass(ErrorMessageImpl, [{
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(props) {
      if (getIn(this.props.formik.errors, this.props.name) !== getIn(props.formik.errors, this.props.name) || getIn(this.props.formik.touched, this.props.name) !== getIn(props.formik.touched, this.props.name) || Object.keys(this.props).length !== Object.keys(props).length) {
        return true;
      } else {
        return false;
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
        component = _this$props.component,
        formik = _this$props.formik,
        render = _this$props.render,
        children = _this$props.children,
        name = _this$props.name,
        rest = _objectWithoutProperties(_this$props, _excluded);
      var touch = getIn(formik.touched, name);
      var error = getIn(formik.errors, name);
      return !!touch && !!error ? render ? isFunction(render) ? render(error) : null : children ? isFunction(children) ? children(error) : null : component ? /*#__PURE__*/React.createElement(component, rest, error) : error : null;
    }
  }]);
}(React.Component);
export var ErrorMessage = connect(ErrorMessageImpl);
export default ErrorMessage;