"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const material_1 = require("@mui/material");
const styles_1 = require("@mui/material/styles");
const system_1 = require("@mui/system");
const react_core_1 = require("@selfcommunity/react-core");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const react_1 = tslib_1.__importStar(require("react"));
const react_intl_1 = require("react-intl");
const BaseItemButton_1 = tslib_1.__importDefault(require("../../shared/BaseItemButton"));
const UserAvatar_1 = tslib_1.__importDefault(require("../../shared/UserAvatar"));
const UserDeletedSnackBar_1 = tslib_1.__importDefault(require("../../shared/UserDeletedSnackBar"));
const ConnectionUserButton_1 = tslib_1.__importDefault(require("../ConnectionUserButton"));
const constants_1 = require("./constants");
const Skeleton_1 = tslib_1.__importDefault(require("./Skeleton"));
const messages = (0, react_intl_1.defineMessages)({
    userFollowers: {
        id: 'ui.user.userFollowers',
        defaultMessage: 'ui.user.userFollowers'
    }
});
const classes = {
    root: `${constants_1.PREFIX}-root`,
    avatar: `${constants_1.PREFIX}-avatar`,
    staffBadgeLabel: `${constants_1.PREFIX}-staff-badge-label`,
    groupAdminBadgeLabel: `${constants_1.PREFIX}-group-admin-badge-label`
};
const Root = (0, styles_1.styled)(BaseItemButton_1.default, {
    name: constants_1.PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({}));
const PREFERENCES = [react_core_1.SCPreferences.STAFF_STAFF_BADGE_LABEL, react_core_1.SCPreferences.STAFF_STAFF_BADGE_ICON];
/**
 * > API documentation for the Community-JS User component. Learn about the available props and the CSS API.
 *
 *
 * This component renders a user item.
 * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/User)

 #### Import

 ```jsx
 import {User} from '@selfcommunity/react-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.|
 |avatar|.SCUser-avatar|Styles applied to the avatar element.|
 |staffBadgeLabel|.SCUser-staff-badge-label|Styles applied to the staff badge label element.|

 *
 * @param inProps
 */
function User(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: constants_1.PREFIX
    });
    const { userId = null, user = null, handleIgnoreAction, className = null, followConnectUserButtonProps = {}, showFollowers = false, elevation, badgeContent = null, actions = null, isGroupAdmin = false, buttonProps = null, secondary = null } = props, rest = tslib_1.__rest(props, ["userId", "user", "handleIgnoreAction", "className", "followConnectUserButtonProps", "showFollowers", "elevation", "badgeContent", "actions", "isGroupAdmin", "buttonProps", "secondary"]);
    // STATE
    const { scUser } = (0, react_core_1.useSCFetchUser)({ id: userId, user });
    // CONTEXT
    const scRoutingContext = (0, react_core_1.useSCRouting)();
    // PREFERENCES
    const scPreferences = (0, react_core_1.useSCPreferences)();
    const preferences = (0, react_1.useMemo)(() => {
        const _preferences = {};
        PREFERENCES.map((p) => (_preferences[p] = p in scPreferences.preferences ? scPreferences.preferences[p].value : null));
        return _preferences;
    }, [scPreferences.preferences]);
    const hasBadge = scUser && scUser.community_badge;
    const [openAlert, setOpenAlert] = (0, react_1.useState)(false);
    // INTL
    const intl = (0, react_intl_1.useIntl)();
    /**
     * Render authenticated actions
     * @return {JSX.Element}
     */
    function renderAuthenticatedActions() {
        return ((0, jsx_runtime_1.jsxs)(react_1.default.Fragment, { children: [handleIgnoreAction && ((0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ size: "small", onClick: handleIgnoreAction }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.user.ignore", id: "ui.user.ignore" }) }))), (0, jsx_runtime_1.jsx)(ConnectionUserButton_1.default, Object.assign({ user: scUser }, followConnectUserButtonProps))] }));
    }
    /**
     * Renders user object
     */
    if (!scUser) {
        return (0, jsx_runtime_1.jsx)(Skeleton_1.default, { elevation: elevation });
    }
    /**
     * Renders root object
     */
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Root, Object.assign({ elevation: elevation }, rest, { className: (0, classnames_1.default)(classes.root, className), ButtonBaseProps: buttonProps !== null && buttonProps !== void 0 ? buttonProps : (scUser.deleted
                    ? { onClick: () => setOpenAlert(true) }
                    : { component: react_core_1.Link, to: scRoutingContext.url(react_core_1.SCRoutes.USER_PROFILE_ROUTE_NAME, scUser) }), image: badgeContent ? ((0, jsx_runtime_1.jsx)(material_1.Badge, Object.assign({ overlap: "circular", anchorOrigin: { vertical: 'bottom', horizontal: 'right' }, badgeContent: badgeContent }, { children: (0, jsx_runtime_1.jsx)(material_1.Avatar, { alt: scUser.username, src: scUser.avatar, className: classes.avatar }) }))) : ((0, jsx_runtime_1.jsx)(UserAvatar_1.default, Object.assign({ hide: !hasBadge }, { children: (0, jsx_runtime_1.jsx)(material_1.Avatar, { alt: scUser.username, src: scUser.avatar, className: classes.avatar }) }))), primary: (hasBadge && preferences) || isGroupAdmin ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [scUser.username, (0, jsx_runtime_1.jsx)(material_1.Chip, { component: "span", className: isGroupAdmin ? classes.groupAdminBadgeLabel : classes.staffBadgeLabel, size: "small", label: isGroupAdmin ? ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.user.group.admin", id: "ui.user.group.admin" })) : (preferences[react_core_1.SCPreferences.STAFF_STAFF_BADGE_LABEL]) })] })) : (scUser.username), secondary: secondary || (showFollowers ? `${intl.formatMessage(messages.userFollowers, { total: scUser.followers_counter })}` : scUser.description), actions: actions !== null && actions !== void 0 ? actions : renderAuthenticatedActions() })), openAlert && (0, jsx_runtime_1.jsx)(UserDeletedSnackBar_1.default, { open: openAlert, handleClose: () => setOpenAlert(false) })] }));
}
exports.default = User;
