"use client";
import { ForwardedRef, useImperativeHandle } from "react";
import { useForm } from "react-hook-form";
import useReportStepValidity from "./useReportStepValidity";

export interface IHeroStepFields {
  title: string;
  description: string;
  button_text: string;
  button_link: string;
  gif_file: string;
}

const useHeroStepPresenter = (
  initialData?: IHeroStepFields,
  ref?: ForwardedRef<unknown>,
  onValidityChange?: (valid: boolean) => void,
) => {
  const {
    register,
    handleSubmit,
    formState: { errors, isValid },
    getValues,
    trigger,
  } = useForm<IHeroStepFields>({
    defaultValues: initialData,
    mode: "onChange",
  });

  useReportStepValidity(isValid, onValidityChange);

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

  useImperativeHandle(ref, () => ({
    getData: () => getValues(),
    validate: () => trigger(),
  }));

  return {
    register,
    handleSubmit,
    errors,
    onSubmit,
  };
};

export default useHeroStepPresenter;
