'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var clsx = require('clsx');
var React = require('react');
var Body = require('../body/Body.js');
require('../common/theme.js');
require('../common/direction.js');
require('../common/propsValues/control.js');
require('../common/propsValues/breakpoint.js');
var size = require('../common/propsValues/size.js');
var typography = require('../common/propsValues/typography.js');
require('../common/propsValues/width.js');
require('../common/propsValues/type.js');
require('../common/propsValues/dateMode.js');
require('../common/propsValues/monthFormat.js');
require('../common/propsValues/position.js');
require('../common/propsValues/layouts.js');
require('../common/propsValues/status.js');
var sentiment = require('../common/propsValues/sentiment.js');
require('../common/propsValues/profileType.js');
require('../common/propsValues/variant.js');
require('../common/propsValues/scroll.js');
require('../common/propsValues/markdownNodeType.js');
require('../common/fileType.js');
var constants = require('../common/constants.js');
var CloseButton = require('../common/closeButton/CloseButton.js');
var StatusIcon = require('../statusIcon/StatusIcon.js');
var Title = require('../title/Title.js');
var logActionRequired = require('../utilities/logActionRequired.js');
var InlineMarkdown = require('./inlineMarkdown/InlineMarkdown.js');
var Action = require('../common/action/Action.js');
var jsxRuntime = require('react/jsx-runtime');

exports.AlertArrowPosition = void 0;
(function (AlertArrowPosition) {
  AlertArrowPosition["TOP_LEFT"] = "up-left";
  AlertArrowPosition["TOP"] = "up-center";
  AlertArrowPosition["TOP_RIGHT"] = "up-right";
  AlertArrowPosition["BOTTOM_LEFT"] = "down-left";
  AlertArrowPosition["BOTTOM"] = "down-center";
  AlertArrowPosition["BOTTOM_RIGHT"] = "down-right";
})(exports.AlertArrowPosition || (exports.AlertArrowPosition = {}));
function resolveType(type) {
  switch (type) {
    case 'success':
      return 'positive';
    case 'info':
      return 'neutral';
    case 'error':
      return 'negative';
    default:
      return type;
  }
}
function Alert({
  action,
  className,
  icon,
  statusIconLabel,
  onDismiss,
  message,
  title,
  type = 'neutral',
  variant = 'desktop',
  arrow,
  children,
  size: size$1,
  dismissible,
  active = true,
  dynamicRender
}) {
  React.useEffect(() => {
    if (active !== undefined || dynamicRender !== undefined) {
      logActionRequired.logActionRequired("Alert component doesn't support `active` or `dynamicRender` anymore, please remove that prop.");
    }
  }, [active, dynamicRender]);
  React.useEffect(() => {
    if (arrow !== undefined) {
      logActionRequired.logActionRequired("Alert component doesn't support 'arrow' anymore, use 'InlineAlert' instead.");
    }
  }, [arrow]);
  React.useEffect(() => {
    if (children !== undefined) {
      logActionRequired.logActionRequired("Alert component doesn't support 'children' anymore, use 'message' instead.");
    }
  }, [children]);
  React.useEffect(() => {
    if (dismissible !== undefined) {
      logActionRequired.logActionRequired("Alert component doesn't support 'dismissible' anymore, use 'onDismiss' instead.");
    }
  }, [dismissible]);
  React.useEffect(() => {
    if (size$1 !== undefined) {
      logActionRequired.logActionRequired("Alert component doesn't support 'size' anymore, please remove that prop.");
    }
  }, [size$1]);
  const resolvedType = resolveType(type);
  React.useEffect(() => {
    if (resolvedType !== type) {
      logActionRequired.logActionRequired(`Alert component has deprecated '${type}' value for the 'type' prop. Please use '${resolvedType}' instead.`);
    }
  }, [resolvedType, type]);
  const [shouldFire, setShouldFire] = React.useState();
  const [shouldAnnounce, setShouldAnnounce] = React.useState(false);
  React.useEffect(() => {
    setTimeout(() => {
      setShouldAnnounce(true);
    }, constants.WDS_LIVE_REGION_DELAY_MS);
  }, []);
  const closeButtonReference = React.useRef(null);
  return /*#__PURE__*/jsxRuntime.jsx("div", {
    role: resolvedType === sentiment.Sentiment.NEGATIVE ? 'alert' : 'status',
    className: "wds-alert__liveRegion",
    children: active && /*#__PURE__*/jsxRuntime.jsxs("div", {
      "aria-hidden": shouldAnnounce ? undefined : 'true',
      className: clsx.clsx('alert d-flex', `alert-${resolvedType}`, arrow != null && alertArrowClassNames(arrow), className),
      "data-testid": "alert",
      onTouchStart: () => setShouldFire(true),
      onTouchEnd: event => {
        if (shouldFire && action?.href &&
        // Check if current event is triggered from closeButton
        event.target instanceof Node && closeButtonReference.current && !closeButtonReference.current.contains(event.target)) {
          if (action.target === '_blank') {
            window.top?.open(action.href);
          } else {
            window.top?.location.assign(action.href);
          }
        }
        setShouldFire(false);
      },
      onTouchMove: () => setShouldFire(false),
      children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
        className: clsx.clsx('alert__content', 'd-flex', 'flex-grow-1', variant),
        "data-testid": variant,
        children: [icon ? /*#__PURE__*/jsxRuntime.jsx("div", {
          className: "alert__icon",
          children: icon
        }) : /*#__PURE__*/jsxRuntime.jsx(StatusIcon, {
          size: size.Size.LARGE,
          sentiment: resolvedType,
          iconLabel: statusIconLabel
        }), /*#__PURE__*/jsxRuntime.jsxs("div", {
          className: "alert__message",
          children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
            children: [title && /*#__PURE__*/jsxRuntime.jsx(Title, {
              className: "m-b-1",
              type: typography.Typography.TITLE_BODY,
              children: title
            }), /*#__PURE__*/jsxRuntime.jsx(Body, {
              as: "span",
              className: "d-block",
              type: typography.Typography.BODY_LARGE,
              children: children || /*#__PURE__*/jsxRuntime.jsx(InlineMarkdown, {
                children: message
              })
            })]
          }), action && /*#__PURE__*/jsxRuntime.jsx(Action.Action, {
            action: action,
            variant: "action-button",
            className: "m-t-1"
          })]
        })]
      }), onDismiss && /*#__PURE__*/jsxRuntime.jsx(CloseButton.CloseButton, {
        ref: closeButtonReference,
        className: "m-l-2",
        onClick: onDismiss
      })]
    })
  });
}
function alertArrowClassNames(arrow) {
  switch (arrow) {
    case 'down-center':
      return 'arrow arrow-bottom arrow-center';
    case 'down-left':
      return 'arrow arrow-bottom arrow-left';
    case 'down-right':
      return 'arrow arrow-bottom arrow-right';
    case 'up-center':
      return 'arrow arrow-center';
    case 'up-right':
      return 'arrow arrow-right';
    case 'up-left':
    default:
      return 'arrow';
  }
}

exports.default = Alert;
//# sourceMappingURL=Alert.js.map
