"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.NotificationPopupContainer = NotificationPopupContainer;
var _react = _interopRequireWildcard(require("react"));
var _notificationService = require("./notification-service");
var _notificationPopup = require("./notification-popup");
var _useTranslation = require("../hooks/use-translation");
var _jsxRuntime = require("react/jsx-runtime");
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); }
function NotificationPopupContainer({
  closeButtonText,
  retryText: retryTextParams
}) {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const retryText = retryTextParams ?? t('Retry');
  const [shownNotifications, setShownNotifications] = (0, _react.useState)([]);
  function handleShowNotification(customNotification) {
    const newNotification = {
      ...customNotification,
      title: customNotification.retry ? customNotification.title ?? retryText : customNotification.title
    };
    if (!newNotification) {
      return;
    }
    setShownNotifications(existingNotifications => {
      const messageAlreadyShown = existingNotifications.some(notification => notification.message === newNotification.message);
      return messageAlreadyShown ? existingNotifications : [newNotification, ...existingNotifications];
    });
  }
  (0, _react.useEffect)(() => {
    const subscriptions = [_notificationService.NotificationService.subscribe(_notificationService.EVENTS.NOTIFICATION_SHOW, handleShowNotification), _notificationService.NotificationService.subscribe(_notificationService.EVENTS.NOTIFICATIONS_DISMISS_ALL, () => setShownNotifications([]))];
    return () => subscriptions.forEach(subscription => subscription.cancel());
  }, []);
  function onNotificationHidden(hiddenNotification) {
    setShownNotifications(existingNotifications => {
      return existingNotifications.filter(notification => {
        return notification.message !== hiddenNotification.message;
      });
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: "notification-popup-container",
    "test-id": "notification-popup-container",
    children: shownNotifications.map(notification => {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_notificationPopup.NotificationPopup, {
        notification: notification,
        onHidden: onNotificationHidden,
        closeButtonText: closeButtonText ?? t('Close')
      }, notification.message);
    })
  });
}