"use client";
import * as React from "react";
import { apiCall } from "../utils/apiCall.js";
import {
  subscribeNotificationNew,
  emitNotificationCountChanged
} from "@open-mercato/shared/lib/frontend/notificationEvents";
import {
  dispatchNotificationHandlers,
  getRequiredNotificationHandlerFeatures
} from "./NotificationDispatcher.js";
import { useNotificationActions } from "./useNotificationActions.js";
const POLL_INTERVAL = 5e3;
function useNotificationsPoll() {
  const [notifications, setNotifications] = React.useState([]);
  const [unreadCount, setUnreadCount] = React.useState(0);
  const [hasNew, setHasNew] = React.useState(false);
  const [isLoading, setIsLoading] = React.useState(true);
  const [error, setError] = React.useState(null);
  const grantedFeaturesRef = React.useRef([]);
  const lastIdRef = React.useRef(null);
  const prevUnreadRef = React.useRef(0);
  const {
    markAsRead,
    executeAction,
    dismiss,
    dismissUndo,
    undoDismiss,
    markAllRead,
    markAsReadRef,
    dismissRef
  } = useNotificationActions(notifications, setNotifications, setUnreadCount);
  const fetchNotifications = React.useCallback(async () => {
    try {
      const [notifResult, countResult] = await Promise.all([
        apiCall("/api/notifications?pageSize=50"),
        apiCall("/api/notifications/unread-count")
      ]);
      if (notifResult.ok && notifResult.result) {
        const newNotifications = notifResult.result.items;
        if (lastIdRef.current && newNotifications.length > 0) {
          const firstId = newNotifications[0].id;
          if (firstId !== lastIdRef.current) {
            setHasNew(true);
            setTimeout(() => setHasNew(false), 3e3);
          }
        }
        if (newNotifications.length > 0) {
          lastIdRef.current = newNotifications[0].id;
        }
        setNotifications(newNotifications);
        if (newNotifications.length > 0) {
          dispatchNotificationHandlers(newNotifications, {
            features: grantedFeaturesRef.current,
            currentPath: typeof window === "undefined" ? "/" : `${window.location.pathname}${window.location.search}`,
            refreshNotifications: () => {
              void fetchNotifications();
            },
            navigate: (href) => {
              if (typeof window === "undefined") return;
              if (!href.startsWith("/")) return;
              window.location.assign(href);
            },
            markAsRead: async (notificationId) => markAsReadRef.current(notificationId),
            dismiss: async (notificationId) => dismissRef.current(notificationId)
          });
        }
      }
      if (countResult.ok && countResult.result) {
        const newCount = countResult.result.unreadCount;
        if (newCount !== prevUnreadRef.current) {
          setUnreadCount(newCount);
          prevUnreadRef.current = newCount;
          emitNotificationCountChanged(newCount);
        }
      }
      setError(null);
    } catch (err) {
      setError(err instanceof Error ? err.message : "Failed to fetch notifications");
    } finally {
      setIsLoading(false);
    }
  }, []);
  const refresh = React.useCallback(() => {
    fetchNotifications();
  }, [fetchNotifications]);
  React.useEffect(() => {
    let mounted = true;
    const run = async () => {
      const requiredFeatures = getRequiredNotificationHandlerFeatures();
      if (requiredFeatures.length === 0) {
        grantedFeaturesRef.current = [];
        return;
      }
      const response = await apiCall("/api/auth/feature-check", {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ features: requiredFeatures })
      });
      if (!mounted) return;
      grantedFeaturesRef.current = response.ok ? response.result?.granted ?? [] : [];
    };
    void run();
    return () => {
      mounted = false;
    };
  }, []);
  React.useEffect(() => {
    fetchNotifications();
    const interval = setInterval(fetchNotifications, POLL_INTERVAL);
    return () => clearInterval(interval);
  }, [fetchNotifications]);
  React.useEffect(() => {
    const unsub = subscribeNotificationNew(() => refresh());
    return unsub;
  }, [refresh]);
  return {
    notifications,
    unreadCount,
    hasNew,
    isLoading,
    error,
    refresh,
    markAsRead,
    executeAction,
    dismiss,
    dismissUndo,
    undoDismiss,
    markAllRead
  };
}
export {
  useNotificationsPoll
};
//# sourceMappingURL=useNotificationsPoll.js.map
