"use strict";

exports.__esModule = true;
exports.default = UserToastNotifications;

var _react = _interopRequireWildcard(require("react"));

var _styles = require("@mui/material/styles");

var _material = require("@mui/material");

var _core = require("@selfcommunity/core");

var _pubsubJs = _interopRequireDefault(require("pubsub-js"));

var _notistack = require("notistack");

var _CustomSnackMessage = _interopRequireDefault(require("../../shared/CustomSnackMessage"));

var _types = require("../../types");

var _Comment = _interopRequireDefault(require("../Notification/Comment"));

var _ContributionFollow = _interopRequireDefault(require("../Notification/ContributionFollow"));

var _UserFollow = _interopRequireDefault(require("../Notification/UserFollow"));

var _UserConnection = _interopRequireDefault(require("../Notification/UserConnection"));

var _VoteUp = _interopRequireDefault(require("../Notification/VoteUp"));

var _PrivateMessage = _interopRequireDefault(require("../Notification/PrivateMessage"));

var _Mention = _interopRequireDefault(require("../Notification/Mention"));

var _KindlyNoticeFlag = _interopRequireDefault(require("../Notification/KindlyNoticeFlag"));

var _IncubatorApproved = _interopRequireDefault(require("../Notification/IncubatorApproved"));

var _UserBlocked = _interopRequireDefault(require("../Notification/UserBlocked"));

var _Message = _interopRequireDefault(require("../BroadcastMessages/Message"));

