/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.0.0-canary-3803e7e
 * Released under the MIT license.
 */

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useContext, useRef } from 'react';
import { tasty } from '../../../tasty/index.js';
import { ItemActionProvider } from '../../actions/ItemActionContext.js';
import { Block } from '../../Block.js';
import { Item } from '../../content/Item/Item.js';
import { Flex } from '../../layout/Flex.js';
import { Space } from '../../layout/Space.js';
import { getThemeIcon } from '../Toast/useToast.js';
import { DismissActionDetectedContext, NotificationAction, NotificationDismissProvider, } from './NotificationAction.js';
// ─── Styled Components ──────────────────────────────────────────────
const StyledItem = tasty(Item, {
    styles: {
        width: 'max min((100vw - 4x), 50x)',
        shadow: '$card-shadow',
        transition: 'theme, inset',
        Description: {
            preset: 't4',
        },
    },
});
// ─── Dismiss Action Detection ────────────────────────────────────────
/**
 * 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);
    // Reset each render so detection is fresh
    ref.current = false;
    return (_jsx(DismissActionDetectedContext.Provider, { value: ref, children: 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() {
    const dismissDetectedRef = useContext(DismissActionDetectedContext);
    if (dismissDetectedRef?.current)
        return null;
    return _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 `isDismissible`. The `showAutoDismiss` flag only controls the
 * auto-appended "Dismiss" button.
 */
function ActionsSection({ actions, theme, showAutoDismiss, hasDismissContext, notificationId, onDismiss, }) {
    const actionsContent = (_jsxs(Space, { placeContent: "end", flexGrow: 1, children: [actions, showAutoDismiss && _jsx(AutoDismissButton, {})] }));
    const wrappedContent = showAutoDismiss ? (_jsx(DismissActionDetector, { children: actionsContent })) : (actionsContent);
    return (_jsx(ItemActionProvider, { type: "card", theme: theme, children: hasDismissContext ? (_jsx(NotificationDismissProvider, { notificationId: notificationId, onDismiss: onDismiss, children: wrappedContent })) : (wrappedContent) }));
}
/**
 * Shared presentational card used by both overlay NotificationItem
 * and PersistentNotificationListItem.
 */
export function NotificationCard({ qa = 'Notification', theme, title, description, icon: providedIcon, actions, isDismissible = true, notificationId, onDismiss, suffix, }) {
    const icon = getThemeIcon(theme, providedIcon);
    const hasDismissContext = notificationId != null && onDismiss != null;
    const showAutoDismiss = isDismissible && hasDismissContext;
    const hasActions = !!(actions || showAutoDismiss);
    const descriptionContent = description || hasActions ? (_jsxs(Flex, { flow: "row wrap", gap: "0.5x 1x", placeItems: "center stretch", children: [description && _jsx(Block, { children: description }), hasActions && (_jsx(ActionsSection, { actions: actions, theme: theme, showAutoDismiss: showAutoDismiss, hasDismissContext: hasDismissContext, notificationId: notificationId, onDismiss: onDismiss }))] })) : undefined;
    return (_jsx(StyledItem, { qa: qa, type: "card", theme: theme, icon: icon, description: descriptionContent, suffix: suffix, children: title }));
}

        