import classNames from 'classnames';
import mergeRefs from "../utilities/mergeRefs";
import useAutofocus from "../utilities/useAutoFocus";
import useAlertAnalytics from "./useAlertAnalytics";
import { InfoCircleIcon, AlertCircleIcon, WarningIcon, CheckCircleIcon } from "../Icons";
import { t } from "../i18n";
import useId from "../utilities/useId";
import { jsx as _jsx } from "preact/compat/jsx-runtime";
import { jsxs as _jsxs } from "preact/compat/jsx-runtime";
/**
 * For information about how and when to use this component,
 * [refer to its full documentation page](https://design.cms.gov/components/alert/).
 */
export const Alert = props => {
  const {
    headingRef,
    bodyRef
  } = useAlertAnalytics(props);
  const focusRef = useAutofocus(props.autoFocus);
  const rootId = useId('alert--', props.id);
  const headingId = props.headingId ?? `${rootId}__heading`;
  const a11yLabelId = `${rootId}__a11y-label`;
  if (process.env.NODE_ENV !== 'production') {
    if (!props.heading && !props.children) {
      console.warn(`Empty <Alert> components are not allowed, please use the 'heading' prop or include children.`);
    }
  }
  const {
    children,
    className,
    autoFocus,
    heading,
    headingId: _headingId,
    headingLevel,
    hideIcon,
    alertRef,
    role,
    variation,
    weight,
    analytics,
    analyticsLabelOverride,
    analyticsEventTypeOverride,
    onAnalyticsEvent,
    ...alertProps
  } = props;
  let headingElement;
  if (heading) {
    const Heading = `h${headingLevel}`;
    headingElement = /*#__PURE__*/_jsx(Heading, {
      className: "ds-c-alert__heading",
      children: heading
    });
  }
  const classes = classNames('ds-c-alert', hideIcon && 'ds-c-alert--hide-icon', variation && `ds-c-alert--${variation}`, weight && `ds-c-alert--${weight}`, className);
  const a11yLabelText = t(`alert.${variation ?? 'defaultLabel'}`);
  const a11yLabel = /*#__PURE__*/_jsx("span", {
    className: "ds-c-alert__a11y-label ds-u-visibility--screen-reader",
    id: a11yLabelId,
    children: `${a11yLabelText}: `
  });

  // getting proper icon for alert variation
  function getIcon() {
    const iconClass = 'ds-c-alert__icon';
    if (hideIcon) {
      return null;
    }
    switch (variation) {
      case 'error':
        return /*#__PURE__*/_jsx(AlertCircleIcon, {
          className: iconClass
        });
      case 'success':
        return /*#__PURE__*/_jsx(CheckCircleIcon, {
          className: iconClass
        });
      case 'warn':
        return /*#__PURE__*/_jsx(WarningIcon, {
          className: iconClass
        });
      default:
        return /*#__PURE__*/_jsx(InfoCircleIcon, {
          className: iconClass
        });
    }
  }
  return /*#__PURE__*/_jsxs("div", {
    className: classes,
    ref: mergeRefs([alertRef, focusRef]),
    tabIndex: alertRef || autoFocus ? -1 : null,
    role: role,
    "aria-labelledby": heading ? headingId : a11yLabelId,
    ...alertProps,
    children: [getIcon(), /*#__PURE__*/_jsxs("div", {
      className: "ds-c-alert__body",
      id: headingId,
      ref: bodyRef,
      children: [heading ? /*#__PURE__*/_jsxs("div", {
        className: "ds-c-alert__header ds-c-alert__heading",
        ref: headingRef,
        children: [a11yLabel, headingElement]
      }) : a11yLabel, children]
    })]
  });
};
Alert.defaultProps = {
  role: 'region',
  headingLevel: '2'
};
export default Alert;