import { useEffect, useState } from "react";
import { GET_ROLE_LISTS } from "@/core/utils/routes";
import useRequest from "@/lib/hooks/useRequest";

const useRoles = () => {
  const requestServer = useRequest();
  const [roles, setRoles] = useState([]);
  const [loadingRoles, setLoadingRoles] = useState(true);
  const [errorRoles, setErrorRoles] = useState(null);

  useEffect(() => {
    const fetchRoles = async () => {
      try {
        const response = await requestServer(`${GET_ROLE_LISTS}`);
        setRoles(response.data.data);
        setLoadingRoles(false);
      } catch (e) {
        setErrorRoles(e);
        setLoadingRoles(false);
      }
    };

    fetchRoles();
  }, []);

  return { roles, loadingRoles, errorRoles };
};

export default useRoles;
