import { __rest } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { useEffect, useState } from 'react';
import { styled } from '@mui/material/styles';
import { SCOPE_SC_UI } from '../../constants/Errors';
import { LoadingButton } from '@mui/lab';
import { FormattedMessage } from 'react-intl';
import classNames from 'classnames';
import { CacheStrategies, Logger } from '@selfcommunity/utils';
import { useSCContext, useSCFetchIncubator, useSCUser } from '@selfcommunity/react-core';
import { useThemeProps } from '@mui/system';
const PREFIX = 'SCIncubatorSubscribeButton';
const classes = {
    root: `${PREFIX}-root`
};
const Root = styled(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
 */
export default function IncubatorSubscribeButton(inProps) {
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { className, incubatorId, incubator, onSubscribe } = props, rest = __rest(props, ["className", "incubatorId", "incubator", "onSubscribe"]);
    // CONTEXT
    const scContext = useSCContext();
    const scUserContext = useSCUser();
    const scSubscribedIncubatorsManager = scUserContext.managers.incubators;
    const authUserId = scUserContext.user ? scUserContext.user.id : null;
    // STATE
    const { scIncubator, setSCIncubator } = useSCFetchIncubator({
        id: incubatorId,
        incubator,
        cacheStrategy: authUserId ? CacheStrategies.CACHE_FIRST : CacheStrategies.STALE_WHILE_REVALIDATE
    });
    const [subscribed, setSubscribed] = useState(null);
    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) => {
            Logger.error(SCOPE_SC_UI, e);
        });
    };
    const handleSubscribeAction = () => {
        if (!scUserContext.user) {
            scContext.settings.handleAnonymousAction();
        }
        else {
            subscribe();
        }
    };
    return (_jsx(Root, Object.assign({ size: "small", variant: subscribed ? 'contained' : 'outlined', onClick: handleSubscribeAction, loading: scUserContext.user ? subscribed === null || scSubscribedIncubatorsManager.isLoading(scIncubator) : null, className: classNames(classes.root, className) }, rest, { children: subscribed && scUserContext.user ? (_jsx(FormattedMessage, { defaultMessage: "ui.incubator.subscribeButton.button.subscribed", id: "ui.incubator.subscribeButton.button.subscribed" })) : (_jsx(FormattedMessage, { defaultMessage: "ui.incubator.subscribeButton.button.subscribe", id: "ui.incubator.subscribeButton.button.subscribe" })) })));
}
