"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 use_deep_compare_effect_1 = require("use-deep-compare-effect");
const react_core_1 = require("@selfcommunity/react-core");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const system_1 = require("@mui/system");
const Bullet_1 = tslib_1.__importDefault(require("../../shared/Bullet"));
const PREFIX = 'SCUserCounters';
const classes = {
    root: `${PREFIX}-root`,
    button: `${PREFIX}-button`
};
const Root = (0, styles_1.styled)(material_1.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
 */
function UserCounters(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    const { className = null, userId = null, user = null } = props, rest = tslib_1.__rest(props, ["className", "userId", "user"]);
    // CONTEXT
    const scRoutingContext = (0, react_core_1.useSCRouting)();
    const scPreferencesContext = (0, react_1.useContext)(react_core_1.SCPreferencesContext);
    const followEnabled = react_core_1.SCPreferences.CONFIGURATIONS_FOLLOW_ENABLED in scPreferencesContext.preferences &&
        scPreferencesContext.preferences[react_core_1.SCPreferences.CONFIGURATIONS_FOLLOW_ENABLED].value;
    const scUserContext = (0, react_core_1.useSCUser)();
    // HOOKS
    const { scUser, refresh: refreshScUser, refreshing: refreshingScUser } = (0, react_core_1.useSCFetchUser)({ id: userId, user });
    const isMe = (0, react_1.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.
     */
    (0, use_deep_compare_effect_1.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
     */
    (0, use_deep_compare_effect_1.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 ((0, jsx_runtime_1.jsxs)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { children: [followEnabled ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ className: classes.button, variant: "text", component: react_core_1.Link, to: scRoutingContext.url(react_core_1.SCRoutes.USER_PROFILE_FOLLOWINGS_ROUTE_NAME, scUser) }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.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) => (0, jsx_runtime_1.jsx)("strong", { children: chunks })
                            } }) })), (0, jsx_runtime_1.jsx)(Bullet_1.default, {}), (0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ className: classes.button, variant: "text", component: react_core_1.Link, to: scRoutingContext.url(react_core_1.SCRoutes.USER_PROFILE_FOLLOWERS_ROUTE_NAME, scUser) }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.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) => (0, jsx_runtime_1.jsx)("strong", { children: chunks })
                            } }) }))] })) : ((0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ className: classes.button, variant: "text", component: react_core_1.Link, to: scRoutingContext.url(react_core_1.SCRoutes.USER_PROFILE_CONNECTIONS_ROUTE_NAME, scUser) }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.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) => (0, jsx_runtime_1.jsx)("strong", { children: chunks })
                    } }) }))), (0, jsx_runtime_1.jsx)(Bullet_1.default, {}), (0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ className: classes.button, variant: "text", component: react_core_1.Link, to: scRoutingContext.url(react_core_1.SCRoutes.USER_PROFILE_CATEGORIES_ROUTE_NAME, scUser) }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.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) => (0, jsx_runtime_1.jsx)("strong", { children: chunks })
                    } }) }))] })));
}
exports.default = UserCounters;
