// import { SubmitErrorHandler, UseFormReturn } from "react-hook-form"; // import { // FormDataPath, // FormGroup, // ValidationConfig, // ValidationResult, // } from "../../types"; // import { FormHandlers } from "./form-handlers"; // import { FormValidation } from "../validation/base/types"; // interface UseFormHandlersProps { // methods: UseFormReturn<{ groups: FormGroup[] }>; // config: ValidationConfig & { // validateOnBlur?: boolean; // onSubmit?: (groups: FormGroup[]) => Promise; // }; // validation: FormValidation; // } // export const useFormHandlers = >({ // methods, // config, // validation, // }: UseFormHandlersProps): FormHandlers => { // const handleBlur = async ( // fieldName: keyof T, // groupIndex: number // ): Promise => { // const fieldConfig = config.fields?.[fieldName]; // const shouldValidate = fieldConfig?.validateOnBlur ?? config.validateOnBlur; // if (!shouldValidate) { // return { isValid: true }; // } // const fieldPath = `groups.${groupIndex}.data.${String( // fieldName // )}` as FormDataPath; // const fieldValue = methods.getValues(fieldPath) as T[keyof T]; // return validation.validateField(fieldName, fieldValue, groupIndex); // }; // const handleSubmit = ( // onValid: (data: { groups: FormGroup[] }) => Promise, // onInvalid?: SubmitErrorHandler<{ groups: FormGroup[] }> // ) => { // return methods.handleSubmit(async (data) => { // try { // await onValid(data); // } catch (error) { // const message = // error instanceof Error ? error.message : "Submission failed"; // methods.setError("root", { type: "submit", message }); // } // }, onInvalid); // }; // const handleChange = ( // fieldName: keyof T, // groupIndex: number, // callback?: (value: T[keyof T]) => void // ) => { // const fieldPath = `groups.${groupIndex}.data.${String( // fieldName // )}` as FormDataPath; // return { // onChange: async (e: any) => { // const value = e?.target?.value; // methods.setValue(fieldPath, value, { // shouldValidate: true, // shouldDirty: true, // shouldTouch: true, // }); // callback?.(value); // }, // }; // }; // const handleFocus = ( // fieldName: keyof T, // groupIndex: number, // callback?: () => void // ) => { // return { // onFocus: () => { // methods.clearErrors( // `groups.${groupIndex}.data.${String(fieldName)}` as FormDataPath // ); // callback?.(); // }, // }; // }; // const resetField = (fieldName: keyof T, groupIndex: number) => { // const fieldPath = `groups.${groupIndex}.data.${String( // fieldName // )}` as FormDataPath; // methods.resetField(fieldPath); // }; // const setFieldValue = ( // fieldName: keyof T, // groupIndex: number, // value: T[keyof T], // options?: { // shouldValidate?: boolean; // shouldDirty?: boolean; // shouldTouch?: boolean; // } // ) => { // const fieldPath = `groups.${groupIndex}.data.${String( // fieldName // )}` as FormDataPath; // methods.setValue(fieldPath, value, options); // }; // const touchField = (fieldName: keyof T, groupIndex: number) => { // const fieldPath = `groups.${groupIndex}.data.${String( // fieldName // )}` as FormDataPath; // methods.setValue(fieldPath, methods.getValues(fieldPath), { // shouldTouch: true, // }); // }; // const resetForm = ( // defaultValues?: { groups: FormGroup[] }, // options?: { // keepDirty?: boolean; // keepErrors?: boolean; // keepValues?: boolean; // keepIsSubmitted?: boolean; // keepTouched?: boolean; // } // ) => { // methods.reset(defaultValues, options); // }; // return { // handleBlur, // handleSubmit, // handleChange, // handleFocus, // resetField, // setFieldValue, // touchField, // resetForm, // }; // };