"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 utils_1 = require("@selfcommunity/utils");
const react_core_1 = require("@selfcommunity/react-core");
const Errors_1 = require("../../constants/Errors");
const lab_1 = require("@mui/lab");
const react_intl_1 = require("react-intl");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const system_1 = require("@mui/system");
const types_1 = require("@selfcommunity/types");
const PREFIX = 'SCCategoryFollowButton';
const classes = {
    root: `${PREFIX}-root`
};
const FollowButton = (0, styles_1.styled)(lab_1.LoadingButton, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({}));
/**
 * > API documentation for the Community-JS Follow Category Button component. Learn about the available props and the CSS API.

 #### Import

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

 #### Component Name

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


 #### CSS

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

 * @param inProps
 */
function CategoryFollowButton(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    const { className, categoryId, category, onFollow } = props, rest = tslib_1.__rest(props, ["className", "categoryId", "category", "onFollow"]);
    // CONTEXT
    const scContext = (0, react_core_1.useSCContext)();
    const scUserContext = (0, react_core_1.useSCUser)();
    const scCategoriesManager = scUserContext.managers.categories;
    // CONST
    const authUserId = scUserContext.user ? scUserContext.user.id : null;
    const { scCategory, setSCCategory } = (0, react_core_1.useSCFetchCategory)({
        id: categoryId,
        category,
        cacheStrategy: authUserId ? utils_1.CacheStrategies.CACHE_FIRST : utils_1.CacheStrategies.STALE_WHILE_REVALIDATE
    });
    const [followed, setFollowed] = (0, react_1.useState)(null);
    (0, react_1.useEffect)(() => {
        /**
         * Call scCategoriesManager.isFollowed inside an effect
         * to avoid warning rendering child during update parent state
         */
        if (authUserId) {
            setFollowed(scCategoriesManager.isFollowed(scCategory));
        }
    }, [authUserId, scCategoriesManager.isFollowed]);
    const followCategory = () => {
        scCategoriesManager
            .follow(scCategory)
            .then(() => {
            onFollow && onFollow(scCategory, !followed);
        })
            .catch((e) => {
            utils_1.Logger.error(Errors_1.SCOPE_SC_UI, e);
        });
    };
    const handleFollowAction = () => {
        if (!scUserContext.user) {
            scContext.settings.handleAnonymousAction();
        }
        else {
            followCategory();
        }
    };
    if (!scCategory || (scCategory && followed && scCategory.auto_follow === types_1.SCCategoryAutoFollowType.FORCED)) {
        return null;
    }
    return ((0, jsx_runtime_1.jsx)(FollowButton, Object.assign({ size: "small", variant: "outlined", onClick: handleFollowAction, loading: scUserContext.user ? followed === null || scCategoriesManager.isLoading(scCategory) : null, className: (0, classnames_1.default)(classes.root, className) }, rest, { children: followed && scUserContext.user ? ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.categoryFollowButton.unfollow", id: "ui.categoryFollowButton.unfollow" })) : ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.categoryFollowButton.follow", id: "ui.categoryFollowButton.follow" })) })));
}
exports.default = CategoryFollowButton;
