"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 react_core_1 = require("@selfcommunity/react-core");
const react_intl_1 = require("react-intl");
const DateTimeAgo_1 = tslib_1.__importDefault(require("../../../shared/DateTimeAgo"));
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const types_1 = require("../../../types");
const NotificationItem_1 = tslib_1.__importDefault(require("../../../shared/NotificationItem"));
const lab_1 = require("@mui/lab");
const UserDeletedSnackBar_1 = tslib_1.__importDefault(require("../../../shared/UserDeletedSnackBar"));
const UserAvatar_1 = tslib_1.__importDefault(require("../../../shared/UserAvatar"));
const constants_1 = require("../constants");
const messages = (0, react_intl_1.defineMessages)({
    receivePrivateMessage: {
        id: 'ui.notification.receivePrivateMessage',
        defaultMessage: 'ui.notification.receivePrivateMessage'
    }
});
const classes = {
    root: `${constants_1.PREFIX}-private-message-root`,
    avatar: `${constants_1.PREFIX}-avatar`,
    actions: `${constants_1.PREFIX}-actions`,
    replyButton: `${constants_1.PREFIX}-reply-button`,
    activeAt: `${constants_1.PREFIX}-active-at`,
    messageLabel: `${constants_1.PREFIX}-message-label`,
    username: `${constants_1.PREFIX}-username`,
    messageWrap: `${constants_1.PREFIX}-message-wrap`,
    message: `${constants_1.PREFIX}-message`
};
const Root = (0, styles_1.styled)(NotificationItem_1.default, {
    name: constants_1.PREFIX,
    slot: 'PrivateMessageRoot'
})(() => ({}));
/**
 * This component render the content of the notification of type private message
 * @constructor
 * @param props
 */
