"use client";
import {
  useCreateCompanyCategory,
  useUpdateCompanyCategories,
} from "@/hooks/queries";
import { TCreateCompanyCategoryCredentials } from "@/lib/api";
import { useRouter } from "next/navigation";
import { SubmitHandler, useForm } from "react-hook-form";

interface IProps {
  editMode?: boolean;
  defaultValues?: TCreateCompanyCategoryCredentials;
  id?: string;
}

const useCreateCompanyCategoryPresenter = ({
  defaultValues,
  editMode,
  id,
}: IProps) => {
  const {
    register,
    handleSubmit,
    formState: { errors },
  } = useForm<TCreateCompanyCategoryCredentials>({
    mode: "onChange",
    defaultValues,
  });
  const router = useRouter();
  const { mutate } = useCreateCompanyCategory();
  const { mutate: editCategory } = useUpdateCompanyCategories(id as string);
  const onSubmit: SubmitHandler<TCreateCompanyCategoryCredentials> = (data) => {
    if (editMode) {
      editCategory({ id: id as string, credentials: data });
    } else {
      mutate(data);
    }
  };
  return {
    register,
    onSubmit,
    handleSubmit,
    errors,
    router,
  };
};

export default useCreateCompanyCategoryPresenter;
