"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const styles_1 = require("@mui/material/styles");
const material_1 = require("@mui/material");
const Skeleton_1 = tslib_1.__importDefault(require("./Skeleton"));
const react_intl_1 = require("react-intl");
const types_1 = require("@selfcommunity/types");
const react_core_1 = require("@selfcommunity/react-core");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const system_1 = require("@mui/system");
const UserAvatar_1 = tslib_1.__importDefault(require("../../shared/UserAvatar"));
const constants_1 = require("./constants");
const classes = {
    root: `${constants_1.PREFIX}-root`,
    username: `${constants_1.PREFIX}-username`,
    badgeLabel: `${constants_1.PREFIX}-badge-label`,
    time: `${constants_1.PREFIX}-time`,
    menuItem: `${constants_1.PREFIX}-menu-item`,
    unread: `${constants_1.PREFIX}-unread`
};
const Root = (0, styles_1.styled)(material_1.ListItem, {
    name: constants_1.PREFIX,
    slot: 'Root'
})(() => ({}));
const PREFERENCES = [react_core_1.SCPreferences.STAFF_STAFF_BADGE_LABEL, react_core_1.SCPreferences.STAFF_STAFF_BADGE_ICON];
/**
 * > API documentation for the Community-JS PrivateMessageSnippetItem component. Learn about the available props and the CSS API.

 #### Import

 ```jsx
 import {PrivateMessageSnippetItem} from '@selfcommunity/react-ui';
 ```

 #### Component Name

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


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCPrivateMessageSnippetItem-root|Styles applied to the root element.|
 |username|.SCPrivateMessageSnippetItem-username|Styles applied to the username element.|
 |badgeLabel|.SCPrivateMessageSnippetItem-badgeLabel|Styles applied to the badgeLabel element.|
 |time|.SCPrivateMessageSnippetItem-time|Styles applied to the time element.|
 |menuItem|.SCPrivateMessageSnippetItem-menu-item|Styles applied to the menu item element.|
 |unread|.SCPrivateMessageSnippetItem-unread|Styles applied to snippet item if there's an unread message.|

 * @param inProps
 */
function PrivateMessageSnippetItem(inProps) {
    var _a, _b, _c, _d;
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: constants_1.PREFIX
    });
    const { message = null, className = null, onItemClick = null, secondaryAction = null } = props, rest = tslib_1.__rest(props, ["message", "className", "onItemClick", "secondaryAction"]);
    // CONTEXT
    const scUserContext = (0, react_1.useContext)(react_core_1.SCUserContext);
    // 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]);
    // INTL
    const intl = (0, react_intl_1.useIntl)();
    // STATE
    const hasBadge = () => {
        var _a;
        if (message.receiver) {
            if ((message === null || message === void 0 ? void 0 : message.receiver.id) !== ((_a = scUserContext === null || scUserContext === void 0 ? void 0 : scUserContext.user) === null || _a === void 0 ? void 0 : _a.id)) {
                return message === null || message === void 0 ? void 0 : message.receiver.community_badge;
            }
            return message === null || message === void 0 ? void 0 : message.sender.community_badge;
        }
    };
    if (!message) {
        return (0, jsx_runtime_1.jsx)(Skeleton_1.default, { elevation: 0 });
    }
    /**
     * Renders root object
     */
    return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { secondaryAction: secondaryAction, disablePadding: true }, { children: (0, jsx_runtime_1.jsxs)(material_1.ListItemButton, Object.assign({ onClick: onItemClick, classes: { root: (0, classnames_1.default)({ [classes.unread]: message.thread_status === types_1.SCPrivateMessageStatusType.NEW }) } }, { children: [(0, jsx_runtime_1.jsx)(material_1.ListItemAvatar, { children: (0, jsx_runtime_1.jsx)(UserAvatar_1.default, Object.assign({ hide: !hasBadge() }, { children: message.group ? ((0, jsx_runtime_1.jsx)(material_1.Avatar, { alt: message.group.name, src: message.group.image_big })) : ((0, jsx_runtime_1.jsx)(material_1.Avatar, { alt: ((_a = scUserContext === null || scUserContext === void 0 ? void 0 : scUserContext.user) === null || _a === void 0 ? void 0 : _a.username) === message.receiver.username ? message.sender.username : message.receiver.username, src: ((_b = scUserContext === null || scUserContext === void 0 ? void 0 : scUserContext.user) === null || _b === void 0 ? void 0 : _b.username) === message.receiver.username ? message.sender.avatar : message.receiver.avatar })) })) }), (0, jsx_runtime_1.jsx)(material_1.ListItemText, { primary: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ component: "span", className: classes.username }, { children: message.group
                                    ? message.group.name
                                    : ((_c = scUserContext === null || scUserContext === void 0 ? void 0 : scUserContext.user) === null || _c === void 0 ? void 0 : _c.username) === message.receiver.username
                                        ? message.sender.username
                                        : message.receiver.username })), hasBadge() && preferences && ((0, jsx_runtime_1.jsx)(material_1.Chip, { component: "span", className: classes.badgeLabel, size: "small", label: preferences[react_core_1.SCPreferences.STAFF_STAFF_BADGE_LABEL] })), (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ color: "secondary", className: classes.time, component: "span" }, { children: `${intl.formatDate(message.last_message_at, {
                                    day: '2-digit',
                                    month: '2-digit',
                                    year: '2-digit'
                                })}` }))] }), secondary: (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsx)(material_1.Typography, { component: "span", color: "text.secondary", dangerouslySetInnerHTML: { __html: (_d = message.headline) !== null && _d !== void 0 ? _d : message.message } }) }) })] })) })));
}
exports.default = PrivateMessageSnippetItem;
