"use strict";

exports.__esModule = true;
exports.default = UsersFollowed;

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 _Skeleton = _interopRequireDefault(require("../PeopleSuggestion/Skeleton"));

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

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

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

const messages = (0, _reactIntl.defineMessages)({
  usersFollowed: {
    id: 'ui.usersFollowed.usersFollowed',
    defaultMessage: 'ui.usersFollowed.usersFollowed'
  },
  noUsers: {
    id: 'ui.usersFollowed.subtitle.noResults',
    defaultMessage: 'ui.usersFollowed.subtitle.noResults'
  }
});
const PREFIX = 'SCUsersFollowed';
const Root = (0, _styles.styled)(_Card.default, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  maxWidth: 700,
  marginBottom: theme.spacing(2)
}));

function UsersFollowed(props) {
  // CONST
  const limit = 3; // INTL

  const intl = (0, _reactIntl.useIntl)(); // CONTEXT

  const scUserContext = (0, _react.useContext)(_core.SCUserContext); // PROPS

  const {
    autoHide,
    className,
    UserProps = {}
  } = props; // STATE

  const [followed, setFollowed] = (0, _react.useState)([]);
  const [visibleUsers, setVisibleUsers] = (0, _react.useState)(limit);
  const [loading, setLoading] = (0, _react.useState)(true);
  const [hasMore, setHasMore] = (0, _react.useState)(false);
  const [total, setTotal] = (0, _react.useState)(0);
  /**
   * Handles list change on user follow
   */

  function handleOnFollowUser(user, follow) {
    setFollowed(followed.filter(u => u.id !== user.id));
    setTotal(prev => prev - 1);

    if (visibleUsers < limit && total > 1) {
      loadUsers(1);
    }
  }
  /**
   * Fetches the list of users followed
   */


  function fetchFollowed() {
    _core.http.request({
      url: _core.Endpoints.UsersFollowed.url({
        id: scUserContext.user['id']
      }),
      method: _core.Endpoints.UsersFollowed.method
    }).then(res => {
      const data = res.data;
      setFollowed(data.results);
      setHasMore(data.count > visibleUsers);
      setLoading(false);
      setTotal(data.count);
    }).catch(error => {
      console.log(error);
    });
  }
  /**
   * Loads more users on "see more" button click
   */


  function loadUsers(n) {
    const newIndex = visibleUsers + n;
    const newHasMore = newIndex < followed.length - 1;
    setVisibleUsers(newIndex);
    setHasMore(newHasMore);
  }
  /**
   * On mount, fetches the list of users followed
   */


  (0, _react.useEffect)(() => {
    if (scUserContext.user) {
      fetchFollowed();
    }
  }, []);
  /**
   * Renders the list of users followed
   */

  const u = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, loading ? /*#__PURE__*/_react.default.createElement(_Skeleton.default, {
    elevation: 0
  }) : /*#__PURE__*/_react.default.createElement(_CardContent.default, null, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: "body1"
  }, `${intl.formatMessage(messages.usersFollowed, {
    total: total
  })}`), !total ? /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: "body2"
  }, `${intl.formatMessage(messages.noUsers)}`) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_List.default, null, followed.slice(0, visibleUsers).map((user, index) => /*#__PURE__*/_react.default.createElement("div", {
    key: index
  }, /*#__PURE__*/_react.default.createElement(_User.default, _extends({
    elevation: 0,
    user: user,
    key: user.id,
    followUserButtonProps: {
      onFollow: handleOnFollowUser
    }
  }, UserProps))))), hasMore && /*#__PURE__*/_react.default.createElement(_material.Button, {
    size: "small",
    sx: {
      color: ' black'
    },
    onClick: () => loadUsers(limit)
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.button.showMore",
    defaultMessage: "ui.button.showMore"
  })))));
  /**
   * Renders root object (if not hidden by autoHide prop)
   */


  if (!autoHide && scUserContext.user) {
    return /*#__PURE__*/_react.default.createElement(Root, {
      className: className
    }, u);
  }

  return null;
}