"use strict";

exports.__esModule = true;
exports.default = PrivateMessageNotification;

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

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

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

var _Send = _interopRequireDefault(require("@mui/icons-material/Send"));

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

var _colors = require("@mui/material/colors");

var _reactIntl = require("react-intl");

var _DateTimeAgo = _interopRequireDefault(require("../../../shared/DateTimeAgo"));

var _NewChip = _interopRequireDefault(require("../../../shared/NewChip/NewChip"));

var _classnames = _interopRequireDefault(require("classnames"));

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

const _excluded = ["notificationObject", "id", "className", "template"];

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

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

const messages = (0, _reactIntl.defineMessages)({
  receivePrivateMessage: {
    id: 'ui.notification.receivePrivateMessage',
    defaultMessage: 'ui.notification.receivePrivateMessage'
  }
});
const PREFIX = 'SCUserNotificationPrivateMessage';
const classes = {
  root: `${PREFIX}-root`,
  listItemSnippet: `${PREFIX}-list-item-snippet`,
  listItemSnippetNew: `${PREFIX}-list-item-snippet-new`,
  avatarWrap: `${PREFIX}-avatar-wrap`,
  avatar: `${PREFIX}-avatar`,
  avatarSnippet: `${PREFIX}-avatar-snippet`,
  actions: `${PREFIX}-actions`,
  replyButton: `${PREFIX}-reply-button`,
  replyButtonIcon: `${PREFIX}-reply-button-icon`,
  activeAt: `${PREFIX}-active-at`,
  messageLabel: `${PREFIX}-message-label`,
  messageSender: `${PREFIX}-message-sender`,
  messageWrap: `${PREFIX}-message-wrap`,
  message: `${PREFIX}-message`
};
const Root = (0, _styles.styled)(_material.Box, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  [`& .${classes.listItemSnippet}`]: {
    padding: '0px 5px',
    alignItems: 'center'
  },
  [`& .${classes.listItemSnippetNew}`]: {
    borderLeft: '2px solid red'
  },
  [`& .${classes.avatarWrap}`]: {
    minWidth: 'auto',
    paddingRight: 10
  },
  [`& .${classes.avatar}`]: {
    backgroundColor: _colors.red[500],
    color: '#FFF'
  },
  [`& .${classes.avatarSnippet}`]: {
    width: 30,
    height: 30
  },
  [`& .${classes.replyButton}`]: {
    minWidth: 30
  },
  [`& .${classes.activeAt}`]: {
    minWidth: 30,
    [theme.breakpoints.down('md')]: {
      display: 'none'
    }
  },
  [`& .${classes.messageSender}`]: {
    display: 'inline',
    fontWeight: '600'
  },
  [`& .${classes.messageLabel}`]: {
    display: 'inline',
    color: theme.palette.text.primary
  },
  [`& .${classes.messageWrap}`]: {
    display: 'inline-block',
    height: 20
  },
  [`& .${classes.actions}`]: {
    color: _colors.grey[600],
    fontSize: '13px',
    maxWidth: '40%'
  }
}));

/**
 * This component render the content of the notification of type private message
 * @param props
 * @constructor
 */
