import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import { useRef, useEffect } from 'react';
import { useFormikContext } from 'formik';
import { is } from 'ramda';

var _transformObjectToDotNotation = function transformObjectToDotNotation(object) {
  var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
  var result = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  var errorKey = Object.keys(object).find(function (key) {
    return !!object[key];
  });
  if (!errorKey) return;
  var value = object[errorKey];
  if (!value) return;
  var nextKey = prefix ? "".concat(prefix, ".").concat(errorKey) : errorKey;
  is(Object, value) ? _transformObjectToDotNotation(value, nextKey, result) : result.push(nextKey);

  // eslint-disable-next-line consistent-return
  return result;
};
var getErrorFieldName = function getErrorFieldName(formikErrors) {
  var _transformObjectToDot;
  return (_transformObjectToDot = _transformObjectToDotNotation(formikErrors)) === null || _transformObjectToDot === void 0 ? void 0 : _transformObjectToDot[0];
};

var ScrollToErrorField = function ScrollToErrorField(_ref) {
  var formRef = _ref.formRef;
  var _useFormikContext = useFormikContext(),
    submitCount = _useFormikContext.submitCount,
    isValid = _useFormikContext.isValid,
    errors = _useFormikContext.errors;
  var isValidatedReference = useRef(false);
  useEffect(function () {
    isValidatedReference.current = false;
  }, [submitCount]);
  useEffect(function () {
    if (!formRef.current) return;
    if (isValidatedReference.current || isValid) return;
    isValidatedReference.current = true;
    var fieldErrorNames = getErrorFieldName(errors);
    if (fieldErrorNames.length <= 0) return;
    var errorFormElement = _toConsumableArray(formRef.current.elements).find(function (element) {
      return element.name === fieldErrorNames;
    });
    errorFormElement === null || errorFormElement === void 0 || errorFormElement.scrollIntoView({
      behavior: "smooth",
      block: "center"
    });
  }, [submitCount, errors]); // eslint-disable-line react-hooks/exhaustive-deps

  return null;
};

export { ScrollToErrorField as S };
//# sourceMappingURL=index-BISwUwuE.js.map
