import { cn } from "./cva.js";
import * as React from "react";
import { cva } from "class-variance-authority";
import { jsx, jsxs } from "react/jsx-runtime";
import * as RadixToast from "@radix-ui/react-toast";
//#region src/Toast.tsx
const ToastContext = React.createContext(null);
const toastVariants = cva("pointer-events-auto flex w-full max-w-sm flex-col gap-1 rounded-md border border-border p-4 shadow-[var(--mod-shadow-sm)]", {
	variants: { tone: {
		neutral: "bg-surface text-fg",
		success: "bg-success text-success-fg",
		warning: "bg-warning text-warning-fg",
		danger: "bg-danger text-danger-fg"
	} },
	defaultVariants: { tone: "neutral" }
});
function nextToastId() {
	return globalThis.crypto?.randomUUID?.() ?? `toast-${Math.random().toString(36).slice(2)}`;
}
function ToastProvider({ children }) {
	const [toasts, setToasts] = React.useState([]);
	const dismiss = React.useCallback((id) => {
		setToasts((current) => id ? current.map((toast) => toast.id === id ? {
			...toast,
			open: false
		} : toast) : current.map((toast) => ({
			...toast,
			open: false
		})));
	}, []);
	const toast = React.useCallback((options) => {
		const id = nextToastId();
		setToasts((current) => [...current, {
			...options,
			id,
			open: true
		}]);
	}, []);
	const handleOpenChange = React.useCallback((id, open) => {
		if (!open) setToasts((current) => current.filter((item) => item.id !== id));
	}, []);
	return /* @__PURE__ */ jsx(ToastContext.Provider, {
		value: {
			toast,
			dismiss
		},
		children: /* @__PURE__ */ jsxs(RadixToast.Provider, {
			swipeDirection: "right",
			children: [
				children,
				toasts.map((item) => /* @__PURE__ */ jsx(ToastItem, {
					item,
					onOpenChange: handleOpenChange
				}, item.id)),
				/* @__PURE__ */ jsx(RadixToast.Viewport, { className: "fixed bottom-0 right-0 z-50 flex max-h-screen w-full flex-col-reverse gap-2 p-4 sm:max-w-sm" })
			]
		})
	});
}
function ToastItem({ item, onOpenChange }) {
	const tone = item.tone ?? "neutral";
	const type = tone === "danger" ? "foreground" : "background";
	return /* @__PURE__ */ jsxs(RadixToast.Root, {
		open: item.open,
		type,
		duration: item.durationMs,
		className: cn(toastVariants({ tone })),
		onOpenChange: (open) => onOpenChange(item.id, open),
		children: [
			/* @__PURE__ */ jsx(RadixToast.Title, {
				className: "text-sm font-semibold",
				children: item.title
			}),
			item.description ? /* @__PURE__ */ jsx(RadixToast.Description, {
				className: "text-sm opacity-90",
				children: item.description
			}) : null,
			/* @__PURE__ */ jsx(RadixToast.Close, {
				"aria-label": "Dismiss notification",
				className: "absolute right-2 top-2 rounded-sm text-inherit opacity-70 hover:bg-[var(--mod-color-neutral-a3)] active:bg-[var(--mod-color-neutral-a4)] focus-visible:outline-[var(--mod-focus-ring-width)] focus-visible:outline-[var(--mod-focus-ring-color)] focus-visible:outline-offset-[var(--mod-focus-ring-offset)]",
				children: "×"
			})
		]
	});
}
function useToast() {
	const context = React.useContext(ToastContext);
	if (!context) throw new Error("useToast must be used within ToastProvider");
	return context;
}
//#endregion
export { ToastProvider, useToast };
