import { useEffect } from "react";
import { useCommonStore } from "@/zustand/commonStore";
import commonService from "@/lib/services/common";

export const useProductData = () => {
  const {
    deliveryTimeList,
    holidaysList,
    setDeliveryTimeList,
    setHolidaysList,
  } = useCommonStore();

  useEffect(() => {
    const fetchData = async () => {
      try {
        if (deliveryTimeList.length && holidaysList.length) return;

        const [deliveryTimeListResponse, holidaysListResponse] =
          await Promise.all([
            commonService.fetchDeliveryTimeList(),
            commonService.fetchHolidaysList(),
          ]);

        setDeliveryTimeList(deliveryTimeListResponse?.data || []);
        setHolidaysList(holidaysListResponse?.data || []);
      } catch (error) {
        console.error("Error fetching data:", error);
      }
    };

    fetchData();
  }, [
    setDeliveryTimeList,
    setHolidaysList,
    deliveryTimeList.length,
    holidaysList.length,
  ]);

  return { deliveryTimeList, holidaysList };
};
