"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.NotificationPopup = NotificationPopup;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _faCircleInfo = require("@fortawesome/free-solid-svg-icons/faCircleInfo");
var _faCircleCheck = require("@fortawesome/free-solid-svg-icons/faCircleCheck");
var _faCircleXmark = require("@fortawesome/free-solid-svg-icons/faCircleXmark");
var _faCircleExclamation = require("@fortawesome/free-solid-svg-icons/faCircleExclamation");
var _faXmark = require("@fortawesome/free-solid-svg-icons/faXmark");
var _button = require("../button");
var _fontAwesomeIcon = require("../font-awesome-icon");
var _types = require("./types");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const {
  INFO,
  SUCCESS,
  WARNING,
  FAILURE
} = _types.NOTIFICATION_LEVEL;
function isActionButtonVisible(notification) {
  return !!notification?.action;
}
function isActionLinkVisible(notification) {
  return !!notification?.href;
}
const getLinkComponent = ({
  LinkComponent,
  ...props
}) => LinkComponent ? /*#__PURE__*/(0, _jsxRuntime.jsx)(LinkComponent, {
  className: "anchor notification-action",
  testId: "notification-popup-action",
  ...props
}) : /*#__PURE__*/(0, _jsxRuntime.jsx)("a", {
  href: props.href,
  className: "anchor notification-action",
  "test-id": "notification-popup-action",
  target: props.target,
  children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
    children: [props.actionIcon && /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
      icon: props.actionIcon,
      "test-id": "notification-popup-action-icon",
      className: "notification-action-icon"
    }), props.title]
  })
});
function getIcon(notification) {
  switch (notification.level) {
    case INFO:
      return _faCircleInfo.faCircleInfo;
    case SUCCESS:
      return _faCircleCheck.faCircleCheck;
    case WARNING:
      return _faCircleExclamation.faCircleExclamation;
    case FAILURE:
      return _faCircleXmark.faCircleXmark;
    default:
      return _faCircleInfo.faCircleInfo;
  }
}
/**
 * Notification Popup.
 * Used to provide feedback about recent user actions.
 * Use NotificationPopupService to show a notification.
 * Use <NotificationPopupContainer /> where to display a notifications.
 * Different types of notifications are supported.
 * Notification can be actionable.
 * Notification disappears automatically after a specific amount of time
 * or can be dismissed immediately.
 * [CAPITALIZATION] Use sentence case
 * [CAPITALIZATION] Keep as brief as possible, ideally not a complete sentence/no punctuation at the end
 * [CAPITALIZATION] If you feel there are multiple thoughts (sentences), use punctuation. But consider whether a popup notification is appropriate.
 * @midnight-gray-supported
 * @alpine-light-supported
 * */
function NotificationPopup({
  notification,
  onHidden,
  closeButtonText
}) {
  const [isVisible, setVisibility] = (0, _react.useState)(true);
  const dismissTimer = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    if (notification?.level === _types.NOTIFICATION_LEVEL.FAILURE || notification?.disableAutoDismiss) {
      return;
    }
    clearDismissTimerTimeout();
    setVisibility(true);
    const dismissDelay = isActionButtonVisible(notification) ? _types.MAX_DISMISS_DELAY : _types.MIN_DISMISS_DELAY;

    // We can't fix this without making some breaking changes
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    dismissTimer.current = setTimeout(() => {
      setVisibility(false);
    }, dismissDelay);
    return () => {
      clearDismissTimerTimeout();
    };
  }, []);
  function handleActionApply() {
    notification.action?.();
    handleDismiss();
  }
  function clearDismissTimerTimeout() {
    if (dismissTimer.current) {
      clearTimeout(dismissTimer.current);
    }
  }
  function handleDismiss() {
    clearDismissTimerTimeout();
    setVisibility(false);
    onHidden(notification);
  }
  function handleNotificationCleanup() {
    if (!isVisible) {
      onHidden(notification);
    }
  }
  if (!notification) {
    return null;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: (0, _classnames.default)('notification-popup', notification.level.toLowerCase(), {
      dismissed: !isVisible
    }),
    onAnimationEnd: handleNotificationCleanup,
    "test-id": "notification-popup",
    role: "alert",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "notification-icon",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
        icon: getIcon(notification),
        "test-id": "notification-popup-glyph"
      })
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "notification-content",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
          className: "notification-text",
          "test-id": "notification-text",
          children: notification.message
        }), isActionButtonVisible(notification) ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.Button, {
          accentType: _button.ACCENT_TYPES.COMMAND,
          onClick: handleActionApply,
          leftIcon: notification.actionIcon,
          className: "notification-action",
          testId: "notification-popup-action",
          children: notification.title
        }) : null, isActionLinkVisible(notification) && getLinkComponent(notification)]
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.Button, {
        className: "notification-dismiss",
        accentType: _button.ACCENT_TYPES.COMMAND,
        "aria-label": closeButtonText,
        onClick: handleDismiss,
        testId: "notification-popup-dismiss",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
          icon: _faXmark.faXmark
        })
      })]
    })]
  });
}