import { jsx, jsxs, Fragment } from "react/jsx-runtime";
import { useSafeState } from "ahooks";
import { useForm, FormProvider } from "react-hook-form";
/* empty css                                                                                                                                                                                       */
/* empty css                                                                                                                                                                                        */
/* empty css                                                                                                                                                                                                */
/* empty css                                                                                                                                                                                           */
/* empty css                                                                                                                                                                                           */
import { useHookFormContext } from "./context.mjs";
import { FormActions } from "./FormActions.mjs";
import { FormErrorMessage } from "./FormErrorMessage.mjs";
import { FormLayout } from "./FormLayout.mjs";
import { Box } from "../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/core/Box/Box.mjs";
const getErrorMessage = (e) => e == null ? void 0 : e.message;
const _Form = ({
  children,
  form,
  formMode,
  reValidateMode = "onChange",
  defaultValues,
  onSubmit,
  onFormUnMount,
  onCancel,
  withActions = true,
  actionsProps,
  errorMessageProps,
  layout = "vertical",
  layoutProps,
  onError,
  stopPropagation,
  preventDefault,
  ...rest
}) => {
  const [submitError, setSubmitError] = useSafeState("");
  const [submitting, setSubmitting] = useSafeState(false);
  const defaultMethods = useForm({
    mode: formMode,
    reValidateMode,
    defaultValues
  });
  const methods = form || defaultMethods;
  const { handleSubmit } = methods;
  const context = useHookFormContext();
  const submit = async (data, event) => {
    setSubmitError("");
    setSubmitting(true);
    try {
      await onSubmit(data, event);
      setSubmitting(false);
    } catch (e) {
      const handleError = onError || (context == null ? void 0 : context.onError) || getErrorMessage;
      setSubmitError(handleError == null ? void 0 : handleError(e));
      setSubmitting(false);
    }
  };
  const handleDismissErrorMessage = () => {
    setSubmitError("");
  };
  return /* @__PURE__ */ jsx(FormProvider, { ...methods, children: /* @__PURE__ */ jsxs(
    Box,
    {
      component: "form",
      ...rest,
      onSubmit: (e) => {
        if (stopPropagation) {
          e == null ? void 0 : e.stopPropagation();
        }
        if (preventDefault) {
          e == null ? void 0 : e.preventDefault();
        }
        return handleSubmit(submit)(e);
      },
      children: [
        /* @__PURE__ */ jsx(FormLayout, { layout, ...layoutProps, children }),
        withActions && /* @__PURE__ */ jsxs(Fragment, { children: [
          submitError && /* @__PURE__ */ jsx(
            FormErrorMessage,
            {
              mt: 24,
              message: submitError,
              onDismiss: handleDismissErrorMessage,
              ...errorMessageProps
            }
          ),
          /* @__PURE__ */ jsx(FormActions, { onCancel, loading: submitting, ...actionsProps })
        ] })
      ]
    }
  ) });
};
const Form = _Form;
Form.Actions = FormActions;
Form.ErrorMessage = FormErrorMessage;
Form.Layout = FormLayout;
export {
  Form
};
