"use strict";

exports.__esModule = true;
exports.default = Comment;

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

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

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

var _ChatBubbleOutline = _interopRequireDefault(require("@mui/icons-material/ChatBubbleOutline"));

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

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

var _feedObject = require("../../../../types/feedObject");

const _excluded = ["className", "id", "feedObject", "feedObjectType", "feedObjectTemplate", "withAction", "onViewCommentsAction", "onCommentAction"];

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)({
  comments: {
    id: 'ui.feedObject.comment.comments',
    defaultMessage: 'ui.feedObject.comment.comments'
  },
  comment: {
    id: 'ui.feedObject.comment.comment',
    defaultMessage: 'ui.feedObject.comment.comment'
  }
});
const PREFIX = 'SCCommentObject';
const Root = (0, _styles.styled)(_material.Box, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({}));

function Comment(props) {
  // PROPS
  const {
    className,
    id,
    feedObject,
    feedObjectType = _core.SCFeedObjectTypologyType.POST,
    feedObjectTemplate = _feedObject.FeedObjectTemplateType,
    withAction = false,
    onViewCommentsAction,
    onCommentAction
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // STATE


  const {
    obj,
    setObj
  } = (0, _core.useSCFetchFeedObject)({
    id,
    feedObject,
    feedObjectType
  }); // CONTEXT

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

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

  return /*#__PURE__*/_react.default.createElement(Root, _extends({
    className: className
  }, rest), onViewCommentsAction ? /*#__PURE__*/_react.default.createElement(_material.Button, {
    variant: "text",
    size: "small",
    sx: {
      height: 32
    },
    onClick: onViewCommentsAction,
    color: "primary"
  }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: 'body2'
  }, `${intl.formatMessage(messages.comments, {
    total: obj.comment_count
  })}`)) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, feedObjectTemplate === _feedObject.FeedObjectTemplateType.DETAIL ? /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: 'body2',
    sx: {
      mt: '7px',
      mb: '6px'
    }
  }, `${intl.formatMessage(messages.comments, {
    total: obj.comment_count
  })}`) : /*#__PURE__*/_react.default.createElement(_material.Button, {
    color: "primary",
    variant: "text",
    size: "small",
    component: _core.Link,
    to: scRoutingContext.url(feedObjectType.toLowerCase(), {
      id: obj.id
    }),
    sx: {
      height: 32
    }
  }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: 'body2'
  }, `${intl.formatMessage(messages.comments, {
    total: obj.comment_count
  })}`))), withAction && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_material.Divider, null), /*#__PURE__*/_react.default.createElement(_material.Tooltip, {
    title: `${intl.formatMessage(messages.comment)}`
  }, /*#__PURE__*/_react.default.createElement(_material.Button, {
    onClick: onCommentAction,
    color: "primary"
  }, /*#__PURE__*/_react.default.createElement(_ChatBubbleOutline.default, {
    fontSize: 'large'
  })))));
}