import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useContext, useEffect, useMemo, useState } from 'react';
import { styled } from '@mui/material/styles';
import { Avatar, Box, Stack, Typography } from '@mui/material';
import { Link, SCPreferences, SCRoutes, SCUserContext, useSCPreferences, useSCRouting } from '@selfcommunity/react-core';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import DateTimeAgo from '../../../shared/DateTimeAgo';
import classNames from 'classnames';
import { SCNotificationObjectTemplateType } from '../../../types';
import NotificationItem from '../../../shared/NotificationItem';
import { LoadingButton } from '@mui/lab';
import UserDeletedSnackBar from '../../../shared/UserDeletedSnackBar';
import UserAvatar from '../../../shared/UserAvatar';
import { PREFIX } from '../constants';
const messages = defineMessages({
    receivePrivateMessage: {
        id: 'ui.notification.receivePrivateMessage',
        defaultMessage: 'ui.notification.receivePrivateMessage'
    }
});
const classes = {
    root: `${PREFIX}-private-message-root`,
    avatar: `${PREFIX}-avatar`,
    actions: `${PREFIX}-actions`,
    replyButton: `${PREFIX}-reply-button`,
    activeAt: `${PREFIX}-active-at`,
    messageLabel: `${PREFIX}-message-label`,
    username: `${PREFIX}-username`,
    messageWrap: `${PREFIX}-message-wrap`,
    message: `${PREFIX}-message`
};
const Root = styled(NotificationItem, {
    name: PREFIX,
    slot: 'PrivateMessageRoot'
})(() => ({}));
/**
 * This component render the content of the notification of type private message
 * @constructor
 * @param props
 */
