import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
import axios from "axios";
import { useCallback, useEffect, useState } from "react";

export default function useApprovalWorkflows(entitiesString?: string) {
  const [workflows, setWorkflows] = useState<ApprovalWorkflow[]>([]);
  const [isLoading, setIsLoading] = useState(false);
  const [isError, setIsError] = useState(false);

  const getData = useCallback(() => {
    setIsLoading(true);
    axios
      .get<ApprovalWorkflow[]>(`/api/approval-workflows`, {params: { entityIds: entitiesString }})
      .then((response) => setWorkflows(response.data))
      .catch((error) => {
        setIsError(true);
      })
      .finally(() => setIsLoading(false));
  }, []);

  useEffect(getData, [getData]);

  return { workflows, isLoading, isError, reload: getData };
}
