import { __rest } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useState } from 'react';
import { styled } from '@mui/material/styles';
import { Avatar, Stack, Typography } from '@mui/material';
import { SCContributionType } from '@selfcommunity/types';
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 Bullet from '../../../shared/Bullet';
import VoteButton from '../../VoteButton';
import UserDeletedSnackBar from '../../../shared/UserDeletedSnackBar';
import UserAvatar from '../../../shared/UserAvatar';
import { PREFIX } from '../constants';
const messages = defineMessages({
    postOrStatus: {
        id: 'ui.notification.contribution.newPostOrStatus',
        defaultMessage: 'ui.notification.contribution.newPostOrStatus'
    },
    discussion: {
        id: 'ui.notification.contribution.discussion',
        defaultMessage: 'ui.notification.contribution.discussion'
    },
    postOrStatusSnippet: {
        id: 'ui.notification.contribution.snippet.newPostOrStatus',
        defaultMessage: 'ui.notification.contribution.snippet.newPostOrStatus'
    },
    discussionSnippet: {
        id: 'ui.notification.contribution.snippet.discussion',
        defaultMessage: 'ui.notification.contribution.snippet.discussion'
    }
});
const classes = {
    root: `${PREFIX}-contribution-root`,
    avatar: `${PREFIX}-avatar`,
    username: `${PREFIX}-username`,
    voteButton: `${PREFIX}-vote-button`,
    contributionText: `${PREFIX}-contribution-text`,
    activeAt: `${PREFIX}-active-at`,
    bullet: `${PREFIX}-bullet`
};
const Root = styled(NotificationItem, {
    name: PREFIX,
    slot: 'ContributionRoot'
})(() => ({}));
/**
 * This component render the content of the notification of type follow (contribution)
 * @constructor
 * @param props
 */
export default function ContributionNotification(props) {
    // PROPS
    const { notificationObject, onVote, id = `n_${props.notificationObject['sid']}`, className, template = SCNotificationObjectTemplateType.DETAIL } = props, rest = __rest(props, ["notificationObject", "onVote", "id", "className", "template"]);
    // ROUTING
    const scRoutingContext = useSCRouting();
    // CONST
    const contributionType = getContributionType(notificationObject);
    // INTL
    const intl = useIntl();
    // STATE
    const [openAlert, setOpenAlert] = useState(false);
    // HANDLERS
    const handleVote = (contribution) => {
        return onVote && onVote(contribution);
    };
    /**
     * 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[contributionType].author.deleted && {
                    to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject[contributionType].author)
                }), { onClick: notificationObject[contributionType].author.deleted ? () => setOpenAlert(true) : null }, { children: _jsx(UserAvatar, Object.assign({ hide: !notificationObject[contributionType].author.community_badge, smaller: true }, { children: _jsx(Avatar, { alt: notificationObject[contributionType].author.username, variant: "circular", src: notificationObject[contributionType].author.avatar, classes: { root: classes.avatar } }) })) })), primary: _jsxs(_Fragment, { children: [_jsx(Link, Object.assign({}, (!notificationObject[contributionType].author.deleted && {
                            to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject[contributionType].author)
                        }), { onClick: notificationObject[contributionType].author.deleted ? () => setOpenAlert(true) : null, className: classes.username }, { children: notificationObject[contributionType].author.username })), ' ', template === SCNotificationObjectTemplateType.SNIPPET ? (_jsx(_Fragment, { children: notificationObject[contributionType]['type'] === SCContributionType.POST ||
                                notificationObject[contributionType]['type'] === SCContributionType.STATUS
                                ? intl.formatMessage(messages.postOrStatusSnippet, { contribution: notificationObject[contributionType]['type'] })
                                : intl.formatMessage(messages.discussionSnippet) })) : (_jsx(_Fragment, { children: notificationObject[contributionType]['type'] === SCContributionType.POST ||
                                notificationObject[contributionType]['type'] === SCContributionType.STATUS
                                ? intl.formatMessage(messages.postOrStatus, { contribution: notificationObject[contributionType]['type'] })
                                : intl.formatMessage(messages.discussion) }))] }), secondary: _jsxs(React.Fragment, { children: [template === SCNotificationObjectTemplateType.SNIPPET && (_jsx(Link, Object.assign({ to: scRoutingContext.url(SCRoutes[`${contributionType.toUpperCase()}_ROUTE_NAME`], getRouteData(notificationObject[contributionType])) }, { children: _jsx(Typography, Object.assign({ variant: "body2", className: classes.contributionText, component: 'div' }, { children: getContributionSnippet(notificationObject[contributionType]) })) }))), template === SCNotificationObjectTemplateType.DETAIL && (_jsxs(Stack, Object.assign({ direction: "row", justifyContent: "flex-start", alignItems: "center", spacing: 1 }, { children: [_jsx(DateTimeAgo, { date: notificationObject.active_at, className: classes.activeAt }), _jsx(Bullet, { className: classes.bullet }), _jsx(VoteButton, { className: classes.voteButton, variant: "text", size: "small", contributionId: notificationObject[contributionType].id, contributionType: contributionType, contribution: notificationObject[contributionType], onVote: handleVote })] }))), template === SCNotificationObjectTemplateType.SNIPPET && (_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) })] }));
}
