"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_intl_1 = require("react-intl");
const react_core_1 = require("@selfcommunity/react-core");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const system_1 = require("@mui/system");
const lab_1 = require("@mui/lab");
const notistack_1 = require("notistack");
const Errors_1 = require("../../constants/Errors");
const utils_1 = require("@selfcommunity/utils");
const PREFIX = 'SCUserProfileBlocked';
const classes = {
    root: `${PREFIX}-root`,
    info: `${PREFIX}-info`,
    button: `${PREFIX}-button`
};
const Root = (0, styles_1.styled)(material_1.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
 */
function UserProfileBlocked(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    const { className = null, userId = null, user = null, blockedByUser = null, syncBlockedByUserStatus = false } = props, rest = tslib_1.__rest(props, ["className", "userId", "user", "blockedByUser", "syncBlockedByUserStatus"]);
    // CONTEXT
    const scUserContext = (0, react_core_1.useSCUser)();
    const { enqueueSnackbar } = (0, notistack_1.useSnackbar)();
    // HOOKS
    const { scUser } = (0, react_core_1.useSCFetchUser)({ id: userId, user });
    const { blockedBy, loading: loadingBlockedBy } = (0, react_core_1.useSCFetchUserBlockedBy)({ user: scUser, blockedByUser, sync: syncBlockedByUserStatus });
    // CONST
    const isMe = (0, react_1.useMemo)(() => scUserContext.user && (scUser === null || scUser === void 0 ? void 0 : scUser.id) === scUserContext.user.id, [scUserContext.user, scUser]);
    /**
     * Handle block action
     */
    const handleBlock = (0, react_1.useMemo)(() => () => {
        scUserContext.managers.blockedUsers.block(scUser).catch((error) => {
            utils_1.Logger.error(Errors_1.SCOPE_SC_UI, error);
            enqueueSnackbar((0, jsx_runtime_1.jsx)(react_intl_1.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 ((0, jsx_runtime_1.jsxs)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { children: [(0, jsx_runtime_1.jsx)(material_1.Divider, {}), !isMe && blockedBy && ((0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "body1", className: classes.info }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.userProfileBlocked.blockedUserBy", defaultMessage: "ui.userProfileBlocked.blockedUserBy" }) }))), !isMe && scUserContext.managers.blockedUsers.isBlocked(scUser) && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "body1", className: classes.info }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.userProfileBlocked.blockedUser", defaultMessage: "ui.userProfileBlocked.blockedUser" }) })), (0, jsx_runtime_1.jsx)(lab_1.LoadingButton, Object.assign({ variant: "contained", className: classes.button, loading: scUserContext.managers.blockedUsers.loading, disabled: scUserContext.managers.blockedUsers.loading, onClick: handleBlock }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.userProfileBlocked.unBlockUser", defaultMessage: "ui.userProfileBlocked.unBlockUser" }) }))] }))] })));
}
exports.default = UserProfileBlocked;
