Source: data/retrievefoodpoints.js

import { getDatabase, ref, child, get } from "firebase/database";
import {
  ref as storeRef,
  uploadString,
  getDownloadURL,
  getStorage,
  uploadBytes,
  deleteObject,
  getMetadata,
} from "firebase/storage";
import { useEffect, useState } from "react";

const database = getDatabase();
const storage = getStorage();

/**
 * The function `getAllFoodPoints` retrieves all food points data from a Firebase Realtime Database and
 * returns it as an array of objects.
 * @returns The `getAllFoodPoints` function returns an array of objects representing food points data
 * retrieved from the database. If no food points are found in the database, it returns `null`. If
 * there is an error during the retrieval process, the function will log an error message and propagate
 * the error to the caller.
 */
export async function getAllFoodPoints() {
  const foodpointsRef = ref(database, "foodpoints");

  try {
    const snapshot = await get(foodpointsRef);
    if (snapshot.exists()) {
      const foodpointsData = snapshot.val();
      // Convert the object of objects to an array of objects
      const foodpointsArray = Object.values(foodpointsData);
      return foodpointsArray;
    } else {
      console.log("No food points found in the database.");
      return null;
    }
  } catch (error) {
    console.error("Error retrieving food points from database:", error);
    throw error; // Propagate the error to the caller
  }
}

/**
 * The function `restaurantLink` asynchronously retrieves the download URL for a restaurant icon image
 * stored in Firebase Storage.
 * @returns The `restaurantLink` function returns a link to download the restaurant icon image from the
 * specified storage location.
 */
export const restaurantLink = async () => {
  const storageRef = storeRef(storage, `mapicons/restaurant_icon.png`);
  const link = await getDownloadURL(storageRef);
  return link;
};