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

const useProvinces = () => {
  const requestServer = useRequest();
  const [provinces, setProvinces] = useState([]);
  const [loadingProvinces, setLoadingProvinces] = useState(true);
  const [errorProvinces, setErrorProvinces] = useState(null);

  useEffect(() => {
    const fetchProvinces = async () => {
      try {
        const response = await requestServer(`${GET_PROVINCE_LISTS}`);
        setProvinces(response.data.data);
        setLoadingProvinces(false);
      } catch (e) {
        setErrorProvinces(e);
        setLoadingProvinces(false);
      }
    };

    fetchProvinces();
  }, []);

  return { provinces, loadingProvinces, errorProvinces };
};

export default useProvinces;
