'use strict';

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var formik = require('formik');
var ramda = require('ramda');

const transformObjectToDotNotation = (object, prefix = "") => {
  const result = [];
  Object.entries(object).forEach(([key, value]) => {
    if (!value) return;
    const nextKey = prefix ? `${prefix}.${key}` : key;
    if (ramda.is(Object, value)) {
      result.push(
        ...transformObjectToDotNotation(
          value,
          nextKey
        )
      );
    } else {
      result.push(nextKey);
    }
  });
  return result;
};
const getErrorFieldName = (formikErrors) => transformObjectToDotNotation(formikErrors)?.[0];
const getFieldsWithServerError = (status = {}) => Object.keys(status).filter(
  (fieldName) => status[fieldName] != null && status[fieldName] !== ""
);
const scrollToError = (formRef, errors, status) => {
  const fieldErrorName = getErrorFieldName(errors);
  const [fieldWithServerError] = getFieldsWithServerError(status);
  if (!fieldErrorName && !fieldWithServerError) return;
  const errorFieldName = fieldErrorName || fieldWithServerError;
  const errorFormElement = formRef.current?.querySelector(
    `[name="${errorFieldName}"]`
  );
  errorFormElement?.scrollIntoView({
    behavior: "smooth",
    block: "center"
  });
};

const ScrollToErrorField = ({ formRef }) => {
  const { submitCount, isValid, errors, status } = formik.useFormikContext();
  React.useEffect(() => {
    const fieldsWithServerError = getFieldsWithServerError(status);
    if (!formRef.current || isValid && fieldsWithServerError.length === 0) {
      return;
    }
    scrollToError(formRef, errors, status);
  }, [submitCount]);
  return null;
};

const FormWrapper = React.forwardRef(
  ({ className, formProps, children, scrollToErrorField }, ref) => {
    const { validateForm, setErrors, setTouched, submitForm, ...formikBag } = formik.useFormikContext();
    const { dirty: isFormDirty, isSubmitting, status } = formikBag;
    const formRefForScrollToErrorField = React.useRef(null);
    const formRef = ref || formRefForScrollToErrorField;
    const handleKeyDown = React.useCallback(
      async (event) => {
        const target = event.target;
        const isEventFromEditorOrTextarea = target.tagName === "TEXTAREA" || target.editor;
        if (event.key !== "Enter") return;
        if (isEventFromEditorOrTextarea && !event.metaKey) return;
        event.preventDefault();
        if (event.shiftKey) return;
        if (!isFormDirty || isSubmitting) return;
        try {
          const errors = await validateForm();
          const fieldStatuses = getFieldsWithServerError(status);
          if (Object.keys(errors).length > 0 || Object.keys(fieldStatuses).length > 0) {
            setErrors(errors);
            setTouched({ ...errors, ...status });
            if (scrollToErrorField) {
              scrollToError(formRef, errors, status);
            }
          } else {
            submitForm();
          }
        } catch (error) {
          console.error(
            "An unhandled error was caught from validateForm()",
            error
          );
        }
      },
      [
        validateForm,
        setErrors,
        setTouched,
        isFormDirty,
        isSubmitting,
        submitForm,
        status,
        scrollToErrorField,
        formRef
      ]
    );
    return /* @__PURE__ */ jsxRuntime.jsxs(
      formik.Form,
      {
        ...{ className },
        noValidate: true,
        ref: formRef,
        onKeyDown: handleKeyDown,
        ...formProps,
        children: [
          scrollToErrorField && /* @__PURE__ */ jsxRuntime.jsx(ScrollToErrorField, { ...{ formRef } }),
          children
        ]
      }
    );
  }
);
FormWrapper.displayName = "FormWrapper";

const Form = React.forwardRef(
  ({ className, children, formikProps, formProps, scrollToErrorField = false }, ref) => {
    const formikRef = React.useRef(null);
    const handleSubmit = (values, actions) => {
      const fieldsWithServerError = getFieldsWithServerError(
        formikRef.current?.status
      );
      if (fieldsWithServerError.length > 0) {
        actions.setSubmitting(false);
        return void 0;
      }
      return formikProps.onSubmit(values, actions);
    };
    return /* @__PURE__ */ jsxRuntime.jsx(
      formik.Formik,
      {
        innerRef: formikRef,
        ...formikProps,
        onSubmit: handleSubmit,
        children: (props) => /* @__PURE__ */ jsxRuntime.jsx(FormWrapper, { ...{ className, formProps, ref, scrollToErrorField }, children: typeof children === "function" ? children(props) : children })
      }
    );
  }
);
Form.displayName = "Form";

exports.Form = Form;
//# sourceMappingURL=Form.js.map
