/** @license MIT | @cube-dev/ui-kit v0.127.1 | Cube Dev Team */
import { Block } from "../../Block.js";
import { ItemActionProvider } from "../../actions/ItemActionContext.js";
import { Space } from "../../layout/Space.js";
import { _Item } from "../../content/Item/Item.js";
import { getThemeIcon } from "../Toast/useToast.js";
import { Flex } from "../../layout/Flex.js";
import { DismissActionDetectedContext, NotificationAction, NotificationDismissProvider } from "./NotificationAction.js";
import { tasty } from "@tenphi/tasty";
import { useContext, useRef } from "react";
import { jsx, jsxs } from "react/jsx-runtime";

//#region src/components/overlays/Notifications/NotificationCard.tsx
const StyledItem = tasty(_Item, { styles: {
	width: {
		"": "max min((100vw - 4x), 50x)",
		flat: "100%"
	},
	shadow: {
		"": "$shadow",
		flat: false
	},
	radius: {
		"": "1cr",
		flat: "0"
	},
	transition: "theme, inset",
	Description: { preset: "t4" }
} });
/**
* Provides a ref via context that NotificationAction children write to
* during render when `isDismiss` is set. The ref is reset each render.
*/
function DismissActionDetector({ children }) {
	const ref = useRef(false);
	ref.current = false;
	return /* @__PURE__ */ jsx(DismissActionDetectedContext.Provider, {
		value: ref,
		children
	});
}
/**
* Renders the default "Dismiss" button only if no sibling NotificationAction
* has `isDismiss` set. Reads from DismissActionDetectedContext ref which is
* populated by actions that rendered before this component (left-to-right order).
*/
function AutoDismissButton() {
	if (useContext(DismissActionDetectedContext)?.current) return null;
	return /* @__PURE__ */ jsx(NotificationAction, {
		isDismiss: true,
		children: "Dismiss"
	});
}
/**
* Extracted sub-component for the actions area of a notification card.
*
* The dismiss provider is always rendered when `hasDismissContext` is true,
* so any action with `closeOnPress` can close the notification — regardless
* of `isDismissable`. The `showAutoDismiss` flag only controls the
* auto-appended "Dismiss" button.
*/
function ActionsSection({ actions, theme, showAutoDismiss, hasDismissContext, notificationId, onDismiss, onRestore }) {
	const actionsContent = /* @__PURE__ */ jsxs(Space, {
		placeSelf: "end",
		placeContent: "end",
		flexGrow: 1,
		children: [actions, showAutoDismiss && /* @__PURE__ */ jsx(AutoDismissButton, {})]
	});
	const wrappedContent = showAutoDismiss ? /* @__PURE__ */ jsx(DismissActionDetector, { children: actionsContent }) : actionsContent;
	return /* @__PURE__ */ jsx(ItemActionProvider, {
		type: "card",
		theme,
		children: hasDismissContext ? /* @__PURE__ */ jsx(NotificationDismissProvider, {
			notificationId,
			onDismiss,
			onRestore,
			children: wrappedContent
		}) : wrappedContent
	});
}
/**
* Shared presentational card used by both overlay NotificationItem
* and PersistentNotificationListItem.
*/
function NotificationCard({ qa = "Notification", theme, title, description, icon: providedIcon, actions, isDismissable = true, elevated = true, notificationId, onDismiss, onRestore, suffix }) {
	const icon = getThemeIcon(theme, providedIcon);
	const hasDismissContext = notificationId != null && onDismiss != null;
	const showAutoDismiss = isDismissable && hasDismissContext;
	const hasActions = !!(actions || showAutoDismiss);
	return /* @__PURE__ */ jsx(StyledItem, {
		qa,
		type: "card",
		theme,
		icon,
		description: description || hasActions ? /* @__PURE__ */ jsxs(Flex, {
			flow: "row wrap",
			gap: "0.5x 1x",
			placeItems: "center stretch",
			children: [description && /* @__PURE__ */ jsx(Block, { children: description }), hasActions && /* @__PURE__ */ jsx(ActionsSection, {
				actions,
				theme,
				showAutoDismiss,
				hasDismissContext,
				notificationId,
				onDismiss,
				onRestore
			})]
		}) : void 0,
		suffix,
		mods: { flat: !elevated },
		styles: !elevated ? { border: "0" } : void 0,
		children: title
	});
}

//#endregion
export { NotificationCard };
//# sourceMappingURL=NotificationCard.js.map