function PrivateMessageNotification(props) {
    var _a, _b, _c, _d, _e;
    // PROPS
    const { notificationObject, id = `n_${props.notificationObject['sid']}`, className, template = types_1.SCNotificationObjectTemplateType.DETAIL } = props, rest = tslib_1.__rest(props, ["notificationObject", "id", "className", "template"]);
    // CONTEXT
    const scRoutingContext = (0, react_core_1.useSCRouting)();
    const scUserContext = (0, react_1.useContext)(react_core_1.SCUserContext);
    const scPreferencesContext = (0, react_core_1.useSCPreferences)();
    const followEnabled = (0, react_1.useMemo)(() => react_core_1.SCPreferences.CONFIGURATIONS_FOLLOW_ENABLED in scPreferencesContext.preferences &&
        scPreferencesContext.preferences[react_core_1.SCPreferences.CONFIGURATIONS_FOLLOW_ENABLED].value, [scPreferencesContext.preferences]);
    const manager = followEnabled
        ? scUserContext.managers.followers
        : scUserContext.managers.connections;
    function checkFollowerOrConnection(user) {
        if ('isFollower' in manager) {
            return manager.isFollower(user);
        }
        // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
        // @ts-ignore
        return manager.status(user);
    }
    const [follower, setFollower] = (0, react_1.useState)(null);
    const [openAlert, setOpenAlert] = (0, react_1.useState)(false);
    // CONST
    const isSnippetTemplate = template === types_1.SCNotificationObjectTemplateType.SNIPPET;
    const isToastTemplate = template === types_1.SCNotificationObjectTemplateType.TOAST;
    //INTL
    const intl = (0, react_intl_1.useIntl)();
    (0, react_1.useEffect)(() => {
        /**
         * Call scFollowedManager.isFollower inside an effect
         * to avoid warning rendering child during update parent state
         */
        if (scUserContext.user && scUserContext.user.id !== notificationObject.message.sender.id && !notificationObject.message.sender.deleted) {
            setFollower(checkFollowerOrConnection(notificationObject.message.sender));
        }
    });
    // RENDER
    if (isSnippetTemplate || isToastTemplate) {
        return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ id: id, className: (0, classnames_1.default)(classes.root, className, `${constants_1.PREFIX}-${template}`), template: template, isNew: notificationObject.is_new, disableTypography: true, image: (0, jsx_runtime_1.jsx)(react_core_1.Link, Object.assign({}, (!notificationObject.message.sender.deleted && {
                to: scRoutingContext.url(react_core_1.SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.message.sender)
            }), { onClick: notificationObject.message.sender.deleted ? () => setOpenAlert(true) : null }, { children: (0, jsx_runtime_1.jsx)(UserAvatar_1.default, Object.assign({ hide: !notificationObject.message.sender.community_badge, smaller: true }, { children: (0, jsx_runtime_1.jsx)(material_1.Avatar, { alt: notificationObject.message.sender.username, variant: "circular", src: notificationObject.message.sender.avatar, classes: { root: classes.avatar } }) })) })), primary: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [isToastTemplate && ((0, jsx_runtime_1.jsxs)(material_1.Box, { children: [(0, jsx_runtime_1.jsx)(react_core_1.Link, Object.assign({}, (!notificationObject.message.sender.deleted && {
                                to: scRoutingContext.url(react_core_1.SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.message.sender)
                            }), { onClick: notificationObject.message.sender.deleted ? () => setOpenAlert(true) : null }, { children: notificationObject.message.sender.username })), ' ', (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: 'ui.userToastNotifications.privateMessage.sentMessage', defaultMessage: 'ui.userToastNotifications.privateMessage.sentMessage' }), ":", (0, jsx_runtime_1.jsx)(material_1.Box, Object.assign({ className: classes.messageWrap }, { children: (0, jsx_runtime_1.jsx)(react_core_1.Link, Object.assign({ to: ((_a = notificationObject.message) === null || _a === void 0 ? void 0 : _a.group)
                                        ? scRoutingContext.url(react_core_1.SCRoutes.GROUP_MESSAGES_ROUTE_NAME, notificationObject.message.group)
                                        : scRoutingContext.url(react_core_1.SCRoutes.USER_PRIVATE_MESSAGES_ROUTE_NAME, notificationObject.message.sender), className: classes.message }, { children: (0, jsx_runtime_1.jsx)(material_1.Typography, { variant: "body2", dangerouslySetInnerHTML: { __html: notificationObject.message.message } }) })) }))] })), isSnippetTemplate && ((0, jsx_runtime_1.jsx)(material_1.Box, { children: (0, jsx_runtime_1.jsxs)(material_1.Typography, Object.assign({ component: "div", color: "inherit" }, { children: [(0, jsx_runtime_1.jsx)(react_core_1.Link, Object.assign({}, (!notificationObject.message.sender.deleted && {
                                    to: scRoutingContext.url(react_core_1.SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.message.sender)
                                }), { onClick: notificationObject.message.sender.deleted ? () => setOpenAlert(true) : null, className: classes.username }, { children: notificationObject.message.sender.username })), ' ', (0, jsx_runtime_1.jsx)(react_core_1.Link, Object.assign({ to: ((_b = notificationObject.message) === null || _b === void 0 ? void 0 : _b.group)
                                        ? scRoutingContext.url(react_core_1.SCRoutes.GROUP_MESSAGES_ROUTE_NAME, notificationObject.message.group)
                                        : scRoutingContext.url(react_core_1.SCRoutes.USER_PRIVATE_MESSAGES_ROUTE_NAME, notificationObject.message.sender), className: classes.messageLabel }, { children: intl.formatMessage(messages.receivePrivateMessage, {
                                        total: 1,
                                        b: (...chunks) => (0, jsx_runtime_1.jsx)("strong", { children: chunks })
                                    }) }))] })) }))] }), footer: isToastTemplate && ((0, jsx_runtime_1.jsxs)(material_1.Stack, Object.assign({ direction: "row", justifyContent: "space-between", alignItems: "center", spacing: 2 }, { children: [(0, jsx_runtime_1.jsx)(DateTimeAgo_1.default, { date: notificationObject.active_at }), (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ color: "primary" }, { children: (0, jsx_runtime_1.jsx)(react_core_1.Link, Object.assign({ to: ((_c = notificationObject.message) === null || _c === void 0 ? void 0 : _c.group)
                                ? scRoutingContext.url(react_core_1.SCRoutes.GROUP_MESSAGES_ROUTE_NAME, notificationObject.message.group)
                                : scRoutingContext.url(react_core_1.SCRoutes.USER_PRIVATE_MESSAGES_ROUTE_NAME, notificationObject.message.sender) }, { children: scUserContext.user && follower ? ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.userToastNotifications.replyMessage", defaultMessage: 'ui.userToastNotifications.replyMessage' })) : ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.userToastNotifications.viewMessage", defaultMessage: 'ui.userToastNotifications.viewMessage' })) })) }))] }))) }, rest)));
    }
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Root, Object.assign({ id: id, className: (0, classnames_1.default)(classes.root, className, `${constants_1.PREFIX}-${template}`), template: template, isNew: notificationObject.is_new, disableTypography: true, actions: (0, jsx_runtime_1.jsxs)(material_1.Stack, Object.assign({ direction: "row", justifyContent: "space-between", alignItems: "center", spacing: 2 }, { children: [(0, jsx_runtime_1.jsx)(DateTimeAgo_1.default, { date: notificationObject.active_at, className: classes.activeAt }), (0, jsx_runtime_1.jsx)(lab_1.LoadingButton, Object.assign({ color: 'primary', variant: "outlined", size: "small", classes: { root: classes.replyButton }, component: react_core_1.Link, loading: notificationObject.message.sender.deleted
                                ? null
                                : scUserContext.user
                                    ? follower === null || manager.isLoading(notificationObject.message.sender)
                                    : null, to: ((_d = notificationObject.message) === null || _d === void 0 ? void 0 : _d.group)
                                ? scRoutingContext.url(react_core_1.SCRoutes.GROUP_MESSAGES_ROUTE_NAME, notificationObject.message.group)
                                : scRoutingContext.url(react_core_1.SCRoutes.USER_PRIVATE_MESSAGES_ROUTE_NAME, notificationObject.message.sender) }, { children: scUserContext.user && follower ? ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.notification.privateMessage.btnReplyLabel", defaultMessage: "ui.notification.privateMessage.btnReplyLabel" })) : ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.notification.privateMessage.btnViewLabel", defaultMessage: "ui.notification.privateMessage.btnViewLabel" })) }))] })), primary: (0, jsx_runtime_1.jsx)(material_1.Box, Object.assign({ className: classes.messageWrap }, { children: (0, jsx_runtime_1.jsx)(react_core_1.Link, Object.assign({ to: ((_e = notificationObject.message) === null || _e === void 0 ? void 0 : _e.group)
                            ? scRoutingContext.url(react_core_1.SCRoutes.GROUP_MESSAGES_ROUTE_NAME, notificationObject.message.group)
                            : scRoutingContext.url(react_core_1.SCRoutes.USER_PRIVATE_MESSAGES_ROUTE_NAME, notificationObject.message.sender), className: classes.message }, { children: (0, jsx_runtime_1.jsx)(material_1.Typography, { variant: "body2", dangerouslySetInnerHTML: { __html: notificationObject.message.message } }) })) })) }, rest)), openAlert && (0, jsx_runtime_1.jsx)(UserDeletedSnackBar_1.default, { open: openAlert, handleClose: () => setOpenAlert(false) })] }));
}
exports.default = PrivateMessageNotification;
