"use strict";

exports.__esModule = true;
exports.default = TrendingPeople;

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

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

var _List = _interopRequireDefault(require("@mui/material/List"));

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

var _Card = _interopRequireDefault(require("@mui/material/Card"));

var _CardContent = _interopRequireDefault(require("@mui/material/CardContent"));

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

var _PeopleSuggestion = require("../PeopleSuggestion");

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

var _User = _interopRequireDefault(require("../User"));

const _excluded = ["categoryId", "autoHide", "className", "UserProps"];

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 = 'SCTrendingPeople';
const Root = (0, _styles.styled)(_Card.default, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  maxWidth: 700,
  marginBottom: theme.spacing(2)
}));

function TrendingPeople(props) {
  // CONST
  const limit = 3; // PROPS

  const {
    categoryId,
    autoHide,
    className,
    UserProps = {}
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // STATE


  const [people, setPeople] = (0, _react.useState)([]);
  const [visiblePeople, setVisiblePeople] = (0, _react.useState)(limit);
  const [loading, setLoading] = (0, _react.useState)(true);
  const [hasMore, setHasMore] = (0, _react.useState)(false);
  const [openTrendingPeopleDialog, setOpenTrendingPeopleDialog] = (0, _react.useState)(false);
  const [total, setTotal] = (0, _react.useState)(0);
  /**
   * Fetches trending people list
   */

  function fetchTrendingPeople() {
    setLoading(true);

    _core.http.request({
      url: _core.Endpoints.CategoryTrendingPeople.url({
        id: categoryId
      }),
      method: _core.Endpoints.CategoryTrendingPeople.method
    }).then(res => {
      const data = res.data;
      setPeople(data.results);
      setHasMore(data.count > visiblePeople);
      setLoading(false);
      setTotal(data.count);
    }).catch(error => {
      console.log(error);
    });
  }
  /**
   * Loads more people on "see more" button click
   */


  function loadPeople() {
    const newIndex = visiblePeople + limit;
    const newHasMore = newIndex < people.length - 1;
    setVisiblePeople(newIndex);
    setHasMore(newHasMore);
  }
  /**
   * On mount, fetches trending people list
   */


  (0, _react.useEffect)(() => {
    fetchTrendingPeople();
  }, []);
  /**
   * Renders trending people list
   */

  const p = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, loading ? /*#__PURE__*/_react.default.createElement(_PeopleSuggestion.PeopleSuggestionSkeleton, null) : /*#__PURE__*/_react.default.createElement(_CardContent.default, null, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: "body1"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.trendingPeople.title",
    defaultMessage: "ui.trendingPeople.title"
  })), !total ? /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: "body2"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.trendingPeople.noResults",
    defaultMessage: "ui.trendingPeople.noResults"
  })) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_List.default, null, people.slice(0, visiblePeople).map((user, index) => /*#__PURE__*/_react.default.createElement(_User.default, _extends({
    elevation: 0,
    user: user,
    id: user.id,
    key: index
  }, UserProps))))), hasMore && /*#__PURE__*/_react.default.createElement(_material.Button, {
    size: "small",
    onClick: () => loadPeople()
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.trendingPeople.showAll",
    defaultMessage: "ui.trendingPeople.showAll"
  })), openTrendingPeopleDialog && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null)));
  /**
   * Renders root object (if not hidden by autoHide prop)
   */


  if (!autoHide) {
    return /*#__PURE__*/_react.default.createElement(Root, _extends({
      className: className
    }, rest), p);
  }

  return null;
}