import { __rest } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useContext, useMemo } from 'react';
import { styled } from '@mui/material/styles';
import { Box, Button } from '@mui/material';
import { FormattedMessage } from 'react-intl';
import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect';
import { Link, SCPreferences, SCPreferencesContext, SCRoutes, useSCFetchUser, useSCRouting, useSCUser } from '@selfcommunity/react-core';
import classNames from 'classnames';
import { useThemeProps } from '@mui/system';
import Bullet from '../../shared/Bullet';
const PREFIX = 'SCUserCounters';
const classes = {
    root: `${PREFIX}-root`,
    button: `${PREFIX}-button`
};
const Root = styled(Box, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({}));
/**
 * > API documentation for the Community-JS User Counters component. Learn about the available props and the CSS API.

 #### Import

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

 #### Component Name

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


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCUserCounters-root|Styles applied to the root element.|
 |field|.SCUserCounters-field|Styles applied to the field element.|

 * @param inProps
 */
export default function UserCounters(inProps) {
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { className = null, userId = null, user = null } = props, rest = __rest(props, ["className", "userId", "user"]);
    // CONTEXT
    const scRoutingContext = useSCRouting();
    const scPreferencesContext = useContext(SCPreferencesContext);
    const followEnabled = SCPreferences.CONFIGURATIONS_FOLLOW_ENABLED in scPreferencesContext.preferences &&
        scPreferencesContext.preferences[SCPreferences.CONFIGURATIONS_FOLLOW_ENABLED].value;
    const scUserContext = useSCUser();
    // HOOKS
    const { scUser, refresh: refreshScUser, refreshing: refreshingScUser } = useSCFetchUser({ id: userId, user });
    const isMe = useMemo(() => scUserContext.user && (scUser === null || scUser === void 0 ? void 0 : scUser.id) === scUserContext.user.id, [scUserContext.user, scUser]);
    // CONST
    const scUserId = scUser ? scUser.id : null;
    /**
     * Refresh counters: categories, followed, connections, etc.
     */
    useDeepCompareEffectNoCheck(() => {
        if (scUserId &&
            scUserContext.user &&
            !scUserContext.managers.followed.isLoading(scUser) &&
            !scUserContext.managers.connections.isLoading(scUser) &&
            !refreshingScUser) {
            // eslint-disable-next-line @typescript-eslint/no-empty-function
            refreshScUser().catch(() => { });
        }
    }, [scUserId, scUserContext.user, scUserContext.managers.connections.connections, scUserContext.managers.followed.followed.length]);
    /**
     * Refresh counters: categories
     */
    useDeepCompareEffectNoCheck(() => {
        // Refresh only if follow/unfollow a category and I'm watching my own user
        if (isMe) {
            // eslint-disable-next-line @typescript-eslint/no-empty-function
            refreshScUser().catch(() => { });
        }
    }, [isMe, scUserContext.managers.categories.categories.length]);
    if (scUserContext.user === undefined || !scUser) {
        return null;
    }
    return (_jsxs(Root, Object.assign({ className: classNames(classes.root, className) }, rest, { children: [followEnabled ? (_jsxs(_Fragment, { children: [_jsx(Button, Object.assign({ className: classes.button, variant: "text", component: Link, to: scRoutingContext.url(SCRoutes.USER_PROFILE_FOLLOWINGS_ROUTE_NAME, scUser) }, { children: _jsx(FormattedMessage, { id: "ui.userCounters.followings", defaultMessage: "ui.userCounters.followings", values: {
                                count: scUser === null || scUser === void 0 ? void 0 : scUser.followings_counter,
                                // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
                                // @ts-ignore
                                b: (chunks) => _jsx("strong", { children: chunks })
                            } }) })), _jsx(Bullet, {}), _jsx(Button, Object.assign({ className: classes.button, variant: "text", component: Link, to: scRoutingContext.url(SCRoutes.USER_PROFILE_FOLLOWERS_ROUTE_NAME, scUser) }, { children: _jsx(FormattedMessage, { id: "ui.userCounters.followers", defaultMessage: "ui.userCounters.followers", values: {
                                count: scUser === null || scUser === void 0 ? void 0 : scUser.followers_counter,
                                // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
                                // @ts-ignore
                                b: (chunks) => _jsx("strong", { children: chunks })
                            } }) }))] })) : (_jsx(Button, Object.assign({ className: classes.button, variant: "text", component: Link, to: scRoutingContext.url(SCRoutes.USER_PROFILE_CONNECTIONS_ROUTE_NAME, scUser) }, { children: _jsx(FormattedMessage, { id: "ui.userCounters.connections", defaultMessage: "ui.userCounters.connections", values: {
                        count: scUser === null || scUser === void 0 ? void 0 : scUser.connections_counter,
                        // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
                        // @ts-ignore
                        b: (chunks) => _jsx("strong", { children: chunks })
                    } }) }))), _jsx(Bullet, {}), _jsx(Button, Object.assign({ className: classes.button, variant: "text", component: Link, to: scRoutingContext.url(SCRoutes.USER_PROFILE_CATEGORIES_ROUTE_NAME, scUser) }, { children: _jsx(FormattedMessage, { id: "ui.userCounters.categories", defaultMessage: "ui.userCounters.categories", values: {
                        count: scUser === null || scUser === void 0 ? void 0 : scUser.categories_counter,
                        // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
                        // @ts-ignore
                        b: (chunks) => _jsx("strong", { children: chunks })
                    } }) }))] })));
}
