'use client';

import { cn } from "../../utils/cn.mjs";
import { jsx } from "react/jsx-runtime";
import { FormProvider, useForm as useForm$1, useFormState } 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 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
	});
	const { isSubmitting, isSubmitted, isLoading, isValid } = useFormState({ control: form.control });
	return {
		form,
		isSubmitting,
		isSubmitted,
		isLoading,
		isValid
	};
};

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