import { useState, useId } from 'react';
import { clsx } from 'clsx';
import Body from '../../body/Body.mjs';
import Button from '../../button/Button.resolver.mjs';
import Container from '../../container/Container.mjs';
import '../../common/theme.mjs';
import '../../common/direction.mjs';
import { Breakpoint } from '@transferwise/neptune-tokens';
import '../../common/propsValues/control.mjs';
import '../../common/propsValues/size.mjs';
import { Typography } from '../../common/propsValues/typography.mjs';
import '../../common/propsValues/width.mjs';
import '../../common/propsValues/type.mjs';
import '../../common/propsValues/dateMode.mjs';
import '../../common/propsValues/monthFormat.mjs';
import '../../common/propsValues/position.mjs';
import '../../common/propsValues/layouts.mjs';
import '../../common/propsValues/status.mjs';
import '../../common/propsValues/sentiment.mjs';
import '../../common/propsValues/profileType.mjs';
import '../../common/propsValues/variant.mjs';
import '../../common/propsValues/scroll.mjs';
import '../../common/propsValues/markdownNodeType.mjs';
import '../../common/fileType.mjs';
import '@transferwise/formatting';
import '@transferwise/icons';
import 'react-intl';
import '../../common/closeButton/CloseButton.messages.mjs';
import { jsxs, jsx } from 'react/jsx-runtime';
import { LiveRegion } from '../../common/liveRegion/LiveRegion.mjs';
import { PrimitivePrompt } from '../PrimitivePrompt/PrimitivePrompt.mjs';
import { renderPromptMedia } from '../helpers/promptMedia.mjs';
import { ExpanderToggle } from '../common/Expander/Expander.mjs';
import { buildAnnouncementString } from './helpers.mjs';
import { useContainerSize } from '../../common/hooks/useContainerSize.mjs';

const CriticalBanner = ({
  sentiment = 'negative',
  title,
  description,
  media = {},
  action,
  actionSecondary,
  expanded: expandedProp,
  onToggle,
  id,
  className,
  'data-testid': testId
}) => {
  const [containerRef, isDesktop] = useContainerSize(Breakpoint.MEDIUM);
  const isControlled = expandedProp !== undefined && onToggle !== undefined;
  const [internalExpanded, setInternalExpanded] = useState(true);
  const resolvedExpanded = isDesktop ? true : isControlled ? expandedProp : internalExpanded;
  const handleToggle = isControlled ? onToggle : () => setInternalExpanded(previousExpanded => !previousExpanded);
  const hasActions = action ?? actionSecondary;
  const mediaId = useId();
  const titleId = useId();
  const descId = useId();
  const ariaLabelledByIds = [media['aria-hidden'] ? undefined : mediaId, !title ? undefined : titleId].filter(Boolean).join(' ');
  const bannerSurface = /*#__PURE__*/jsxs(Container, {
    ref: containerRef,
    size: "fluid",
    as: PrimitivePrompt,
    id: id,
    sentiment: sentiment,
    emphasis: sentiment === 'neutral' ? 'base' : 'elevated',
    "data-testid": testId,
    className: clsx('wds-critical-banner', 'wds-critical-banner-overhang', {
      'wds-critical-banner--collapsed': !resolvedExpanded,
      'wds-critical-banner--with-two-actions': !!actionSecondary
    }, className),
    media: renderPromptMedia({
      media,
      sentiment,
      mediaId,
      imgClassName: 'wds-critical-banner--media-image'
    }),
    actions: hasActions ? /*#__PURE__*/jsxs("div", {
      "aria-hidden": !resolvedExpanded ? true : undefined,
      style: {
        display: 'contents'
      },
      children: [actionSecondary &&
      /*#__PURE__*/
      // @ts-expect-error onClick type mismatch
      jsx(Button, {
        v2: true,
        size: "md",
        priority: "secondary",
        href: actionSecondary.href,
        tabIndex: resolvedExpanded ? undefined : -1,
        onClick: actionSecondary?.onClick,
        children: actionSecondary.label
      }), action &&
      /*#__PURE__*/
      // @ts-expect-error onClick type mismatch
      jsx(Button, {
        v2: true,
        size: "md",
        priority: "primary",
        href: action.href,
        tabIndex: resolvedExpanded ? undefined : -1,
        onClick: action.onClick,
        children: action.label
      })]
    }) : undefined,
    role: "region",
    "aria-labelledby": ariaLabelledByIds || undefined,
    "aria-describedby": description ? descId : undefined,
    children: [/*#__PURE__*/jsxs("div", {
      className: "wds-critical-banner__text-wrapper",
      children: [title && /*#__PURE__*/jsx(Body, {
        id: titleId,
        type: Typography.BODY_LARGE_BOLD,
        className: "wds-critical-banner__content wds-critical-banner__title",
        children: title
      }), description && /*#__PURE__*/jsx(Body, {
        id: descId,
        className: clsx('wds-critical-banner__content', 'wds-critical-banner__description', {
          'wds-critical-banner__description--with-title': !!title
        }),
        children: description
      })]
    }), /*#__PURE__*/jsx(ExpanderToggle, {
      expanded: resolvedExpanded,
      size: 24,
      className: "wds-critical-banner__toggle",
      onToggle: handleToggle
    })]
  });
  return /*#__PURE__*/jsx(LiveRegion, {
    "aria-live": "assertive",
    announceOnChange: buildAnnouncementString({
      title,
      description,
      expanded: resolvedExpanded,
      actionLabel: action?.label,
      actionSecondaryLabel: actionSecondary?.label
    }),
    children: /*#__PURE__*/jsx("div", {
      className: "wds-critical-banner-overhang-query",
      children: /*#__PURE__*/jsx("div", {
        className: "wds-critical-banner__entry-mask",
        children: /*#__PURE__*/jsx("div", {
          className: "wds-critical-banner__entry-track",
          children: bannerSurface
        })
      })
    })
  });
};

export { CriticalBanner };
//# sourceMappingURL=CriticalBanner.mjs.map
