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

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

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

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

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

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

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

export default useChallengesStepPresenter;
