"use strict";

exports.__esModule = true;
exports.default = CommentNotification;

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

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

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

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

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

var _Bullet = _interopRequireDefault(require("../../../shared/Bullet"));

var _lab = require("@mui/lab");

var _Icon = _interopRequireDefault(require("@mui/material/Icon"));

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

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

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

var _contribute = require("../../../utils/contribute");

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

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

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

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)({
  comment: {
    id: 'ui.notification.comment.comment',
    defaultMessage: 'ui.notification.comment.comment'
  },
  nestedComment: {
    id: 'ui.notification.comment.nestedComment',
    defaultMessage: 'ui.notification.comment.nestedComment'
  }
});
const PREFIX = 'SCCommentNotification';
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`,
  username: `${PREFIX}-username`,
  voteButton: `${PREFIX}-vote-button`,
  commentText: `${PREFIX}-comment-text`,
  contributionText: `${PREFIX}-contribution-text`,
  activeAt: `${PREFIX}-active-at`,
  bullet: `${PREFIX}-bullet`,
  toastInfo: `${PREFIX}-toast-info`
};
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.voteButton}`]: {
    marginLeft: '-1px',
    minWidth: '30px',
    paddingTop: 3
  },
  [`& .${classes.commentText}`]: {
    display: 'inline',
    color: theme.palette.text.primary
  },
  [`& .${classes.contributionText}`]: {
    textDecoration: 'underline'
  },
  [`& .${classes.toastInfo}`]: {
    marginTop: 10
  }
}));

/**
 * This component render the content of the notification of type comment/nested comment
 * @param props
 * @constructor
 */
function CommentNotification(props) {
  // PROPS
  const {
    notificationObject,
    index,
    onVote,
    loadingVote,
    id = `n_${props.notificationObject['sid']}`,
    template = _types.NotificationObjectTemplateType.DETAIL,
    className
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // ROUTING


  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 root object
   */

  return /*#__PURE__*/_react.default.createElement(Root, _extends({
    id: id,
    className: (0, _classnames.default)(classes.root, className, `${PREFIX}-${template}`)
  }, rest), /*#__PURE__*/_react.default.createElement(_material.ListItem, {
    alignItems: isSnippetTemplate ? 'center' : 'flex-start',
    component: 'div',
    classes: {
      root: (0, _classnames.default)({
        [classes.listItemSnippet]: isToastTemplate || isSnippetTemplate,
        [classes.listItemSnippetNew]: isSnippetTemplate && 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, notificationObject.comment.author)
  }, /*#__PURE__*/_react.default.createElement(_material.Avatar, {
    alt: notificationObject.comment.author.username,
    variant: "circular",
    src: notificationObject.comment.author.avatar,
    classes: {
      root: (0, _classnames.default)(classes.avatar, {
        [classes.avatarSnippet]: isSnippetTemplate
      })
    }
  }))), /*#__PURE__*/_react.default.createElement(_material.ListItemText, {
    disableTypography: true,
    primary: /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, template === _types.NotificationObjectTemplateType.DETAIL && notificationObject.is_new && /*#__PURE__*/_react.default.createElement(_NewChip.default, null), /*#__PURE__*/_react.default.createElement(_material.Typography, {
      component: "span",
      className: classes.commentText,
      color: "inherit"
    }, /*#__PURE__*/_react.default.createElement(_core.Link, {
      to: scRoutingContext.url(_core.SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.comment.author),
      className: classes.username
    }, notificationObject.comment.author.username), ' ', notificationObject.type === _core.SCNotificationTypologyType.NESTED_COMMENT ? intl.formatMessage(messages.comment, {
      b: (...chunks) => /*#__PURE__*/_react.default.createElement("strong", null, chunks)
    }) : intl.formatMessage(messages.nestedComment, {
      b: (...chunks) => /*#__PURE__*/_react.default.createElement("strong", null, chunks)
    }))),
    secondary: /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_core.Link, {
      to: scRoutingContext.url(_core.SCRoutes.COMMENT_ROUTE_NAME, (0, _contribute.getRouteData)(notificationObject.comment))
    }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
      variant: "body2",
      gutterBottom: true,
      className: classes.contributionText
    }, (0, _contribute.getContributionSnippet)(notificationObject.comment))), template === _types.NotificationObjectTemplateType.DETAIL && /*#__PURE__*/_react.default.createElement(_material.Stack, {
      direction: "row",
      justifyContent: "flex-start",
      alignItems: "center",
      spacing: 1
    }, /*#__PURE__*/_react.default.createElement(_DateTimeAgo.default, {
      date: notificationObject.active_at,
      className: classes.activeAt
    }), /*#__PURE__*/_react.default.createElement(_Bullet.default, {
      className: classes.bullet
    }), /*#__PURE__*/_react.default.createElement(_lab.LoadingButton, {
      color: 'inherit',
      classes: {
        root: classes.voteButton
      },
      variant: 'text',
      onClick: () => {
        onVote && index && onVote(index, notificationObject.comment);
      },
      disabled: loadingVote === index,
      loading: loadingVote === index
    }, notificationObject.comment.voted ? /*#__PURE__*/_react.default.createElement(_material.Tooltip, {
      title: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
        id: 'ui.notification.comment.voteDown',
        defaultMessage: 'ui.notification.comment.voteDown'
      })
    }, /*#__PURE__*/_react.default.createElement(_Icon.default, {
      fontSize: 'small',
      color: 'secondary'
    }, "thumb_up_alt")) : /*#__PURE__*/_react.default.createElement(_material.Tooltip, {
      title: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
        id: 'ui.notification.comment.voteUp',
        defaultMessage: 'ui.notification.comment.voteUp'
      })
    }, /*#__PURE__*/_react.default.createElement(_Icon.default, {
      fontSize: 'small',
      color: "inherit"
    }, "thumb_up_off_alt")))))
  })), template === _types.NotificationObjectTemplateType.TOAST && /*#__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(_core.SCRoutes.COMMENT_ROUTE_NAME, (0, _contribute.getRouteData)(notificationObject.comment)),
    sx: {
      textDecoration: 'underline'
    }
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.userToastNotifications.viewContribution",
    defaultMessage: 'ui.userToastNotifications.viewContribution'
  })))));
}