import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React, { useState } from 'react';
import { styled } from '@mui/material/styles';
import { Avatar, Stack, Typography } from '@mui/material';
import { Link, SCRoutes, 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 { getContributionSnippet, getContributionType, getRouteData } from '../../../utils/contribution';
import NotificationItem from '../../../shared/NotificationItem';
import UserDeletedSnackBar from '../../../shared/UserDeletedSnackBar';
import UserAvatar from '../../../shared/UserAvatar';
import { PREFIX } from '../constants';
const messages = defineMessages({
    contributionFollow: {
        id: 'ui.notification.contributionFollow.follow',
        defaultMessage: 'ui.notification.contributionFollow.follow'
    }
});
const classes = {
    root: `${PREFIX}-contribution-follow-root`,
    avatar: `${PREFIX}-avatar`,
    username: `${PREFIX}-username`,
    followText: `${PREFIX}-follow-text`,
    activeAt: `${PREFIX}-active-at`,
    contributionText: `${PREFIX}-contribution-text`
};
const Root = styled(NotificationItem, {
    name: PREFIX,
    slot: 'ContributionFollowRoot'
})(() => ({}));
/**
 * This component render the content of the notification of type follow (contribution)
 * @constructor
 * @param props
 */
export default function ContributionFollowNotification(props) {
    // 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
    const contributionType = getContributionType(notificationObject);
    // STATE
    const [openAlert, setOpenAlert] = useState(false);
    // INTL
    const intl = useIntl();
    /**
     * Renders root object
     */
    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, image: _jsx(Link, Object.assign({}, (!notificationObject.user.deleted && { to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.user) }), { onClick: notificationObject.user.deleted ? () => setOpenAlert(true) : null }, { children: _jsx(UserAvatar, Object.assign({ hide: !notificationObject.user.community_badge, smaller: true }, { children: _jsx(Avatar, { alt: notificationObject.user.username, variant: "circular", src: notificationObject.user.avatar, classes: { root: classes.avatar } }) })) })), primary: _jsxs(Typography, Object.assign({ component: "span", color: "inherit", className: classes.followText }, { children: [_jsx(Link, Object.assign({}, (!notificationObject.user.deleted && { to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.user) }), { onClick: notificationObject.user.deleted ? () => setOpenAlert(true) : null, className: classes.username }, { children: notificationObject.user.username })), ' ', intl.formatMessage(messages.contributionFollow, {
                            b: (...chunks) => _jsx("strong", { children: chunks })
                        })] })), secondary: _jsxs(React.Fragment, { children: [template === SCNotificationObjectTemplateType.SNIPPET && (_jsx(_Fragment, { children: _jsx(Link, Object.assign({ to: scRoutingContext.url(SCRoutes[`${notificationObject[contributionType]['type'].toUpperCase()}_ROUTE_NAME`], getRouteData(notificationObject[contributionType])) }, { children: _jsx(Typography, Object.assign({ variant: "body2", className: classes.contributionText }, { children: getContributionSnippet(notificationObject[contributionType]) })) })) })), (template === SCNotificationObjectTemplateType.DETAIL || template === SCNotificationObjectTemplateType.SNIPPET) && (_jsx("div", { children: _jsx(DateTimeAgo, { date: notificationObject.active_at, className: classes.activeAt }) }))] }), footer: template === SCNotificationObjectTemplateType.TOAST && (_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", component: 'div' }, { children: _jsx(Link, Object.assign({ to: scRoutingContext.url(SCRoutes[`${notificationObject[contributionType]['type'].toUpperCase()}_ROUTE_NAME`], getRouteData(notificationObject[contributionType])) }, { children: _jsx(FormattedMessage, { id: "ui.userToastNotifications.viewContribution", defaultMessage: 'ui.userToastNotifications.viewContribution' }) })) }))] }))) }, rest)), openAlert && _jsx(UserDeletedSnackBar, { open: openAlert, handleClose: () => setOpenAlert(false) })] }));
}
