"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const ahooks = require("ahooks");
const reactHookForm = require("react-hook-form");
;/* empty css                                                                                                                                                                                        */
;/* empty css                                                                                                                                                                                         */
;/* empty css                                                                                                                                                                                                 */
;/* empty css                                                                                                                                                                                            */
;/* empty css                                                                                                                                                                                            */
const context = require("./context.cjs");
const FormActions = require("./FormActions.cjs");
const FormErrorMessage = require("./FormErrorMessage.cjs");
const FormLayout = require("./FormLayout.cjs");
const Box = require("../../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.cjs");
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] = ahooks.useSafeState("");
  const [submitting, setSubmitting] = ahooks.useSafeState(false);
  const defaultMethods = reactHookForm.useForm({
    mode: formMode,
    reValidateMode,
    defaultValues
  });
  const methods = form || defaultMethods;
  const { handleSubmit } = methods;
  const context$1 = context.useHookFormContext();
  const submit = async (data, event) => {
    setSubmitError("");
    setSubmitting(true);
    try {
      await onSubmit(data, event);
      setSubmitting(false);
    } catch (e) {
      const handleError = onError || (context$1 == null ? void 0 : context$1.onError) || getErrorMessage;
      setSubmitError(handleError == null ? void 0 : handleError(e));
      setSubmitting(false);
    }
  };
  const handleDismissErrorMessage = () => {
    setSubmitError("");
  };
  return /* @__PURE__ */ jsxRuntime.jsx(reactHookForm.FormProvider, { ...methods, children: /* @__PURE__ */ jsxRuntime.jsxs(
    Box.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__ */ jsxRuntime.jsx(FormLayout.FormLayout, { layout, ...layoutProps, children }),
        withActions && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
          submitError && /* @__PURE__ */ jsxRuntime.jsx(
            FormErrorMessage.FormErrorMessage,
            {
              mt: 24,
              message: submitError,
              onDismiss: handleDismissErrorMessage,
              ...errorMessageProps
            }
          ),
          /* @__PURE__ */ jsxRuntime.jsx(FormActions.FormActions, { onCancel, loading: submitting, ...actionsProps })
        ] })
      ]
    }
  ) });
};
const Form = _Form;
Form.Actions = FormActions.FormActions;
Form.ErrorMessage = FormErrorMessage.FormErrorMessage;
Form.Layout = FormLayout.FormLayout;
exports.Form = Form;
