"use strict";

exports.__esModule = true;
exports.default = Activities;

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

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

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

var _comments = require("../../../types/comments");

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

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

var _CommentsObject = _interopRequireDefault(require("../../CommentsObject"));

var _ActivitiesMenu = _interopRequireDefault(require("./ActivitiesMenu"));

var _RelevantActivities = _interopRequireDefault(require("./RelevantActivities"));

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

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

const _excluded = ["id", "className", "feedObjectId", "feedObject", "feedObjectType", "CommentsObjectProps", "feedObjectActivities", "comments", "onSetSelectedActivities", "activitiesType"];

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 = 'SCFeedObjectActivities';
const classes = {
  root: `${PREFIX}-root`
};
const Root = (0, _styles.styled)(_material.Box, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  width: '100%'
}));

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

  const {
    id = `feed_object_activities_${props.feedObjectId ? props.feedObjectId : props.feedObject ? props.feedObject.id : ''}`,
    className,
    feedObjectId,
    feedObject,
    feedObjectType = _core.SCFeedObjectTypologyType.POST,
    CommentsObjectProps = {
      CommentComponentProps: {
        variant: 'outlined'
      }
    },
    feedObjectActivities = [],
    comments = [],
    onSetSelectedActivities,
    activitiesType = _feedObject.SCFeedObjectActivitiesType.RECENT_COMMENTS
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // STATE


  const [selectedActivities, setSelectedActivities] = (0, _react.useState)(activitiesType);
  const commentsObject = (0, _core.useSCFetchCommentObjects)({
    id: feedObjectId,
    feedObject,
    feedObjectType,
    pageSize: 3,
    orderBy: selectedActivities === _feedObject.SCFeedObjectActivitiesType.CONNECTIONS_COMMENTS ? _comments.SCCommentsOrderBy.CONNECTION_DESC : selectedActivities === _feedObject.SCFeedObjectActivitiesType.FIRST_COMMENTS ? _comments.SCCommentsOrderBy.ADDED_AT_ASC : _comments.SCCommentsOrderBy.ADDED_AT_DESC
  });
  const objId = commentsObject.feedObject ? commentsObject.feedObject.id : null;
  /**
   * Sync activities type if prop change
   */

  (0, _react.useEffect)(() => {
    setSelectedActivities(activitiesType);
  }, [activitiesType]);
  /**
   * Prefetch comments only if obj
   */

  (0, _react.useEffect)(() => {
    commentsObject.getNextPage();
  }, [objId]);
  /**
   * Handle change activities type
   * @param type
   */

  function handleSelectActivitiesType(type) {
    onSetSelectedActivities(type);
    setSelectedActivities(type);
  }
  /**
   * Render latest activities of feedObject
   */


  function renderRelevantActivities() {
    return /*#__PURE__*/_react.default.createElement(_RelevantActivities.default, {
      activities: feedObjectActivities
    });
  }
  /**
   * Render comments of feedObject
   */


  function renderComments() {
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, (commentsObject.feedObject.comment_count > 0 || comments.length > 0) && /*#__PURE__*/_react.default.createElement(_CommentsObject.default, _extends({
      feedObject: commentsObject.feedObject,
      comments: commentsObject.comments,
      startComments: comments,
      next: commentsObject.next,
      isLoadingNext: commentsObject.isLoadingNext,
      handleNext: commentsObject.getNextPage,
      totalLoadedComments: commentsObject.comments.length + comments.length,
      totalComments: commentsObject.feedObject.comment_count,
      hideAdvertising: true
    }, CommentsObjectProps)));
  }
  /**
   * Renders root object
   */


  return /*#__PURE__*/_react.default.createElement(Root, _extends({
    id: id,
    className: (0, _classnames.default)(classes.root, className)
  }, rest), (commentsObject.feedObject.comment_count || feedObjectActivities && feedObjectActivities.length > 0 || comments.length > 0) && /*#__PURE__*/_react.default.createElement(_ActivitiesMenu.default, {
    selectedActivities: selectedActivities,
    hideRelevantActivitiesItem: !(feedObjectActivities && feedObjectActivities.length > 0),
    onChange: handleSelectActivitiesType
  }), selectedActivities === _feedObject.SCFeedObjectActivitiesType.RELEVANCE_ACTIVITIES ? renderRelevantActivities() : renderComments());
}