"use strict";

exports.__esModule = true;
exports.default = RelevantActivities;

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

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

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

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

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

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

var _CommentActivity = _interopRequireDefault(require("./CommentActivity"));

var _VoteUpActivity = _interopRequireDefault(require("./VoteUpActivity"));

var _FollowActivity = _interopRequireDefault(require("./FollowActivity"));

var _PollVoteActivity = _interopRequireDefault(require("./PollVoteActivity"));

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

var _useThemeProps = _interopRequireDefault(require("@mui/material/styles/useThemeProps"));

const _excluded = ["className", "activities", "showMaxRelevantActivities"];

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

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 PREFIX = 'SCFeedRelevantActivities';
const classes = {
  root: `${PREFIX}-root`,
  activity: `${PREFIX}-activity`
};
const Root = (0, _styles.styled)(_material.Box, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  [`& .MuiList-root, & .${classes.activity}`]: {
    padding: 0
  }
}));

function RelevantActivities(inProps) {
  //PROPS
  const props = (0, _useThemeProps.default)({
    props: inProps,
    name: PREFIX
  });

  const {
    className = null,
    activities = [],
    showMaxRelevantActivities = 5
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // STATE


  const [openOtherActivities, setOpenOtherActivities] = (0, _react.useState)(false);
  /**
   * Renders single relevant activity
   * @param a
   * @param i
   */

  function renderActivity(a, i) {
    if (a.type === _core.SCFeedUnitActivityTypologyType.COMMENT) {
      return /*#__PURE__*/_react.default.createElement(_CommentActivity.default, {
        activityObject: a,
        key: i,
        elevation: 0
      });
    } else if (a.type === _core.SCFeedUnitActivityTypologyType.FOLLOW) {
      return /*#__PURE__*/_react.default.createElement(_FollowActivity.default, {
        activityObject: a,
        key: i,
        elevation: 0
      });
    } else if (a.type === _core.SCFeedUnitActivityTypologyType.VOTE) {
      return /*#__PURE__*/_react.default.createElement(_VoteUpActivity.default, {
        activityObject: a,
        key: i,
        elevation: 0
      });
    } else if (a.type === _core.SCFeedUnitActivityTypologyType.POLLVOTE) {
      return /*#__PURE__*/_react.default.createElement(_PollVoteActivity.default, {
        activityObject: a,
        key: i,
        elevation: 0
      });
    }

    return null;
  }
  /**
   * Renders root object
   */


  return /*#__PURE__*/_react.default.createElement(Root, _extends({
    className: (0, _classnames.default)(classes.root, className)
  }, rest), activities.length <= 0 ? /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: "body2"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.feedObject.relevantActivities.noResults",
    defaultMessage: "ui.feedObject.relevantActivities.noResults"
  })) : /*#__PURE__*/_react.default.createElement(_material.List, null, activities.slice(0, showMaxRelevantActivities).map((a, i) => /*#__PURE__*/_react.default.createElement(_material.ListItem, {
    className: classes.activity
  }, renderActivity(a, i))), !openOtherActivities && activities.length > showMaxRelevantActivities && /*#__PURE__*/_react.default.createElement(_material.ListItemButton, {
    onClick: () => setOpenOtherActivities(prev => !prev)
  }, /*#__PURE__*/_react.default.createElement(_material.ListItemText, {
    primary: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: 'ui.feedObject.relevantActivities.showOthers',
      defaultMessage: 'ui.feedObject.relevantActivities.showOthers'
    })
  }), openOtherActivities ? /*#__PURE__*/_react.default.createElement(_Icon.default, null, "expand_less") : /*#__PURE__*/_react.default.createElement(_Icon.default, null, "expand_more")), /*#__PURE__*/_react.default.createElement(_material.Collapse, {
    in: openOtherActivities,
    timeout: "auto",
    unmountOnExit: true
  }, activities.slice(showMaxRelevantActivities).map((a, i) => /*#__PURE__*/_react.default.createElement(_material.ListItem, {
    className: classes.activity
  }, renderActivity(a, i))))));
}