import { __rest } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useCallback, useMemo, useState } from 'react';
import { styled } from '@mui/material/styles';
import { Icon, IconButton } from '@mui/material';
import { Link, SCRoutes, UserUtils, useSCContext, useSCRouting, useSCUser } from '@selfcommunity/react-core';
import classNames from 'classnames';
import { useThemeProps } from '@mui/system';
import { FormattedMessage } from 'react-intl';
import Composer from '../Composer';
import { useSnackbar } from 'notistack';
import { getRouteData } from '../../utils/contribution';
const PREFIX = 'SCComposerIconButton';
const classes = {
    root: `${PREFIX}-root`
};
const Root = styled(IconButton, {
    name: PREFIX,
    slot: 'Root'
})(() => ({}));
/**
 * > API documentation for the Community-JS Composer Icon Button component. Learn about the available props and the CSS API.

 #### Import

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

 #### Component Name

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


 #### CSS

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

 * @param inProps
 */
export default React.forwardRef(function ComposerIconButton(inProps, ref) {
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { className = null, ComposerProps = {}, onClick } = props, rest = __rest(props, ["className", "ComposerProps", "onClick"]);
    // STATE
    const [open, setOpen] = useState(false);
    // CONTEXT
    const scUserContext = useSCUser();
    const scContext = useSCContext();
    const scRoutingContext = useSCRouting();
    const { enqueueSnackbar } = useSnackbar();
    // HANDLERS
    const handleClick = useCallback((event) => {
        if (scUserContext.user) {
            if (UserUtils.isBlocked(scUserContext.user)) {
                enqueueSnackbar(_jsx(FormattedMessage, { id: "ui.common.userBlocked", defaultMessage: "ui.common.userBlocked" }), {
                    variant: 'warning',
                    autoHideDuration: 3000
                });
            }
            else {
                setOpen(true);
            }
        }
        else {
            scContext.settings.handleAnonymousAction();
        }
        onClick && onClick(event);
    }, [onClick, enqueueSnackbar, scContext.settings, scUserContext.user]);
    const handleClose = useCallback(() => {
        setOpen(false);
    }, []);
    const handleSuccess = useMemo(() => (feedObject) => {
        setOpen(false);
        enqueueSnackbar(_jsx(FormattedMessage, { id: "ui.composerIconButton.composer.success", defaultMessage: "ui.composerIconButton.composer.success" }), {
            action: (snackbarId) => (_jsx(Link, Object.assign({ to: scRoutingContext.url(SCRoutes[`${feedObject.type.toUpperCase()}_ROUTE_NAME`], getRouteData(feedObject)) }, { children: _jsx(FormattedMessage, { id: "ui.composerIconButton.composer.viewContribute", defaultMessage: "ui.composerIconButton.composer.viewContribute" }) }))),
            variant: 'success',
            autoHideDuration: 7000
        });
    }, []);
    return (_jsxs(_Fragment, { children: [_jsx(Root, Object.assign({ className: classNames(classes.root, className) }, rest, { onClick: handleClick, ref: ref }, { children: _jsx(Icon, { children: "add_circle_outline" }) })), _jsx(Composer, Object.assign({ open: open, fullWidth: true, onClose: handleClose, onSuccess: handleSuccess }, ComposerProps))] }));
});
