import { EntityWithRoles } from "@/interfaces/entity";
import axios from "axios";
import { useCallback, useEffect, useState } from "react";

export default function useEntities(shouldNot?: boolean) {
  const [entities, setEntities] = useState<EntityWithRoles[]>([]);
  const [isLoading, setIsLoading] = useState(false);
  const [isError, setIsError] = useState(false);

  const getData = useCallback(() => {
    if (shouldNot) return;
    setIsLoading(true);
    axios
      .get<EntityWithRoles[]>("/api/entities?showRoles=true")
      .then((response) => setEntities(response.data))
      .finally(() => setIsLoading(false));
  }, [shouldNot]);

  useEffect(getData, [getData])

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