var _useThemeProps = _interopRequireDefault(require("@mui/material/styles/useThemeProps"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const PREFIX = 'SCToastNotifications';
const Root = (0, _styles.styled)(_material.Box, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({}));

/**
 *
 > API documentation for the Community-UI Toast Notifications component. Learn about the available props and the CSS API.

 #### Import

 ```jsx
 import {ToastNotifications} from '@selfcommunity/ui';
 ```

 #### Component Name

 The name `SCToastNotifications` can be used when providing style overrides in the theme.


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCUserNotifications-root|Styles applied to the root element.|
 |toastMessage|.SCUserToastNotifications-toast-message|Styles applied to the toast message element.|
 |toastContent|.SCUserToastNotifications-toast-content|Styles applied to the toast content element.|
 |toastActions|.SCUserToastNotifications-toast-actions|Styles applied to the toast actions section.|

 * @param inProps
 */
function UserToastNotifications(inProps) {
  // PROPS
  const props = (0, _useThemeProps.default)({
    props: inProps,
    name: PREFIX
  });
  const {
    ToastMessageProps = {},
    handleNotification,
    disableToastNotification = false
  } = props; // CONTEXT

  const scContext = (0, _core.useSCContext)(); // REFS

  const notificationSubscription = (0, _react.useRef)(null); // CONTEXT

  const {
    enqueueSnackbar
  } = (0, _notistack.useSnackbar)();
  /**
   * Render every single notification content
   * @param n
   */

  const getContent = n => {
    let content;
    let type;

    if (n.notification_obj) {
      /** Notification of types: comment, nested_comment, etc... */
      type = _core.SCNotification.SCNotificationMapping[n.activity_type];

      if (type === _core.SCNotificationTypologyType.COMMENT || type === _core.SCNotificationTypologyType.NESTED_COMMENT) {
        content = /*#__PURE__*/_react.default.createElement(_Comment.default, {
          notificationObject: n.notification_obj,
          template: _types.SCNotificationObjectTemplateType.TOAST
        });
      } else if (type === _core.SCNotificationTypologyType.FOLLOW) {
        content = /*#__PURE__*/_react.default.createElement(_ContributionFollow.default, {
          notificationObject: n.notification_obj,
          template: _types.SCNotificationObjectTemplateType.TOAST
        });
      } else if (type === _core.SCNotificationTypologyType.USER_FOLLOW) {
        content = /*#__PURE__*/_react.default.createElement(_UserFollow.default, {
          notificationObject: n.notification_obj,
          template: _types.SCNotificationObjectTemplateType.TOAST
        });
      } else if (type === _core.SCNotificationTypologyType.CONNECTION_REQUEST || type === _core.SCNotificationTypologyType.CONNECTION_ACCEPT) {
        content = /*#__PURE__*/_react.default.createElement(_UserConnection.default, {
          notificationObject: n.notification_obj,
          template: _types.SCNotificationObjectTemplateType.TOAST
        });
      } else if (type === _core.SCNotificationTypologyType.VOTE_UP) {
        content = /*#__PURE__*/_react.default.createElement(_VoteUp.default, {
          notificationObject: n.notification_obj,
          template: _types.SCNotificationObjectTemplateType.TOAST
        });
      } else if (type === _core.SCNotificationTypologyType.PRIVATE_MESSAGE) {
        content = /*#__PURE__*/_react.default.createElement(_PrivateMessage.default, {
          notificationObject: n.notification_obj,
          template: _types.SCNotificationObjectTemplateType.TOAST
        });
      } else if (type === _core.SCNotificationTypologyType.BLOCKED_USER || type === _core.SCNotificationTypologyType.UNBLOCKED_USER) {
        return /*#__PURE__*/_react.default.createElement(_UserBlocked.default, {
          notificationObject: n.notification_obj,
          template: _types.SCNotificationObjectTemplateType.TOAST
        });
      } else if (type === _core.SCNotificationTypologyType.MENTION) {
        content = /*#__PURE__*/_react.default.createElement(_Mention.default, {
          notificationObject: n.notification_obj,
          template: _types.SCNotificationObjectTemplateType.TOAST
        });
      } else if (type === _core.SCNotificationTypologyType.KINDLY_NOTICE_FLAG) {
        content = /*#__PURE__*/_react.default.createElement(_KindlyNoticeFlag.default, {
          notificationObject: n.notification_obj,
          template: _types.SCNotificationObjectTemplateType.TOAST
        });
      } else if (type === _core.SCNotificationTypologyType.INCUBATOR_APPROVED) {
        content = /*#__PURE__*/_react.default.createElement(_IncubatorApproved.default, {
          notificationObject: n.notification_obj,
          template: _types.SCNotificationObjectTemplateType.TOAST
        });
      }
    }

    if (n.activity_type && n.activity_type === _core.SCNotificationTypologyType.NOTIFICATION_BANNER) {
      /** Notification of type: 'notification_banner' */
      // TODO: When api is fixed, use BroadcastMessage -> Message as the component to render this content
      content = /*#__PURE__*/_react.default.createElement(_Message.default, {
        key: n.notification_obj.id,
        message: n.notification_obj,
        elevation: 0,
        template: _types.SCBroadcastMessageTemplateType.TOAST
      });
    }

    if (handleNotification && type) {
      /** Override content */
      content = handleNotification(type, n, content);
    }

    return content;
  };
  /**
   * Notification subscriber
   * @param msg
   * @param data
   */


  const notificationSubscriber = (msg, data) => {
    if (data && data.type === _core.SCNotificationTopicType.INTERACTION && (data.data.activity_type === _core.SCNotificationTypologyType.NOTIFICATION_BANNER || _core.SCNotification.SCNotificationMapping[data.data.activity_type]) && !_core.SCNotification.SCSilentToastNotifications.includes(data.data.activity_type) && !disableToastNotification && !scContext.settings.notifications.webSocket.disableToastMessage) {
      /**
       * !IMPORTANT
       * - messageKey for the notification_banner is 'id', for others type 'feed_serialization_id'
       * - the enqueue message is persistent (it remains on the screen while the others replace each other) if type notification_banner
       */
      const messageKey = data.data.feed_serialization_id ? data.data.feed_serialization_id : data.data.id;
      enqueueSnackbar(null, Object.assign({}, {
        content: /*#__PURE__*/_react.default.createElement(_CustomSnackMessage.default, {
          id: messageKey,
          message: getContent(data.data)
        }),
        preventDuplicate: true,
        key: messageKey,
        variant: 'default',
        persist: data.data.activity_type === _core.SCNotificationTypologyType.NOTIFICATION_BANNER ? true : false,
        anchorOrigin: {
          horizontal: 'left',
          vertical: 'bottom'
        },
        action: null
      }, ToastMessageProps));
    }
  };
  /**
   * On mount, fetches first page of notifications
   * On mount, subscribe to receive notification updates
   */


  (0, _react.useEffect)(() => {
    notificationSubscription.current = _pubsubJs.default.subscribe(_core.SCNotificationTopicType.INTERACTION, notificationSubscriber);
    return () => {
      _pubsubJs.default.unsubscribe(notificationSubscription.current);
    };
  }, []);
  return /*#__PURE__*/_react.default.createElement(Root, null);
}