"use client";
import { useFieldArray, useForm } from "react-hook-form";

export interface IBenefitsStepFields {
  title: string;
  description: string;
  cards: {
    icon: string;
    title: string;
    description: string;
  }[];
}

const useBenefitsStepPresenter = (initialData?: IBenefitsStepFields) => {
  const {
    register,
    handleSubmit,
    control,
    watch,
    formState: { errors },
    getValues,
  } = useForm<IBenefitsStepFields>({
    defaultValues: initialData || {
      cards: [{ icon: "", title: "", description: "" }],
    },
  });

  const { fields, append, remove } = useFieldArray({
    control,
    name: "cards",
  });

  const onSubmit = (data: IBenefitsStepFields) => {
    console.log(data);
  };

  const collectData = async () => {
    return getValues();
  };

  return {
    register,
    handleSubmit,
    errors,
    onSubmit,
    fields,
    append,
    remove,
    control,
    watch,
    getValues,
    collectData,
  };
};

export default useBenefitsStepPresenter;
