"use strict";

exports.__esModule = true;
exports.default = UserProfileInfo;

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

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

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

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

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

var _UserProfile = require("../../constants/UserProfile");

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

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

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

const _excluded = ["className", "userId", "user", "fields"];

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)({
  realName: {
    id: 'ui.userProfileInfo.realName',
    defaultMessage: 'ui.userProfileInfo.realName'
  },
  dateJoined: {
    id: 'ui.userProfileInfo.dateJoined',
    defaultMessage: 'ui.userProfileInfo.dateJoined'
  },
  bio: {
    id: 'ui.userProfileInfo.bio',
    defaultMessage: 'ui.userProfileInfo.bio'
  },
  location: {
    id: 'ui.userProfileInfo.location',
    defaultMessage: 'ui.userProfileInfo.location'
  },
  dateOfBirth: {
    id: 'ui.userProfileInfo.dateOfBirth',
    defaultMessage: 'ui.userProfileInfo.dateOfBirth'
  },
  description: {
    id: 'ui.userProfileInfo.description',
    defaultMessage: 'ui.userProfileInfo.description'
  },
  gender: {
    id: 'ui.userProfileInfo.gender',
    defaultMessage: 'ui.userProfileInfo.gender'
  },
  website: {
    id: 'ui.userProfileInfo.website',
    defaultMessage: 'ui.userProfileInfo.website'
  }
});
const PREFIX = 'SCUserProfileInfo';
const classes = {
  root: `${PREFIX}-root`,
  field: `${PREFIX}-field`
};
const Root = (0, _styles.styled)(_material.Box, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  margin: theme.spacing(2),
  [`& .${classes.field}`]: {
    fontWeight: 'bold'
  }
}));

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

 #### Import

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

 #### Component Name

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


 #### CSS

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

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

  const {
    className = null,
    userId = null,
    user = null,
    fields = [..._UserProfile.DEFAULT_FIELDS]
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // STATE


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

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

  const renderField = (user, field) => {
    switch (field) {
      case _core.SCUserFields.DATE_JOINED:
      case _core.SCUserFields.DATE_OF_BIRTH:
        return `${intl.formatDate(user[field], {
          year: 'numeric',
          month: 'numeric',
          day: 'numeric'
        })}`;

      default:
        return user[field];
    }
  };

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

  if (fields.length === 0) {
    return null;
  }

  return /*#__PURE__*/_react.default.createElement(Root, _extends({
    className: (0, _classnames.default)(classes.root, className)
  }, rest), /*#__PURE__*/_react.default.createElement(_material.Grid, {
    container: true,
    spacing: 2
  }, fields.map(field => {
    if (scUser[field]) {
      return /*#__PURE__*/_react.default.createElement(_material.Grid, {
        item: true,
        xs: 6,
        key: field
      }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
        variant: "body2"
      }, /*#__PURE__*/_react.default.createElement("span", {
        className: classes.field
      }, intl.formatMessage(messages[_core.StringUtils.camelCase(field)]), ":"), " ", renderField(scUser, field)));
    }

    return null;
  })));
}