import _extends from "@babel/runtime/helpers/esm/extends";
import * as React from 'react';
import hoistNonReactStatics from 'hoist-non-react-statics';
import { FormikConsumer } from './FormikContext';
import invariant from 'tiny-warning';

/**
 * Connect any component to Formik context, and inject as a prop called `formik`;
 * @param Comp React Component
 */
export default function connect(Comp) {
  var C = function C(props) {
    return /*#__PURE__*/React.createElement(FormikConsumer, null, function (formik) {
      invariant(!!formik, "Formik context is undefined, please verify you are rendering <Form>, <Field>, <FastField>, <FieldArray>, or your custom context-using component as a child of a <Formik> component. Component name: ".concat(Comp.name));
      return /*#__PURE__*/React.createElement(Comp, _extends({}, props, {
        formik: formik
      }));
    });
  };
  var componentDisplayName = Comp.displayName || Comp.name || Comp.constructor && Comp.constructor.name || 'Component';

  // Assign Comp to C.WrappedComponent so we can access the inner component in tests
  // For example, <Field.WrappedComponent /> gets us <FieldInner/>
  C.WrappedComponent = Comp;
  C.displayName = "FormikConnect(".concat(componentDisplayName, ")");
  return hoistNonReactStatics(C, Comp

  // cast type to ComponentClass (even if SFC)
  );
}
export { connect };