var _excluded = ["children", "className", "autoFocus", "heading", "headingId", "headingLevel", "hideIcon", "alertRef", "role", "variation", "weight", "analytics", "analyticsLabelOverride", "analyticsEventTypeOverride", "onAnalyticsEvent"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React from "preact/compat";
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";
/**
 * For information about how and when to use this component,
 * [refer to its full documentation page](https://design.cms.gov/components/alert/).
 */
export var Alert = function Alert(props) {
  var _props$headingId;
  var _useAlertAnalytics = useAlertAnalytics(props),
    headingRef = _useAlertAnalytics.headingRef,
    bodyRef = _useAlertAnalytics.bodyRef;
  var focusRef = useAutofocus(props.autoFocus);
  var rootId = useId('alert--', props.id);
  var headingId = (_props$headingId = props.headingId) !== null && _props$headingId !== void 0 ? _props$headingId : "".concat(rootId, "__heading");
  var a11yLabelId = "".concat(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.");
    }
  }
  var children = props.children,
    className = props.className,
    autoFocus = props.autoFocus,
    heading = props.heading,
    _headingId = props.headingId,
    headingLevel = props.headingLevel,
    hideIcon = props.hideIcon,
    alertRef = props.alertRef,
    role = props.role,
    variation = props.variation,
    weight = props.weight,
    analytics = props.analytics,
    analyticsLabelOverride = props.analyticsLabelOverride,
    analyticsEventTypeOverride = props.analyticsEventTypeOverride,
    onAnalyticsEvent = props.onAnalyticsEvent,
    alertProps = _objectWithoutProperties(props, _excluded);
  var headingElement;
  if (heading) {
    var Heading = "h".concat(headingLevel);
    headingElement = /*#__PURE__*/React.createElement(Heading, {
      className: "ds-c-alert__heading"
    }, heading);
  }
  var classes = classNames('ds-c-alert', hideIcon && 'ds-c-alert--hide-icon', variation && "ds-c-alert--".concat(variation), weight && "ds-c-alert--".concat(weight), className);
  var a11yLabelText = t("alert.".concat(variation !== null && variation !== void 0 ? variation : 'defaultLabel'));
  var a11yLabel = /*#__PURE__*/React.createElement("span", {
    className: "ds-c-alert__a11y-label ds-u-visibility--screen-reader",
    id: a11yLabelId
  }, "".concat(a11yLabelText, ": "));

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