import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["validate", "name", "render", "children", "as", "component", "className"],
  _excluded2 = ["validate", "validationSchema"],
  _excluded3 = ["innerRef"],
  _excluded4 = ["innerRef"];
/* "use client" */

import * as React from 'react';
import { useFormikContext } from './FormikContext';
import { isFunction, isEmptyChildren, isObject } from './utils';
import invariant from 'tiny-warning';
import { __DEV__ } from './constants';
export function useField(propsOrFieldName) {
  var formik = useFormikContext();
  var getFieldProps = formik.getFieldProps,
    getFieldMeta = formik.getFieldMeta,
    getFieldHelpers = formik.getFieldHelpers,
    registerField = formik.registerField,
    unregisterField = formik.unregisterField;
  var isAnObject = isObject(propsOrFieldName);

  // Normalize propsOrFieldName to FieldHookConfig<Val>
  var props = isAnObject ? propsOrFieldName : {
    name: propsOrFieldName
  };
  var fieldName = props.name,
    validateFn = props.validate;
  React.useEffect(function () {
    if (fieldName) {
      registerField(fieldName, {
        validate: validateFn
      });
    }
    return function () {
      if (fieldName) {
        unregisterField(fieldName);
      }
    };
  }, [registerField, unregisterField, fieldName, validateFn]);
  if (__DEV__) {
    invariant(formik, 'useField() / <Field /> must be used underneath a <Formik> component or withFormik() higher order component');
  }
  invariant(fieldName, 'Invalid field name. Either pass `useField` a string or an object containing a `name` key.');
  var fieldHelpers = React.useMemo(function () {
    return getFieldHelpers(fieldName);
  }, [getFieldHelpers, fieldName]);
  return [getFieldProps(props), getFieldMeta(fieldName), fieldHelpers];
}
export function Field(_ref) {
  var validate = _ref.validate,
    name = _ref.name,
    render = _ref.render,
    children = _ref.children,
    is = _ref.as,
    component = _ref.component,
    className = _ref.className,
    props = _objectWithoutProperties(_ref, _excluded);
  var _useFormikContext = useFormikContext(),
    _validate = _useFormikContext.validate,
    _validationSchema = _useFormikContext.validationSchema,
    formik = _objectWithoutProperties(_useFormikContext, _excluded2);
  if (__DEV__) {
    // eslint-disable-next-line react-hooks/rules-of-hooks
    React.useEffect(function () {
      invariant(!render, "<Field render> has been deprecated and will be removed in future versions of Formik. Please use a child callback function instead. To get rid of this warning, replace <Field name=\"".concat(name, "\" render={({field, form}) => ...} /> with <Field name=\"").concat(name, "\">{({field, form, meta}) => ...}</Field>"));
      invariant(!(is && children && isFunction(children)), 'You should not use <Field as> and <Field children> as a function in the same <Field> component; <Field as> will be ignored.');
      invariant(!(component && children && isFunction(children)), 'You should not use <Field component> and <Field children> as a function in the same <Field> component; <Field component> will be ignored.');
      invariant(!(render && children && !isEmptyChildren(children)), 'You should not use <Field render> and <Field children> in the same <Field> component; <Field children> will be ignored');
      // eslint-disable-next-line
    }, []);
  }

  // Register field and field-level validation with parent <Formik>
  var registerField = formik.registerField,
    unregisterField = formik.unregisterField;
  React.useEffect(function () {
    registerField(name, {
      validate: validate
    });
    return function () {
      unregisterField(name);
    };
  }, [registerField, unregisterField, name, validate]);
  var field = formik.getFieldProps(_extends({
    name: name
  }, props));
  var meta = formik.getFieldMeta(name);
  var legacyBag = {
    field: field,
    form: formik
  };
  if (render) {
    return render(_extends({}, legacyBag, {
      meta: meta
    }));
  }
  if (isFunction(children)) {
    return children(_extends({}, legacyBag, {
      meta: meta
    }));
  }
  if (component) {
    // This behavior is backwards compat with earlier Formik 0.9 to 1.x
    if (typeof component === 'string') {
      var innerRef = props.innerRef,
        rest = _objectWithoutProperties(props, _excluded3);
      return /*#__PURE__*/React.createElement(component, _extends({
        ref: innerRef
      }, field, rest, {
        className: className
      }), children);
    }
    // We don't pass `meta` for backwards compat
    return /*#__PURE__*/React.createElement(component, _extends({
      field: field,
      form: formik
    }, props, {
      className: className
    }), children);
  }

  // default to input here so we can check for both `as` and `children` above
  var asElement = is || 'input';
  if (typeof asElement === 'string') {
    var _innerRef = props.innerRef,
      _rest = _objectWithoutProperties(props, _excluded4);
    return /*#__PURE__*/React.createElement(asElement, _extends({
      ref: _innerRef
    }, field, _rest, {
      className: className
    }), children);
  }
  return /*#__PURE__*/React.createElement(asElement, _extends({}, field, props, {
    className: className
  }), children);
}