'use client';


import { cn } from "../../utils/cn.mjs";
import { jsx } from "react/jsx-runtime";
import { FormProvider, useForm as useForm$1 } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";

//#region src/components/Form/FormBase.tsx
const awaitFunction = async (fn) => {
	if (fn && typeof fn.then === "function") return await fn;
	return fn;
};
const Form = ({ schema, onSubmit: onSubmitProp, onSubmitSuccess: onSubmitSuccessProp, onSubmitError: onSubmitErrorProp, className, children, autoComplete,...props }) => {
	const onSubmit = async (values) => {
		const parsedValues = schema?.safeParse(values) ?? {
			success: true,
			data: void 0
		};
		await awaitFunction(onSubmitProp?.(values));
		if (parsedValues.success) await awaitFunction(onSubmitSuccessProp?.(parsedValues.data));
		else await awaitFunction(onSubmitErrorProp?.(new Error(parsedValues.error.issues.map((error) => error.message).join(", "))));
	};
	return /* @__PURE__ */ jsx(FormProvider, {
		...props,
		children: /* @__PURE__ */ jsx("form", {
			className: cn("flex size-full flex-col gap-y-6", className),
			onSubmit: props.handleSubmit(onSubmit),
			autoComplete: autoComplete ? "on" : "off",
			noValidate: true,
			children
		})
	});
};
const useForm = (schema, props) => {
	const form = useForm$1({
		resolver: zodResolver(schema),
		...props
	});
	return {
		form,
		isSubmitting: form.formState.isSubmitting,
		isSubmitted: form.formState.isSubmitted,
		isLoading: form.formState.isLoading,
		isValid: form.formState.isValid
	};
};

//#endregion
export { Form, useForm };
//# sourceMappingURL=FormBase.mjs.map