"use strict";

exports.__esModule = true;
exports.default = VoteUpNotification;

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 _DateTimeAgo = _interopRequireDefault(require("../../../shared/DateTimeAgo"));

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

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

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

var _ThumbUpTwoTone = _interopRequireDefault(require("@mui/icons-material/ThumbUpTwoTone"));

var _ThumbUpOutlined = _interopRequireDefault(require("@mui/icons-material/ThumbUpOutlined"));

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

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

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

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)({
  appreciated: {
    id: 'ui.notification.voteUp.appreciated',
    defaultMessage: 'ui.notification.voteUp.appreciated'
  }
});
const PREFIX = 'SCVoteUpNotification';
const Root = (0, _styles.styled)(_material.Box, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  '& .MuiSvgIcon-root': {
    width: '0.7em',
    marginBottom: '0.5px'
  },
  '& a': {
    textDecoration: 'none',
    color: _colors.grey[900]
  }
}));

/**
 * This component render the content of the notification of type vote up
 * @param props
 * @constructor
 */
function VoteUpNotification(props) {
  // PROPS
  const {
    notificationObject,
    id = `n_${props.notificationObject['sid']}`,
    className,
    index,
    onVote,
    loadingVote
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // CONTEXT


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

  const contributionType = (0, _contribute.getContributeType)(notificationObject);
  const feedObjectType = contributionType === _core.SCCommentTypologyType ? (0, _contribute.getContributeType)(notificationObject[contributionType]) : contributionType;
  const feedObjectId = contributionType === _core.SCCommentTypologyType ? notificationObject[_core.SCCommentTypologyType][feedObjectType] : notificationObject[contributionType].id; // INTL

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

  return /*#__PURE__*/_react.default.createElement(Root, _extends({
    id: id,
    className: className
  }, rest), /*#__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.user)
  }, /*#__PURE__*/_react.default.createElement(_material.Avatar, {
    alt: notificationObject.user.username,
    variant: "circular",
    src: notificationObject.user.avatar
  }))), /*#__PURE__*/_react.default.createElement(_material.ListItemText, {
    disableTypography: true,
    primary: /*#__PURE__*/_react.default.createElement(_material.Typography, {
      component: "div",
      sx: {
        display: 'inline'
      },
      color: "primary"
    }, notificationObject.is_new && /*#__PURE__*/_react.default.createElement(_NewChip.default, null), /*#__PURE__*/_react.default.createElement(_core.Link, {
      to: scRoutingContext.url(_core.SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.user)
    }, notificationObject.user.username), ' ', intl.formatMessage(messages.appreciated, {
      username: notificationObject.user.username,
      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[`${contributionType.toUpperCase()}_ROUTE_NAME`], (0, _contribute.getRouteData)(notificationObject[contributionType])),
      sx: {
        textDecoration: 'underline'
      }
    }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
      variant: "body2",
      gutterBottom: true,
      dangerouslySetInnerHTML: {
        __html: notificationObject[contributionType].summary
      }
    })), /*#__PURE__*/_react.default.createElement(_material.Box, {
      component: "span",
      sx: {
        display: 'flex',
        justifyContent: 'flex-start',
        p: '2px'
      }
    }, /*#__PURE__*/_react.default.createElement(_material.Grid, {
      component: "span",
      item: true,
      sm: "auto",
      container: true,
      direction: "row",
      alignItems: "center"
    }, /*#__PURE__*/_react.default.createElement(_DateTimeAgo.default, {
      date: notificationObject.active_at
    }), /*#__PURE__*/_react.default.createElement(_Bullet.default, {
      sx: {
        paddingLeft: '10px',
        paddingTop: '1px'
      }
    }), /*#__PURE__*/_react.default.createElement(_lab.LoadingButton, {
      variant: 'text',
      sx: {
        marginTop: '-1px',
        minWidth: '30px'
      },
      onClick: () => onVote(index, notificationObject[contributionType]),
      disabled: loadingVote !== null,
      loading: loadingVote === index
    }, notificationObject[contributionType].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(_ThumbUpTwoTone.default, {
      fontSize: 'small',
      color: 'secondary'
    })) : /*#__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(_ThumbUpOutlined.default, {
      fontSize: 'small'
    }))))))
  })));
}