import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2026 GoodData Corporation
import { useContext, useMemo } from "react";
import { useIntl } from "react-intl";
import { commonDialogMessages } from "../../locales.js";
import { bem } from "../@utils/bem.js";
import { UiIcon } from "../UiIcon/UiIcon.js";
import { UiToastContextInternal } from "./UiToastProvider.js";
const { b, e } = bem("gd-ui-kit-toast");
// Errors and warnings interrupt screen readers by default; success/info wait
// for the assistive-tech queue to be free. Callers can override per toast.
const DEFAULT_ARIA_LIVE = {
    success: "polite",
    info: "polite",
    warning: "assertive",
    error: "assertive",
};
/**
 * Presentational single-toast component. Most consumers use
 * `UiToastsContainer` (which reads queued toasts from the provider), but
 * this can be rendered directly for static demos or one-off composition.
 *
 * @internal
 */
export function UiToastItem({ toast, onClose }) {
    const intl = useIntl();
    const ariaLive = toast.accessibilityConfig?.ariaLive ?? DEFAULT_ARIA_LIVE[toast.kind];
    const role = toast.accessibilityConfig?.role ?? (ariaLive === "assertive" ? "alert" : "status");
    const closeLabel = toast.accessibilityConfig?.closeButtonLabel ?? intl.formatMessage(commonDialogMessages.close);
    return (_jsxs("div", { className: e("item", { kind: toast.kind }), role: role, "aria-live": ariaLive, "aria-label": toast.accessibilityConfig?.ariaLabel, "aria-labelledby": toast.accessibilityConfig?.ariaLabelledBy, "aria-describedby": toast.accessibilityConfig?.ariaDescribedBy, "data-testid": `gd-ui-kit-toast-${toast.kind}-${toast.id}`, children: [
            _jsx("span", { className: e("body"), children: toast.text }), toast.action ? (_jsx("button", { type: "button", className: e("action"), onClick: () => {
                    toast.action.onClick();
                    onClose(toast.id);
                }, children: toast.action.label })) : null, _jsx("button", { type: "button", className: e("close"), onClick: () => onClose(toast.id), "aria-label": closeLabel, children: _jsx(UiIcon, { type: "cross", size: 14, color: "currentColor" }) })
        ] }));
}
/**
 * Renders the list of toasts queued by `useUiToast`. Mount once near the app
 * root, inside a `UiToastProvider`. Nested containers render nothing — only
 * the outermost one is visible, preserving the topmost-only behaviour.
 *
 * @internal
 */
export function UiToastsContainer({ dataTestId }) {
    const ctx = useContext(UiToastContextInternal);
    // Render newest-first so the most recent notification sits at the top
    // of the stack — matches the legacy `ToastsCenter` overlay
    // (`messages.sort((a, b) => b.createdAt - a.createdAt)`). The queue
    // itself stays in insertion order for `push` / `remove` correctness;
    // only the rendered list is reversed.
    const items = useMemo(() => (ctx?.toasts ? [...ctx.toasts].reverse() : []), [ctx?.toasts]);
    if (!ctx || ctx.isNested) {
        return null;
    }
    return (_jsx("div", { className: b(), "data-testid": dataTestId, children: items.map((toast) => (_jsx(UiToastItem, { toast: toast, onClose: ctx.remove }, toast.id))) }));
}
