/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-d4ad2ea | Cube Dev Team */
import { useEvent } from "../../../_internal/hooks/use-event.js";
import { ItemAction } from "../../actions/ItemAction/ItemAction.js";
import { createContext, useContext, useMemo } from "react";
import { jsx } from "react/jsx-runtime";

//#region src/components/overlays/Notifications/NotificationAction.tsx
/**
* Optional callback invoked BEFORE any action's onPress handler.
* Provided by PersistentNotificationsList so the parent (e.g. popover)
* can close itself when an action is triggered.
*/
const NotificationActionInterceptorContext = createContext(null);
const NotificationDismissContext = createContext(null);
function NotificationDismissProvider({ notificationId, onDismiss, onRestore, children }) {
	const dismiss = useEvent((reason) => {
		onDismiss(notificationId, reason);
	});
	const restore = useEvent(() => {
		onRestore?.(notificationId);
	});
	const value = useMemo(() => ({
		dismiss,
		restore
	}), [dismiss, restore]);
	return /* @__PURE__ */ jsx(NotificationDismissContext.Provider, {
		value,
		children
	});
}
const DismissActionDetectedContext = createContext(null);
/**
* Action button for use inside Notification components.
* Wraps ItemAction with auto-dismiss behavior.
*
* - `closeOnPress` (default: true) — auto-dismisses the notification after `onPress`.
* - An action with no `onPress` and `closeOnPress: true` acts as a dismiss-only action.
* - `isDismiss` — marks this action as the dismiss button; when present, the default
*   "Dismiss" button is auto-suppressed via context detection.
* - Type (primary/secondary/etc.) is set automatically via ItemActionProvider context.
*/
function NotificationAction({ children, onPress, closeOnPress = true, isDisabled, isDismiss }) {
	const dismissCtx = useContext(NotificationDismissContext);
	const dismissDetectedRef = useContext(DismissActionDetectedContext);
	if (isDismiss && dismissDetectedRef) dismissDetectedRef.current = true;
	const actionInterceptor = useContext(NotificationActionInterceptorContext);
	return /* @__PURE__ */ jsx(ItemAction, {
		type: "secondary",
		isDisabled,
		onPress: useEvent(async () => {
			actionInterceptor?.();
			if (closeOnPress || actionInterceptor) dismissCtx?.dismiss(isDismiss ? "close" : "action");
			if (await onPress?.() === false && (closeOnPress || actionInterceptor)) dismissCtx?.restore();
		}),
		children
	});
}

//#endregion
export { DismissActionDetectedContext, NotificationAction, NotificationActionInterceptorContext, NotificationDismissProvider };
//# sourceMappingURL=NotificationAction.js.map