import { __rest } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo } from 'react';
import { styled } from '@mui/material/styles';
import { Box, Divider, Typography } from '@mui/material';
import { FormattedMessage } from 'react-intl';
import { useSCFetchUser, useSCFetchUserBlockedBy, useSCUser } from '@selfcommunity/react-core';
import classNames from 'classnames';
import { useThemeProps } from '@mui/system';
import { LoadingButton } from '@mui/lab';
import { useSnackbar } from 'notistack';
import { SCOPE_SC_UI } from '../../constants/Errors';
import { Logger } from '@selfcommunity/utils';
const PREFIX = 'SCUserProfileBlocked';
const classes = {
    root: `${PREFIX}-root`,
    info: `${PREFIX}-info`,
    button: `${PREFIX}-button`
};
const Root = styled(Box, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(() => ({
    textAlign: 'center'
}));
/**
 * > API documentation for the Community-JS User Profile Blocked component. Learn about the available props and the CSS API.

 #### Import

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

 #### Component Name

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


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCUserProfileBlocked-root|Styles applied to the root element.|
 |info|.SCUserProfileBlocked-info|Styles applied to info text element.|
 |button|.SCUserProfileBlocked-button|Styles applied to the unblock button element.|

 * @param inProps
 */
export default function UserProfileBlocked(inProps) {
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { className = null, userId = null, user = null, blockedByUser = null, syncBlockedByUserStatus = false } = props, rest = __rest(props, ["className", "userId", "user", "blockedByUser", "syncBlockedByUserStatus"]);
    // CONTEXT
    const scUserContext = useSCUser();
    const { enqueueSnackbar } = useSnackbar();
    // HOOKS
    const { scUser } = useSCFetchUser({ id: userId, user });
    const { blockedBy, loading: loadingBlockedBy } = useSCFetchUserBlockedBy({ user: scUser, blockedByUser, sync: syncBlockedByUserStatus });
    // CONST
    const isMe = useMemo(() => scUserContext.user && (scUser === null || scUser === void 0 ? void 0 : scUser.id) === scUserContext.user.id, [scUserContext.user, scUser]);
    /**
     * Handle block action
     */
    const handleBlock = useMemo(() => () => {
        scUserContext.managers.blockedUsers.block(scUser).catch((error) => {
            Logger.error(SCOPE_SC_UI, error);
            enqueueSnackbar(_jsx(FormattedMessage, { id: "ui.common.error.action", defaultMessage: "ui.common.error.action" }), {
                variant: 'error',
                autoHideDuration: 3000
            });
        });
    }, [scUserContext.managers.blockedUsers, scUser]);
    if (!scUser || !scUserContext.user || loadingBlockedBy) {
        return null;
    }
    return (_jsxs(Root, Object.assign({ className: classNames(classes.root, className) }, rest, { children: [_jsx(Divider, {}), !isMe && blockedBy && (_jsx(Typography, Object.assign({ variant: "body1", className: classes.info }, { children: _jsx(FormattedMessage, { id: "ui.userProfileBlocked.blockedUserBy", defaultMessage: "ui.userProfileBlocked.blockedUserBy" }) }))), !isMe && scUserContext.managers.blockedUsers.isBlocked(scUser) && (_jsxs(_Fragment, { children: [_jsx(Typography, Object.assign({ variant: "body1", className: classes.info }, { children: _jsx(FormattedMessage, { id: "ui.userProfileBlocked.blockedUser", defaultMessage: "ui.userProfileBlocked.blockedUser" }) })), _jsx(LoadingButton, Object.assign({ variant: "contained", className: classes.button, loading: scUserContext.managers.blockedUsers.loading, disabled: scUserContext.managers.blockedUsers.loading, onClick: handleBlock }, { children: _jsx(FormattedMessage, { id: "ui.userProfileBlocked.unBlockUser", defaultMessage: "ui.userProfileBlocked.unBlockUser" }) }))] }))] })));
}
