Source: data/retrievewaterpoints.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 `getAllWaterpoints` retrieves all water points from a database and returns them as an
 * array of objects.
 * @returns The `getAllWaterpoints` function returns an array of waterpoints if they exist in the
 * database. If no water points are found, it logs a message saying "No water points found in the
 * database" and returns `null`. If there is an error retrieving the water points from the database, it
 * logs the error and throws it to propagate it to the caller.
 */
export async function getAllWaterpoints() {
  const waterpointsRef = ref(database, "waterpoints");

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

/**
 * The function `waterpointLink` asynchronously retrieves the download URL for a specific image stored
 * in Firebase storage.
 * @returns The `waterpointLink` function returns a URL link to the image file `water_points.png`
 * stored in the `mapicons` folder in the storage.
 */
export const waterpointLink = async () => {
  const storageRef = storeRef(storage, `mapicons/water_points.png`);
  const link = await getDownloadURL(storageRef);
  return link;
};