"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 _Comment = _interopRequireDefault(require("./Toast/Comment"));

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

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

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

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

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

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

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

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; }

/*
import CollapsedForNotificationToast from './Toast/CollapsedFor';
import DeletedForNotificationToast from './Toast/DeletedFor';
import KindlyNoticeFlagNotificationToast from './Toast/KindlyNoticeFlag';
import KindlyNoticeForNotificationToast from './Toast/KindlyNoticeFor';
import UndeletedForNotificationToast from './Toast/UndeletedFor';
import UserBlockedNotificationToast from './Toast/UserBlocked';
*/
const PREFIX = 'SCUserToastNotifications';
const classes = {
  toastMessage: `${PREFIX}-toast-message`,
  toastContent: `${PREFIX}-toast-content`,
  toastActions: `${PREFIX}-toast-actions`
};
const Root = (0, _styles.styled)(_material.Box, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  [`& .${classes.toastMessage}`]: {
    minWidth: 280
  },
  [`& .${classes.toastContent}`]: {
    marginBottom: 10
  }
}));

function UserToastNotifications(props) {
  // PROPS
  const {
    ToastMessageProps = {},
    handleNotification,
    disableToastNotification = false
  } = props; // REFS

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

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

  const getContent = n => {
    const type = _core.SCNotification.SCNotificationMapping[n.activity_type];
    let content;

    if (type === _core.SCNotificationTypologyType.COMMENT || type === _core.SCNotificationTypologyType.NESTED_COMMENT) {
      content = /*#__PURE__*/_react.default.createElement(_Comment.default, {
        notificationObject: n
      });
    } else if (type === _core.SCNotificationTypologyType.FOLLOW) {
      content = /*#__PURE__*/_react.default.createElement(_ContributionFollow.default, {
        notificationObject: n
      });
    } else if (type === _core.SCNotificationTypologyType.USER_FOLLOW) {
      content = /*#__PURE__*/_react.default.createElement(_UserFollow.default, {
        notificationObject: n
      });
    } else if (type === _core.SCNotificationTypologyType.CONNECTION_REQUEST || type === _core.SCNotificationTypologyType.CONNECTION_ACCEPT) {
      content = /*#__PURE__*/_react.default.createElement(_UserConnection.default, {
        notificationObject: n
      });
    } else if (type === _core.SCNotificationTypologyType.VOTE_UP) {
      content = /*#__PURE__*/_react.default.createElement(_VoteUp.default, {
        notificationObject: n
      });
    } else if (type === _core.SCNotificationTypologyType.PRIVATE_MESSAGE) {
      content = /*#__PURE__*/_react.default.createElement(_PrivateMessage.default, {
        notificationObject: n
      });
    } else if (type === _core.SCNotificationTypologyType.MENTION) {
      content = /*#__PURE__*/_react.default.createElement(_Mention.default, {
        notificationObject: n
      });
    } else if (type === _core.SCNotificationTypologyType.INCUBATOR_APPROVED) {
      content = /*#__PURE__*/_react.default.createElement(_IncubatorApproved.default, {
        notificationObject: n
      });
      /*
      } else if (
      type === SCNotificationTypologyType.KINDLY_NOTICE_ADVERTISING ||
      type === SCNotificationTypologyType.KINDLY_NOTICE_AGGRESSIVE ||
      type === SCNotificationTypologyType.KINDLY_NOTICE_POOR ||
      type === SCNotificationTypologyType.KINDLY_NOTICE_VULGAR ||
      type === SCNotificationTypologyType.KINDLY_NOTICE_OFFTOPIC
      ) {
      content = <KindlyNoticeForNotificationToast notificationObject={n} />;
      } else if (type === SCNotificationTypologyType.KINDLY_NOTICE_FLAG) {
      content = <KindlyNoticeFlagNotificationToast notificationObject={n} />;
      } else if (
      type === SCNotificationTypologyType.DELETED_FOR_ADVERTISING ||
      type === SCNotificationTypologyType.DELETED_FOR_AGGRESSIVE ||
      type === SCNotificationTypologyType.DELETED_FOR_POOR ||
      type === SCNotificationTypologyType.DELETED_FOR_VULGAR ||
      type === SCNotificationTypologyType.DELETED_FOR_OFFTOPIC
      ) {
      content = <DeletedForNotificationToast notificationObject={n} />;
      } else if (type === SCNotificationTypologyType.UNDELETED_FOR) {
      content = <UndeletedForNotificationToast notificationObject={n} />;
      } else if (
      type === SCNotificationTypologyType.COLLAPSED_FOR_ADVERTISING ||
      type === SCNotificationTypologyType.COLLAPSED_FOR_AGGRESSIVE ||
      type === SCNotificationTypologyType.COLLAPSED_FOR_POOR ||
      type === SCNotificationTypologyType.COLLAPSED_FOR_VULGAR ||
      type === SCNotificationTypologyType.COLLAPSED_FOR_OFFTOPIC
      ) {
      content = <CollapsedForNotificationToast notificationObject={n} />;
      } else if (type === SCNotificationTypologyType.BLOCKED_USER || type === SCNotificationTypologyType.UNBLOCKED_USER) {
      content = <UserBlockedNotificationToast notificationObject={n} />;
      */
    }

    if (handleNotification && type && !_core.SCNotification.SCSilentNotifications.includes(type)) {
      content = handleNotification(type, n, content);
    }

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


  const notificationSubscriber = (msg, data) => {
    if (data && data.type === _core.SCNotificationTopicType.INTERACTION && _core.SCNotification.SCNotificationMapping[data.data.activity_type] && !_core.SCNotification.SCSilentNotifications.includes(data.data.activity_type) && !disableToastNotification) {
      enqueueSnackbar(null, Object.assign({}, {
        content: /*#__PURE__*/_react.default.createElement(_CustomSnackMessage.default, {
          id: data.data.feed_serialization_id,
          message: /*#__PURE__*/_react.default.createElement("div", {
            className: classes.toastMessage
          }, /*#__PURE__*/_react.default.createElement("div", {
            className: classes.toastContent
          }, getContent(data.data)))
        }),
        preventDuplicate: true,
        key: data.data.feed_serialization_id,
        variant: 'default',
        persist: true,
        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);
}