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

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

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

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

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

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

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

export default useFeaturesStepPresenter;
