/** @license MIT | @cube-dev/ui-kit v0.147.0 | 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 { useI18n } from "../../../i18n/useI18n.js";
import { getThemeIcon } from "../Toast/useToast.js";
import { Flex } from "../../layout/Flex.js";
import { NotificationAction, NotificationDismissProvider } from "./NotificationAction.js";
import { tasty } from "@tenphi/tasty";
import { isValidElement } 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" }
} });
/**
* Deterministically checks whether any `NotificationAction` with `isDismiss`
* is present in the `actions` ReactNode tree.
*
* Walks the static element tree (arrays, fragments, host elements, and
* composite wrappers via their `children` prop) instead of relying on
* render-phase ref mutation, so suppression does not depend on sibling render
* order or concurrent rendering timing.
*/
function hasDismissAction(node) {
	let found = false;
	const visit = (n) => {
		if (found || n == null || typeof n === "boolean" || typeof n === "string") return;
		if (Array.isArray(n)) {
			for (const child of n) visit(child);
			return;
		}
		if (isValidElement(n)) {
			if (n.type === NotificationAction && n.props.isDismiss) {
				found = true;
				return;
			}
			visit(n.props.children);
		}
	};
	visit(node);
	return found;
}
/**
* Renders the default "Dismiss" button. Suppression is decided by the caller
* via `hasDismissAction(actions)` — this component always renders the button.
*/
function AutoDismissButton() {
	const { t } = useI18n();
	return /* @__PURE__ */ jsx(NotificationAction, {
		isDismiss: true,
		children: t("notifications.dismiss", "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 hasDismiss = hasDismissAction(actions);
	const actionsContent = /* @__PURE__ */ jsxs(Space, {
		placeSelf: "end",
		placeContent: "end",
		flexGrow: 1,
		children: [actions, showAutoDismiss && !hasDismiss && /* @__PURE__ */ jsx(AutoDismissButton, {})]
	});
	return /* @__PURE__ */ jsx(ItemActionProvider, {
		type: "card",
		theme,
		children: hasDismissContext ? /* @__PURE__ */ jsx(NotificationDismissProvider, {
			notificationId,
			onDismiss,
			onRestore,
			children: actionsContent
		}) : actionsContent
	});
}
/**
* 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