import { WithLabeledEntities } from "@/interfaces/entity";
import { Type, User } from "@/interfaces/user";
import axios from "axios";
import { useEffect, useState } from "react";

export default function useEntitiesUsers(type?: Type) {
  const [users, setUsers] = useState<WithLabeledEntities<User>[]>([]);
  const [isLoading, setIsLoading] = useState(false);
  const [isError, setIsError] = useState(false);

  const getData = () => {
    setIsLoading(true);
    axios
      .get<WithLabeledEntities<User>[]>(
        `/api/entities/users${type ? "?type=" + type : ""}`
      )
      .then((response) => setUsers(response.data))
      .finally(() => setIsLoading(false));
  };

  useEffect(getData, [type]);

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