"use client";
import * as React from "react";
import { apiCall } from "../../backend/utils/apiCall.js";
const POLL_INTERVAL = 8e3;
const BASE = "/api/customer_accounts/portal/notifications";
async function fetchJson(url, init) {
  try {
    const { ok, result } = await apiCall(url, init);
    if (!ok) return null;
    return result;
  } catch {
    return null;
  }
}
function usePortalNotifications() {
  const [notifications, setNotifications] = React.useState([]);
  const [unreadCount, setUnreadCount] = React.useState(0);
  const [hasNew, setHasNew] = React.useState(false);
  const [isLoading, setIsLoading] = React.useState(true);
  const lastIdRef = React.useRef(null);
  const fetchAll = React.useCallback(async () => {
    const [listData, countData] = await Promise.all([
      fetchJson(`${BASE}?pageSize=50`),
      fetchJson(`${BASE}/unread-count`)
    ]);
    if (listData?.ok && listData.items) {
      const items = listData.items;
      if (lastIdRef.current && items.length > 0 && items[0].id !== lastIdRef.current) {
        setHasNew(true);
        setTimeout(() => setHasNew(false), 3e3);
      }
      if (items.length > 0) lastIdRef.current = items[0].id;
      setNotifications(items);
    }
    if (countData?.ok) {
      setUnreadCount(countData.unreadCount);
    }
    setIsLoading(false);
  }, []);
  React.useEffect(() => {
    fetchAll();
    const interval = setInterval(fetchAll, POLL_INTERVAL);
    return () => clearInterval(interval);
  }, [fetchAll]);
  React.useEffect(() => {
    const handler = (e) => {
      const detail = e.detail;
      if (detail?.id === "notifications.notification.created" || detail?.id === "notifications.notification.batch_created") {
        fetchAll();
      }
    };
    window.addEventListener("om:portal-event", handler);
    return () => window.removeEventListener("om:portal-event", handler);
  }, [fetchAll]);
  const markAsRead = React.useCallback(async (id) => {
    await fetchJson(`${BASE}/${id}/read`, { method: "PUT" });
    setNotifications(
      (prev) => prev.map((n) => n.id === id ? { ...n, status: "read", readAt: (/* @__PURE__ */ new Date()).toISOString() } : n)
    );
    setUnreadCount((prev) => Math.max(0, prev - 1));
  }, []);
  const dismiss = React.useCallback(async (id) => {
    await fetchJson(`${BASE}/${id}/dismiss`, { method: "PUT" });
    setNotifications((prev) => prev.filter((n) => n.id !== id));
    setUnreadCount((prev) => {
      const wasDismissedUnread = notifications.find((n) => n.id === id)?.status === "unread";
      return wasDismissedUnread ? Math.max(0, prev - 1) : prev;
    });
  }, [notifications]);
  const markAllRead = React.useCallback(async () => {
    await fetchJson(`${BASE}/mark-all-read`, { method: "PUT" });
    setNotifications(
      (prev) => prev.map((n) => n.status === "unread" ? { ...n, status: "read", readAt: (/* @__PURE__ */ new Date()).toISOString() } : n)
    );
    setUnreadCount(0);
  }, []);
  const refresh = React.useCallback(() => {
    fetchAll();
  }, [fetchAll]);
  return { notifications, unreadCount, hasNew, isLoading, refresh, markAsRead, dismiss, markAllRead };
}
export {
  usePortalNotifications
};
//# sourceMappingURL=usePortalNotifications.js.map
