"use strict";

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

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

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

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

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

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

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

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

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

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

var _BaseItemButton = _interopRequireDefault(require("../../shared/BaseItemButton"));

const _excluded = ["userId", "user", "handleIgnoreAction", "className", "followConnectUserButtonProps", "showFollowers", "elevation"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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 messages = (0, _reactIntl.defineMessages)({
  userFollowers: {
    id: 'ui.user.userFollowers',
    defaultMessage: 'ui.user.userFollowers'
  }
});
const PREFIX = 'SCUser';
const classes = {
  root: `${PREFIX}-root`
};
const Root = (0, _styles.styled)(_BaseItemButton.default, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({}));

/**
 * > API documentation for the Community-UI User component. Learn about the available props and the CSS API.

 #### Import

 ```jsx
 import {User} from '@selfcommunity/ui';
 ```

 #### Component Name

 The name `SCUser` can be used when providing style overrides in the theme.


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCUser-root|Styles applied to the root element.|

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

  const {
    userId = null,
    user = null,
    handleIgnoreAction,
    className = null,
    followConnectUserButtonProps = {},
    showFollowers = false,
    elevation
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // STATE


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

  const scRoutingContext = (0, _core.useSCRouting)(); // INTL

  const intl = (0, _reactIntl.useIntl)();
  /**
   * Render authenticated actions
   * @return {JSX.Element}
   */

  function renderAuthenticatedActions() {
    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
    }, followConnectUserButtonProps)));
  }
  /**
   * Renders user object
   */


  if (!scUser) {
    return /*#__PURE__*/_react.default.createElement(_Skeleton.default, {
      elevation: elevation
    });
  }
  /**
   * Renders root object
   */


  return /*#__PURE__*/_react.default.createElement(Root, _extends({
    elevation: elevation
  }, rest, {
    className: (0, _classnames.default)(classes.root, className),
    ButtonBaseProps: {
      component: _core.Link,
      to: scRoutingContext.url(_core.SCRoutes.USER_PROFILE_ROUTE_NAME, scUser)
    },
    image: /*#__PURE__*/_react.default.createElement(_material.Avatar, {
      alt: scUser.username,
      src: scUser.avatar
    }),
    primary: scUser.username,
    secondary: showFollowers ? `${intl.formatMessage(messages.userFollowers, {
      total: scUser.followers_counter
    })}` : scUser.description,
    actions: renderAuthenticatedActions()
  }));
}