import { __rest } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { useEffect, useState } from 'react';
import { styled } from '@mui/material/styles';
import { CacheStrategies, Logger } from '@selfcommunity/utils';
import { useSCContext, useSCFetchCategory, useSCUser } from '@selfcommunity/react-core';
import { SCOPE_SC_UI } from '../../constants/Errors';
import { LoadingButton } from '@mui/lab';
import { FormattedMessage } from 'react-intl';
import classNames from 'classnames';
import { useThemeProps } from '@mui/system';
import { SCCategoryAutoFollowType } from '@selfcommunity/types';
const PREFIX = 'SCCategoryFollowButton';
const classes = {
    root: `${PREFIX}-root`
};
const FollowButton = styled(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
 */
export default function CategoryFollowButton(inProps) {
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { className, categoryId, category, onFollow } = props, rest = __rest(props, ["className", "categoryId", "category", "onFollow"]);
    // CONTEXT
    const scContext = useSCContext();
    const scUserContext = useSCUser();
    const scCategoriesManager = scUserContext.managers.categories;
    // CONST
    const authUserId = scUserContext.user ? scUserContext.user.id : null;
    const { scCategory, setSCCategory } = useSCFetchCategory({
        id: categoryId,
        category,
        cacheStrategy: authUserId ? CacheStrategies.CACHE_FIRST : CacheStrategies.STALE_WHILE_REVALIDATE
    });
    const [followed, setFollowed] = useState(null);
    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) => {
            Logger.error(SCOPE_SC_UI, e);
        });
    };
    const handleFollowAction = () => {
        if (!scUserContext.user) {
            scContext.settings.handleAnonymousAction();
        }
        else {
            followCategory();
        }
    };
    if (!scCategory || (scCategory && followed && scCategory.auto_follow === SCCategoryAutoFollowType.FORCED)) {
        return null;
    }
    return (_jsx(FollowButton, Object.assign({ size: "small", variant: "outlined", onClick: handleFollowAction, loading: scUserContext.user ? followed === null || scCategoriesManager.isLoading(scCategory) : null, className: classNames(classes.root, className) }, rest, { children: followed && scUserContext.user ? (_jsx(FormattedMessage, { defaultMessage: "ui.categoryFollowButton.unfollow", id: "ui.categoryFollowButton.unfollow" })) : (_jsx(FormattedMessage, { defaultMessage: "ui.categoryFollowButton.follow", id: "ui.categoryFollowButton.follow" })) })));
}
