"use client";
import * as React from "react";
import { apiCall } from "../utils/apiCall.js";
function useNotificationActions(notifications, setNotifications, setUnreadCount) {
  const markAsReadRef = React.useRef(async () => {
  });
  const dismissRef = React.useRef(async () => {
  });
  const [dismissUndo, setDismissUndo] = React.useState(null);
  const dismissUndoTimerRef = React.useRef(null);
  const markAsRead = React.useCallback(async (id) => {
    await apiCall(`/api/notifications/${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));
  }, [setNotifications, setUnreadCount]);
  React.useEffect(() => {
    markAsReadRef.current = markAsRead;
  }, [markAsRead]);
  const executeAction = React.useCallback(async (id, actionId) => {
    const result = await apiCall(
      `/api/notifications/${id}/action`,
      {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ actionId })
      }
    );
    if (result.ok) {
      setNotifications(
        (prev) => prev.map(
          (n) => n.id === id ? { ...n, status: "actioned", actionTaken: actionId } : n
        )
      );
      setUnreadCount((prev) => Math.max(0, prev - 1));
    }
    return { href: result.result?.href };
  }, [setNotifications, setUnreadCount]);
  const dismiss = React.useCallback(
    async (id) => {
      await apiCall(`/api/notifications/${id}/dismiss`, { method: "PUT" });
      const notification = notifications.find((n) => n.id === id);
      setNotifications((prev) => prev.filter((n) => n.id !== id));
      if (notification?.status === "unread") {
        setUnreadCount((prev) => Math.max(0, prev - 1));
      }
      if (notification) {
        const previousStatus = notification.status === "unread" ? "unread" : "read";
        setDismissUndo({ notification, previousStatus });
        if (dismissUndoTimerRef.current) {
          window.clearTimeout(dismissUndoTimerRef.current);
        }
        dismissUndoTimerRef.current = window.setTimeout(() => {
          setDismissUndo(null);
        }, 6e3);
      }
    },
    [notifications, setNotifications, setUnreadCount]
  );
  React.useEffect(() => {
    dismissRef.current = dismiss;
  }, [dismiss]);
  const undoDismiss = React.useCallback(async () => {
    if (!dismissUndo) return;
    await apiCall(`/api/notifications/${dismissUndo.notification.id}/restore`, {
      method: "PUT",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({ status: dismissUndo.previousStatus })
    });
    setNotifications((prev) => {
      const next = [
        {
          ...dismissUndo.notification,
          status: dismissUndo.previousStatus,
          readAt: dismissUndo.previousStatus === "unread" ? null : dismissUndo.notification.readAt ?? (/* @__PURE__ */ new Date()).toISOString()
        },
        ...prev.filter((n) => n.id !== dismissUndo.notification.id)
      ];
      return next.sort(
        (a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
      );
    });
    if (dismissUndo.previousStatus === "unread") {
      setUnreadCount((prev) => prev + 1);
    }
    if (dismissUndoTimerRef.current) {
      window.clearTimeout(dismissUndoTimerRef.current);
    }
    setDismissUndo(null);
  }, [dismissUndo, setNotifications, setUnreadCount]);
  const markAllRead = React.useCallback(async () => {
    await apiCall("/api/notifications/mark-all-read", { method: "PUT" });
    setNotifications(
      (prev) => prev.map(
        (n) => n.status === "unread" ? { ...n, status: "read", readAt: (/* @__PURE__ */ new Date()).toISOString() } : n
      )
    );
    setUnreadCount(0);
  }, [setNotifications, setUnreadCount]);
  React.useEffect(() => {
    return () => {
      if (dismissUndoTimerRef.current) {
        window.clearTimeout(dismissUndoTimerRef.current);
      }
    };
  }, []);
  return {
    markAsRead,
    executeAction,
    dismiss,
    dismissUndo,
    undoDismiss,
    markAllRead,
    markAsReadRef,
    dismissRef
  };
}
export {
  useNotificationActions
};
//# sourceMappingURL=useNotificationActions.js.map