function PrivateMessageNotification(props) {
  // PROPS
  const {
    notificationObject,
    id = `n_${props.notificationObject['sid']}`,
    className,
    template = _types.NotificationObjectTemplateType.DETAIL
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // CONTEXT


  const scRoutingContext = (0, _core.useSCRouting)(); // CONST

  const isSnippetTemplate = template === _types.NotificationObjectTemplateType.SNIPPET;
  const isToastTemplate = template === _types.NotificationObjectTemplateType.TOAST; //INTL

  const intl = (0, _reactIntl.useIntl)();
  /**
   * Renders content
   */

  let content;

  if (isSnippetTemplate) {
    content = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_material.ListItem, {
      component: 'div',
      classes: {
        root: (0, _classnames.default)({
          [classes.listItemSnippet]: isSnippetTemplate,
          [classes.listItemSnippetNew]: notificationObject.is_new
        })
      }
    }, /*#__PURE__*/_react.default.createElement(_material.ListItemAvatar, {
      classes: {
        root: classes.avatarWrap
      }
    }, /*#__PURE__*/_react.default.createElement(_core.Link, {
      to: scRoutingContext.url(_core.SCRoutes.USER_PROFILE_ROUTE_NAME, {
        id: notificationObject.message.sender.id
      })
    }, /*#__PURE__*/_react.default.createElement(_material.Avatar, {
      alt: notificationObject.message.sender.username,
      variant: "circular",
      src: notificationObject.message.sender.avatar,
      classes: {
        root: (0, _classnames.default)(classes.avatar, {
          [classes.avatarSnippet]: isSnippetTemplate
        })
      }
    }))), /*#__PURE__*/_react.default.createElement(_material.ListItemText, {
      disableTypography: true,
      primary: /*#__PURE__*/_react.default.createElement(_material.Typography, {
        component: "div",
        color: "inherit"
      }, /*#__PURE__*/_react.default.createElement(_core.Link, {
        to: scRoutingContext.url(_core.SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.message.sender),
        className: classes.messageSender
      }, notificationObject.message.sender.username), ' ', /*#__PURE__*/_react.default.createElement(_core.Link, {
        to: scRoutingContext.url(_core.SCRoutes.USER_PRIVATE_MESSAGES_ROUTE_NAME, notificationObject.message),
        className: classes.messageLabel
      }, intl.formatMessage(messages.receivePrivateMessage, {
        total: 1,
        b: (...chunks) => /*#__PURE__*/_react.default.createElement("strong", null, chunks)
      })))
    })));
  } else if (isToastTemplate) {
    content = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_material.ListItem, {
      alignItems: 'flex-start',
      component: 'div'
    }, /*#__PURE__*/_react.default.createElement(_material.ListItemAvatar, null, /*#__PURE__*/_react.default.createElement(_core.Link, {
      to: scRoutingContext.url(_core.SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.message.sender)
    }, /*#__PURE__*/_react.default.createElement(_material.Avatar, {
      alt: notificationObject.message.sender.username,
      variant: "circular",
      src: notificationObject.message.sender.avatar
    }))), /*#__PURE__*/_react.default.createElement(_material.ListItemText, {
      disableTypography: true,
      primary: /*#__PURE__*/_react.default.createElement(_material.Typography, null, /*#__PURE__*/_react.default.createElement(_core.Link, {
        to: scRoutingContext.url(_core.SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.message.sender)
      }, notificationObject.message.sender.username), ' ', /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
        id: 'ui.userToastNotifications.privateMessage.sentMessage',
        defaultMessage: 'ui.userToastNotifications.privateMessage.sentMessage'
      }), ":"),
      secondary: /*#__PURE__*/_react.default.createElement(_material.Typography, {
        color: "primary",
        gutterBottom: true,
        dangerouslySetInnerHTML: {
          __html: notificationObject.message.html
        }
      })
    })), /*#__PURE__*/_react.default.createElement(_material.Stack, {
      direction: "row",
      justifyContent: "space-between",
      alignItems: "center",
      spacing: 2
    }, /*#__PURE__*/_react.default.createElement(_DateTimeAgo.default, {
      date: notificationObject.active_at
    }), /*#__PURE__*/_react.default.createElement(_material.Typography, {
      color: "primary"
    }, /*#__PURE__*/_react.default.createElement(_core.Link, {
      to: scRoutingContext.url('messages', {
        id: notificationObject.message.id
      })
    }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.userToastNotifications.replyMessage",
      defaultMessage: 'ui.userToastNotifications.replyMessage'
    })))));
  } else {
    content = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_material.ListItem, {
      component: 'div',
      classes: {
        root: (0, _classnames.default)({
          [classes.listItemSnippet]: isSnippetTemplate
        }),
        secondaryAction: classes.actions
      },
      secondaryAction: /*#__PURE__*/_react.default.createElement(_material.Stack, {
        direction: "row",
        justifyContent: "space-between",
        alignItems: "center",
        spacing: 2
      }, /*#__PURE__*/_react.default.createElement(_DateTimeAgo.default, {
        date: notificationObject.active_at,
        className: classes.activeAt
      }), /*#__PURE__*/_react.default.createElement(_material.Button, {
        color: 'primary',
        variant: "outlined",
        size: "small",
        classes: {
          root: classes.replyButton
        },
        component: _core.Link,
        to: scRoutingContext.url(_core.SCRoutes.USER_PRIVATE_MESSAGES_ROUTE_NAME, notificationObject.message),
        endIcon: /*#__PURE__*/_react.default.createElement(_Send.default, {
          className: classes.replyButtonIcon
        })
      }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
        id: "ui.notification.privateMessage.btnReplyLabel",
        defaultMessage: "ui.notification.privateMessage.btnReplyLabel"
      })))
    }, /*#__PURE__*/_react.default.createElement(_material.ListItemText, {
      disableTypography: true,
      primary: /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, notificationObject.is_new && /*#__PURE__*/_react.default.createElement(_NewChip.default, null), /*#__PURE__*/_react.default.createElement(_material.Box, {
        className: classes.messageWrap
      }, /*#__PURE__*/_react.default.createElement(_core.Link, {
        to: scRoutingContext.url(_core.SCRoutes.USER_PRIVATE_MESSAGES_ROUTE_NAME, notificationObject.message),
        className: classes.message
      }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
        variant: "body2",
        gutterBottom: true,
        dangerouslySetInnerHTML: {
          __html: notificationObject.message.html
        }
      }))))
    })));
  }
  /**
   * Renders root object
   */


  return /*#__PURE__*/_react.default.createElement(Root, _extends({
    id: id,
    className: (0, _classnames.default)(classes.root, className, `${PREFIX}-${template}`)
  }, rest), content);
}