"use client";
import { useState, useEffect } from "react";
import axios from "axios";

const BASE_URL_API = process.env.NEXT_PUBLIC_BASE_URL_API;

export default function useFetch(apiEndpoint) {
  const [data, setData] = useState([]);
  const [loading, setLoading] = useState(true);

  const fetchData = async () => {
    try {
      setLoading(true);
      const result = await axios.get(`${BASE_URL_API}${apiEndpoint}`);
      setData(result?.data?.data || []);
      // console.log("Fetched Data:", result?.data);
    } catch (error) {
      console.error("API Fetch Error:", error);
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    fetchData();
  }, [apiEndpoint]);

  return { data, loading, refetch: fetchData };
}
