"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 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 utils_1 = require("@selfcommunity/utils");
const react_core_1 = require("@selfcommunity/react-core");
const system_1 = require("@mui/system");
const PREFIX = 'SCIncubatorSubscribeButton';
const classes = {
    root: `${PREFIX}-root`
};
const Root = (0, styles_1.styled)(lab_1.LoadingButton, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({}));
/**
 * > API documentation for the Community-JS Incubator Subscribe Button component. Learn about the available props and the CSS API.

 #### Import

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

 #### Component Name

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


 #### CSS

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

 * @param inProps
 */
function IncubatorSubscribeButton(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    const { className, incubatorId, incubator, onSubscribe } = props, rest = tslib_1.__rest(props, ["className", "incubatorId", "incubator", "onSubscribe"]);
    // CONTEXT
    const scContext = (0, react_core_1.useSCContext)();
    const scUserContext = (0, react_core_1.useSCUser)();
    const scSubscribedIncubatorsManager = scUserContext.managers.incubators;
    const authUserId = scUserContext.user ? scUserContext.user.id : null;
    // STATE
    const { scIncubator, setSCIncubator } = (0, react_core_1.useSCFetchIncubator)({
        id: incubatorId,
        incubator,
        cacheStrategy: authUserId ? utils_1.CacheStrategies.CACHE_FIRST : utils_1.CacheStrategies.STALE_WHILE_REVALIDATE
    });
    const [subscribed, setSubscribed] = (0, react_1.useState)(null);
    (0, react_1.useEffect)(() => {
        /**
         * Call scFollowedIncubatorsManager.isFollowed inside an effect
         * to avoid warning rendering child during update parent state
         */
        if (authUserId) {
            setSubscribed(scSubscribedIncubatorsManager.isSubscribed(scIncubator));
        }
    }, [authUserId, scSubscribedIncubatorsManager.isSubscribed]);
    const subscribe = () => {
        scSubscribedIncubatorsManager
            .subscribe(scIncubator)
            .then(() => {
            onSubscribe && onSubscribe(scIncubator, !subscribed);
        })
            .catch((e) => {
            utils_1.Logger.error(Errors_1.SCOPE_SC_UI, e);
        });
    };
    const handleSubscribeAction = () => {
        if (!scUserContext.user) {
            scContext.settings.handleAnonymousAction();
        }
        else {
            subscribe();
        }
    };
    return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ size: "small", variant: subscribed ? 'contained' : 'outlined', onClick: handleSubscribeAction, loading: scUserContext.user ? subscribed === null || scSubscribedIncubatorsManager.isLoading(scIncubator) : null, className: (0, classnames_1.default)(classes.root, className) }, rest, { children: subscribed && scUserContext.user ? ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.incubator.subscribeButton.button.subscribed", id: "ui.incubator.subscribeButton.button.subscribed" })) : ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.incubator.subscribeButton.button.subscribe", id: "ui.incubator.subscribeButton.button.subscribe" })) })));
}
exports.default = IncubatorSubscribeButton;
