"use strict";

exports.__esModule = true;
exports.default = User;

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

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

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

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

var _Skeleton = _interopRequireDefault(require("./Skeleton"));

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

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

var _FollowUserButton = _interopRequireDefault(require("../FollowUserButton"));

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

var _ConnectionUserButton = _interopRequireDefault(require("../ConnectionUserButton"));

const _excluded = ["id", "user", "handleIgnoreAction", "className", "autoHide", "followUserButtonProps", "connectUserButtonProps"];

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 = 'SCUser';
const classes = {
  avatar: `${PREFIX}-avatar`,
  actions: `${PREFIX}-actions`
};
const Root = (0, _styles.styled)(_Card.default, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  maxWidth: 700,
  marginBottom: theme.spacing(2),
  '& .MuiList-root': {
    padding: 0
  }
}));

function User(props) {
  // PROPS
  const {
    id = null,
    user = null,
    handleIgnoreAction,
    className = null,
    autoHide = false,
    followUserButtonProps = {},
    connectUserButtonProps = {}
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // STATE


  const {
    scUser,
    setSCUser
  } = (0, _core.useSCFetchUser)({
    id,
    user
  }); // CONTEXT

  const scPreferencesContext = (0, _react.useContext)(_core.SCPreferencesContext);
  const scUserContext = (0, _react.useContext)(_core.SCUserContext);
  const scRoutingContext = (0, _core.useSCRouting)();
  const followEnabled = _core.SCPreferences.CONFIGURATIONS_FOLLOW_ENABLED in scPreferencesContext.preferences && scPreferencesContext.preferences[_core.SCPreferences.CONFIGURATIONS_FOLLOW_ENABLED].value;
  /**
   * Render follow action
   * @return {JSX.Element}
   */

  function renderFollowActions() {
    /* TODO: render proper action based on redux connection (follow) store */
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, handleIgnoreAction && /*#__PURE__*/_react.default.createElement(_material.Button, {
      size: "small",
      onClick: handleIgnoreAction
    }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      defaultMessage: "ui.user.ignore",
      id: "ui.user.ignore"
    })), /*#__PURE__*/_react.default.createElement(_FollowUserButton.default, _extends({
      user: scUser
    }, followUserButtonProps)));
  }
  /**
   * Render connection actions
   * @return {JSX.Element}
   */


  function renderConnectionActions() {
    /* TODO: render proper action based on redux connection (friendship) store */
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, handleIgnoreAction && /*#__PURE__*/_react.default.createElement(_material.Button, {
      size: "small",
      onClick: handleIgnoreAction
    }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      defaultMessage: "ui.user.ignore",
      id: "ui.user.ignore"
    })), /*#__PURE__*/_react.default.createElement(_ConnectionUserButton.default, _extends({
      user: scUser
    }, connectUserButtonProps)));
  }
  /**
   * Render authenticated actions
   * @return {JSX.Element}
   */


  function renderAuthenticatedActions() {
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, followEnabled ? renderFollowActions() : renderConnectionActions());
  }

  const u = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, scUser ? /*#__PURE__*/_react.default.createElement(_material.ListItem, {
    button: true,
    component: _core.Link,
    to: scRoutingContext.url(_core.SCRoutes.USER_PROFILE_ROUTE_NAME, scUser)
  }, /*#__PURE__*/_react.default.createElement(_material.ListItemAvatar, null, /*#__PURE__*/_react.default.createElement(_material.Avatar, {
    alt: scUser.username,
    src: scUser.avatar,
    className: classes.avatar
  })), /*#__PURE__*/_react.default.createElement(_material.ListItemText, {
    primary: scUser.username,
    secondary: scUser.description
  }), /*#__PURE__*/_react.default.createElement(_material.ListItemSecondaryAction, {
    className: classes.actions
  }, scUserContext.user ? renderAuthenticatedActions() : null)) : /*#__PURE__*/_react.default.createElement(_Skeleton.default, {
    elevation: 0
  }));

  return /*#__PURE__*/_react.default.createElement(Root, _extends({}, rest, {
    className: className
  }), /*#__PURE__*/_react.default.createElement(_List.default, null, u));
}