"use strict";

exports.__esModule = true;
exports.default = UserFollowed;

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

const _excluded = ["userId"];

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

function UserFollowed(_ref) {
  let {
    userId = null
  } = _ref,
      props = _objectWithoutPropertiesLoose(_ref, _excluded);

  const [followed, setFollowed] = (0, _react.useState)([]);
  const [lUfollowed, setLUFollowed] = (0, _react.useState)([]);
  const [visibleUsers, setVisibleUsers] = (0, _react.useState)(3);
  const [loading, setLoading] = (0, _react.useState)(true);
  const [hasMore, setHasMore] = (0, _react.useState)(false);
  const [total, setTotal] = (0, _react.useState)(0);
  const [openMutualPeopleDialog, setOpenMutualPeopleDialog] = (0, _react.useState)(false);

  function fetchUserFollowed() {
    _core.http.request({
      url: _core.Endpoints.UsersFollowed.url({
        id: userId
      }),
      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);
    });
  }

  function fetchLUFollowed() {
    _core.http.request({
      url: _core.Endpoints.UsersFollowed.url({
        id: 1
      }),
      method: _core.Endpoints.UsersFollowed.method
    }).then(res => {
      const data = res.data;
      setLUFollowed(data.results);
    }).catch(error => {
      console.log(error);
    });
  }

  function loadUsers() {
    setVisibleUsers(prevVisibleUsers => prevVisibleUsers + 3);
  }

  function getMutuals(followed, lUfollowed) {
    return followed.filter(f => lUfollowed.some(lU => f.id === lU.id));
  }

  function removeMutuals(followed, mutuals) {
    return followed.filter(function (val) {
      return mutuals.indexOf(val) == -1;
    });
  }

  (0, _react.useEffect)(() => {
    fetchUserFollowed();
    fetchLUFollowed();
  }, []);

  if (loading) {
    return /*#__PURE__*/_react.default.createElement(_Skeleton.default, null);
  }

  const mutualPeople = getMutuals(followed, lUfollowed);
  const filteredFollowers = removeMutuals(followed, mutualPeople);

  const u = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_CardContent.default, null, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: "body1"
  }, "People"), /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: "body2"
  }, total, " (", mutualPeople.length, " mutual)"), /*#__PURE__*/_react.default.createElement(_List.default, null, mutualPeople.slice(0, visibleUsers).map((follower, index) => /*#__PURE__*/_react.default.createElement(_User.default, {
    elevation: 0,
    user: follower,
    key: follower.id
  }))), /*#__PURE__*/_react.default.createElement(_material.Divider, null), /*#__PURE__*/_react.default.createElement(_List.default, null, filteredFollowers.slice(0, visibleUsers).map((followed, index) => /*#__PURE__*/_react.default.createElement(_User.default, {
    elevation: 0,
    user: followed,
    key: followed.id
  }))), hasMore && /*#__PURE__*/_react.default.createElement(_material.Button, {
    size: "small",
    sx: {
      color: ' black'
    },
    onClick: () => loadUsers()
  }, "See More"), openMutualPeopleDialog && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null)));

  if (!props.autoHide) {
    return /*#__PURE__*/_react.default.createElement(Root, props, u);
  }

  return null;
}