"use client";
import { jsx } from "react/jsx-runtime";
import * as React from "react";
import { Notification } from "./notification.js";
const NotificationContext = React.createContext(null);
function generateId() {
  if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
    return crypto.randomUUID();
  }
  return `notif-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
}
function NotificationProvider({ children, maxVisible = 5 }) {
  const [notifications, setNotifications] = React.useState([]);
  const timersRef = React.useRef(/* @__PURE__ */ new Map());
  const clearTimer = React.useCallback((id) => {
    const t = timersRef.current.get(id);
    if (t) {
      clearTimeout(t);
      timersRef.current.delete(id);
    }
  }, []);
  const dismiss = React.useCallback(
    (id) => {
      clearTimer(id);
      setNotifications((prev) => prev.filter((n) => n.id !== id));
    },
    [clearTimer]
  );
  const notify = React.useCallback(
    (options) => {
      const id = generateId();
      setNotifications((prev) => {
        const next = [...prev, { ...options, id }];
        return next.length > maxVisible ? next.slice(next.length - maxVisible) : next;
      });
      if (typeof options.autoDismissMs === "number" && options.autoDismissMs > 0) {
        const t = setTimeout(() => dismiss(id), options.autoDismissMs);
        timersRef.current.set(id, t);
      }
      return id;
    },
    [dismiss, maxVisible]
  );
  const dismissAll = React.useCallback(() => {
    timersRef.current.forEach((t) => clearTimeout(t));
    timersRef.current.clear();
    setNotifications([]);
  }, []);
  React.useEffect(() => {
    return () => {
      timersRef.current.forEach((t) => clearTimeout(t));
      timersRef.current.clear();
    };
  }, []);
  const value = React.useMemo(
    () => ({ notifications, notify, dismiss, dismissAll }),
    [notifications, notify, dismiss, dismissAll]
  );
  return /* @__PURE__ */ jsx(NotificationContext.Provider, { value, children });
}
function useNotification() {
  const ctx = React.useContext(NotificationContext);
  if (!ctx) {
    throw new Error("useNotification must be used within a NotificationProvider");
  }
  return ctx;
}
const placementClasses = {
  "top-right": "top-4 right-4 items-end",
  "top-left": "top-4 left-4 items-start",
  "bottom-right": "bottom-4 right-4 items-end",
  "bottom-left": "bottom-4 left-4 items-start",
  "top-center": "top-4 left-1/2 -translate-x-1/2 items-center",
  "bottom-center": "bottom-4 left-1/2 -translate-x-1/2 items-center"
};
function NotificationStack({
  placement = "top-right",
  maxWidth = 380,
  className
}) {
  const { notifications, dismiss } = useNotification();
  if (notifications.length === 0) return null;
  return /* @__PURE__ */ jsx(
    "div",
    {
      className: [
        "pointer-events-none fixed z-toast flex flex-col gap-2",
        placementClasses[placement],
        className
      ].filter(Boolean).join(" "),
      "data-slot": "notification-stack",
      "data-placement": placement,
      children: notifications.map((entry) => /* @__PURE__ */ jsx(
        "div",
        {
          className: "pointer-events-auto w-full",
          style: { maxWidth },
          children: /* @__PURE__ */ jsx(
            Notification,
            {
              ...entry,
              onDismiss: () => dismiss(entry.id)
            }
          )
        },
        entry.id
      ))
    }
  );
}
export {
  NotificationProvider,
  NotificationStack,
  useNotification
};
//# sourceMappingURL=notification-stack.js.map
