"use client";
import * as React from "react";
import { useAppEvent } from "../injection/useAppEvent.js";
import { subscribeNotificationEffects } from "./NotificationDispatcher.js";
const NOTIFICATION_CREATED_EVENT = "notifications.notification.created";
const MAX_SEEN_IDS = 200;
function matchesType(pattern, type) {
  const patterns = Array.isArray(pattern) ? pattern : [pattern];
  return patterns.some((current) => {
    if (current === "*") return true;
    if (current.endsWith(".*")) return type.startsWith(current.slice(0, -1));
    return current === type;
  });
}
function readNotificationFromEvent(event) {
  const payload = event.payload;
  if (!payload || typeof payload !== "object") return null;
  const notification = payload.notification;
  if (!notification || typeof notification !== "object") return null;
  const candidate = notification;
  if (typeof candidate.id !== "string" || typeof candidate.type !== "string") return null;
  return candidate;
}
function useNotificationEffect(notificationType, effect, deps = []) {
  const seenIdsRef = React.useRef([]);
  const handleNotification = React.useCallback((notification) => {
    if (!matchesType(notificationType, notification.type)) return;
    if (seenIdsRef.current.includes(notification.id)) return;
    seenIdsRef.current = [notification.id, ...seenIdsRef.current.filter((id) => id !== notification.id)].slice(0, MAX_SEEN_IDS);
    effect(notification);
  }, [notificationType, ...deps]);
  React.useEffect(() => {
    const unsubscribe = subscribeNotificationEffects(notificationType, handleNotification);
    return unsubscribe;
  }, [notificationType, handleNotification]);
  useAppEvent(
    NOTIFICATION_CREATED_EVENT,
    (event) => {
      const notification = readNotificationFromEvent(event);
      if (!notification) return;
      handleNotification(notification);
    },
    [handleNotification]
  );
}
export {
  useNotificationEffect
};
//# sourceMappingURL=useNotificationEffect.js.map