export default function PrivateMessageNotification(props) {
    var _a, _b, _c, _d, _e;
    // PROPS
    const { notificationObject, id = `n_${props.notificationObject['sid']}`, className, template = SCNotificationObjectTemplateType.DETAIL } = props, rest = __rest(props, ["notificationObject", "id", "className", "template"]);
    // CONTEXT
    const scRoutingContext = useSCRouting();
    const scUserContext = useContext(SCUserContext);
    const scPreferencesContext = useSCPreferences();
    const followEnabled = useMemo(() => SCPreferences.CONFIGURATIONS_FOLLOW_ENABLED in scPreferencesContext.preferences &&
        scPreferencesContext.preferences[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] = useState(null);
    const [openAlert, setOpenAlert] = useState(false);
    // CONST
    const isSnippetTemplate = template === SCNotificationObjectTemplateType.SNIPPET;
    const isToastTemplate = template === SCNotificationObjectTemplateType.TOAST;
    //INTL
    const intl = useIntl();
    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 (_jsx(Root, Object.assign({ id: id, className: classNames(classes.root, className, `${PREFIX}-${template}`), template: template, isNew: notificationObject.is_new, disableTypography: true, image: _jsx(Link, Object.assign({}, (!notificationObject.message.sender.deleted && {
                to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.message.sender)
            }), { onClick: notificationObject.message.sender.deleted ? () => setOpenAlert(true) : null }, { children: _jsx(UserAvatar, Object.assign({ hide: !notificationObject.message.sender.community_badge, smaller: true }, { children: _jsx(Avatar, { alt: notificationObject.message.sender.username, variant: "circular", src: notificationObject.message.sender.avatar, classes: { root: classes.avatar } }) })) })), primary: _jsxs(_Fragment, { children: [isToastTemplate && (_jsxs(Box, { children: [_jsx(Link, Object.assign({}, (!notificationObject.message.sender.deleted && {
                                to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.message.sender)
                            }), { onClick: notificationObject.message.sender.deleted ? () => setOpenAlert(true) : null }, { children: notificationObject.message.sender.username })), ' ', _jsx(FormattedMessage, { id: 'ui.userToastNotifications.privateMessage.sentMessage', defaultMessage: 'ui.userToastNotifications.privateMessage.sentMessage' }), ":", _jsx(Box, Object.assign({ className: classes.messageWrap }, { children: _jsx(Link, Object.assign({ to: ((_a = notificationObject.message) === null || _a === void 0 ? void 0 : _a.group)
                                        ? scRoutingContext.url(SCRoutes.GROUP_MESSAGES_ROUTE_NAME, notificationObject.message.group)
                                        : scRoutingContext.url(SCRoutes.USER_PRIVATE_MESSAGES_ROUTE_NAME, notificationObject.message.sender), className: classes.message }, { children: _jsx(Typography, { variant: "body2", dangerouslySetInnerHTML: { __html: notificationObject.message.message } }) })) }))] })), isSnippetTemplate && (_jsx(Box, { children: _jsxs(Typography, Object.assign({ component: "div", color: "inherit" }, { children: [_jsx(Link, Object.assign({}, (!notificationObject.message.sender.deleted && {
                                    to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.message.sender)
                                }), { onClick: notificationObject.message.sender.deleted ? () => setOpenAlert(true) : null, className: classes.username }, { children: notificationObject.message.sender.username })), ' ', _jsx(Link, Object.assign({ to: ((_b = notificationObject.message) === null || _b === void 0 ? void 0 : _b.group)
                                        ? scRoutingContext.url(SCRoutes.GROUP_MESSAGES_ROUTE_NAME, notificationObject.message.group)
                                        : scRoutingContext.url(SCRoutes.USER_PRIVATE_MESSAGES_ROUTE_NAME, notificationObject.message.sender), className: classes.messageLabel }, { children: intl.formatMessage(messages.receivePrivateMessage, {
                                        total: 1,
                                        b: (...chunks) => _jsx("strong", { children: chunks })
                                    }) }))] })) }))] }), footer: isToastTemplate && (_jsxs(Stack, Object.assign({ direction: "row", justifyContent: "space-between", alignItems: "center", spacing: 2 }, { children: [_jsx(DateTimeAgo, { date: notificationObject.active_at }), _jsx(Typography, Object.assign({ color: "primary" }, { children: _jsx(Link, Object.assign({ to: ((_c = notificationObject.message) === null || _c === void 0 ? void 0 : _c.group)
                                ? scRoutingContext.url(SCRoutes.GROUP_MESSAGES_ROUTE_NAME, notificationObject.message.group)
                                : scRoutingContext.url(SCRoutes.USER_PRIVATE_MESSAGES_ROUTE_NAME, notificationObject.message.sender) }, { children: scUserContext.user && follower ? (_jsx(FormattedMessage, { id: "ui.userToastNotifications.replyMessage", defaultMessage: 'ui.userToastNotifications.replyMessage' })) : (_jsx(FormattedMessage, { id: "ui.userToastNotifications.viewMessage", defaultMessage: 'ui.userToastNotifications.viewMessage' })) })) }))] }))) }, rest)));
    }
    return (_jsxs(_Fragment, { children: [_jsx(Root, Object.assign({ id: id, className: classNames(classes.root, className, `${PREFIX}-${template}`), template: template, isNew: notificationObject.is_new, disableTypography: true, actions: _jsxs(Stack, Object.assign({ direction: "row", justifyContent: "space-between", alignItems: "center", spacing: 2 }, { children: [_jsx(DateTimeAgo, { date: notificationObject.active_at, className: classes.activeAt }), _jsx(LoadingButton, Object.assign({ color: 'primary', variant: "outlined", size: "small", classes: { root: classes.replyButton }, component: 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(SCRoutes.GROUP_MESSAGES_ROUTE_NAME, notificationObject.message.group)
                                : scRoutingContext.url(SCRoutes.USER_PRIVATE_MESSAGES_ROUTE_NAME, notificationObject.message.sender) }, { children: scUserContext.user && follower ? (_jsx(FormattedMessage, { id: "ui.notification.privateMessage.btnReplyLabel", defaultMessage: "ui.notification.privateMessage.btnReplyLabel" })) : (_jsx(FormattedMessage, { id: "ui.notification.privateMessage.btnViewLabel", defaultMessage: "ui.notification.privateMessage.btnViewLabel" })) }))] })), primary: _jsx(Box, Object.assign({ className: classes.messageWrap }, { children: _jsx(Link, Object.assign({ to: ((_e = notificationObject.message) === null || _e === void 0 ? void 0 : _e.group)
                            ? scRoutingContext.url(SCRoutes.GROUP_MESSAGES_ROUTE_NAME, notificationObject.message.group)
                            : scRoutingContext.url(SCRoutes.USER_PRIVATE_MESSAGES_ROUTE_NAME, notificationObject.message.sender), className: classes.message }, { children: _jsx(Typography, { variant: "body2", dangerouslySetInnerHTML: { __html: notificationObject.message.message } }) })) })) }, rest)), openAlert && _jsx(UserDeletedSnackBar, { open: openAlert, handleClose: () => setOpenAlert(false) })] }));
}
