"use client";
import * as React from "react";
import { apiCall } from "../utils/apiCall.js";
import {
  subscribeNotificationNew,
  emitNotificationCountChanged
} from "@open-mercato/shared/lib/frontend/notificationEvents";
import { useAppEvent } from "../injection/useAppEvent.js";
import {
  dispatchNotificationHandlers,
  getRequiredNotificationHandlerFeatures
} from "./NotificationDispatcher.js";
import { useNotificationActions } from "./useNotificationActions.js";
function useNotificationsSse() {
  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 fetched = notifResult.result.items;
        setNotifications(fetched);
        if (fetched.length > 0) {
          lastIdRef.current = fetched[0].id;
          dispatchNotificationHandlers(fetched, {
            features: grantedFeaturesRef.current,
            currentPath: typeof window === "undefined" ? "/" : `${window.location.pathname}${window.location.search}`,
            refreshNotifications: () => {
            },
            navigate: (href) => {
              if (typeof window === "undefined" || !href.startsWith("/")) return;
              window.location.assign(href);
            },
            markAsRead: async (notificationId) => markAsReadRef.current(notificationId),
            dismiss: async (notificationId) => dismissRef.current(notificationId)
          });
        }
      }
      if (countResult.ok && countResult.result) {
        setUnreadCount(countResult.result.unreadCount);
      }
      setError(null);
    } catch (err) {
      setError(err instanceof Error ? err.message : "Failed to fetch notifications");
    } finally {
      setIsLoading(false);
    }
  }, []);
  const refresh = React.useCallback(() => {
    void 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(() => {
    void fetchNotifications();
  }, [fetchNotifications]);
  React.useEffect(() => {
    const unsub = subscribeNotificationNew(() => refresh());
    return unsub;
  }, [refresh]);
  React.useEffect(() => {
    if (unreadCount === prevUnreadRef.current) return;
    prevUnreadRef.current = unreadCount;
    emitNotificationCountChanged(unreadCount);
  }, [unreadCount]);
  useAppEvent(
    "notifications.notification.created",
    (event) => {
      const payload = event.payload;
      const notification = payload?.notification;
      if (!notification || !notification.id) {
        void fetchNotifications();
        return;
      }
      if (notification.id === lastIdRef.current) return;
      lastIdRef.current = notification.id;
      setNotifications((prev) => [notification, ...prev.filter((item) => item.id !== notification.id)].slice(0, 50));
      if (notification.status === "unread") {
        setUnreadCount((prev) => prev + 1);
      }
      setHasNew(true);
      window.setTimeout(() => setHasNew(false), 3e3);
      dispatchNotificationHandlers([notification], {
        features: grantedFeaturesRef.current,
        currentPath: typeof window === "undefined" ? "/" : `${window.location.pathname}${window.location.search}`,
        refreshNotifications: () => {
        },
        navigate: (href) => {
          if (typeof window === "undefined" || !href.startsWith("/")) return;
          window.location.assign(href);
        },
        markAsRead: async (notificationId) => markAsReadRef.current(notificationId),
        dismiss: async (notificationId) => dismissRef.current(notificationId)
      });
    },
    [fetchNotifications]
  );
  useAppEvent(
    "notifications.notification.batch_created",
    (event) => {
      const payload = event.payload;
      if (!payload || typeof payload !== "object") return;
      void fetchNotifications();
    },
    [fetchNotifications]
  );
  useAppEvent("om:bridge:reconnected", () => {
    void fetchNotifications();
  }, [fetchNotifications]);
  React.useEffect(() => {
    const onFocus = () => {
      void fetchNotifications();
    };
    window.addEventListener("focus", onFocus);
    return () => window.removeEventListener("focus", onFocus);
  }, [fetchNotifications]);
  return {
    notifications,
    unreadCount,
    hasNew,
    isLoading,
    error,
    refresh,
    markAsRead,
    executeAction,
    dismiss,
    dismissUndo,
    undoDismiss,
    markAllRead
  };
}
export {
  useNotificationsSse
};
//# sourceMappingURL=useNotificationsSse.js.map
