"use client";

import { onMessage } from "firebase/messaging";
import { useEffect, type PropsWithChildren } from "react";

import { messaging } from "@/services/firebase";
import { toast } from "react-toastify";

const Notification = ({ children }: PropsWithChildren) => {
  useEffect(() => {
    const messagingInstance = messaging();
    if (messagingInstance) {
      onMessage(messagingInstance, (payload) => {
        console.log("Message received. ", payload);
        if (payload.notification) {
          toast.success(payload.notification.title ?? "New Notification");
        }
      });
    }
  }, []);

  return <>{children}</>;
};

export default Notification;
