import { Assignment } from "@/interfaces/results";
import axios from "axios";
import { useEffect, useState } from "react";

export default function useAssignmentsCorporates({
  corporates,
}: {
  corporates: string[];
}) {
  const [assignments, setAssignments] = useState<Assignment[]>([]);
  const [isLoading, setIsLoading] = useState(false);
  const [isError, setIsError] = useState(false);

  const getData = () => {
    if (corporates.length === 0) {
      setAssignments([]);
      return;
    }

    setIsLoading(true);
    axios
      .get<Assignment[]>(
        `/api/assignments/corporate?ids=${corporates.join(",")}`
      )
      .then(async (response) => {
        setAssignments(response.data);
      })
      .finally(() => setIsLoading(false));
  };

  useEffect(getData, [corporates]);

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