"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importStar(require("react"));
const styles_1 = require("@mui/material/styles");
const material_1 = require("@mui/material");
const react_core_1 = require("@selfcommunity/react-core");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const system_1 = require("@mui/system");
const react_intl_1 = require("react-intl");
const Composer_1 = tslib_1.__importDefault(require("../Composer"));
const notistack_1 = require("notistack");
const contribution_1 = require("../../utils/contribution");
const PREFIX = 'SCComposerIconButton';
const classes = {
    root: `${PREFIX}-root`
};
const Root = (0, styles_1.styled)(material_1.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
 */
exports.default = react_1.default.forwardRef(function ComposerIconButton(inProps, ref) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    const { className = null, ComposerProps = {}, onClick } = props, rest = tslib_1.__rest(props, ["className", "ComposerProps", "onClick"]);
    // STATE
    const [open, setOpen] = (0, react_1.useState)(false);
    // CONTEXT
    const scUserContext = (0, react_core_1.useSCUser)();
    const scContext = (0, react_core_1.useSCContext)();
    const scRoutingContext = (0, react_core_1.useSCRouting)();
    const { enqueueSnackbar } = (0, notistack_1.useSnackbar)();
    // HANDLERS
    const handleClick = (0, react_1.useCallback)((event) => {
        if (scUserContext.user) {
            if (react_core_1.UserUtils.isBlocked(scUserContext.user)) {
                enqueueSnackbar((0, jsx_runtime_1.jsx)(react_intl_1.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 = (0, react_1.useCallback)(() => {
        setOpen(false);
    }, []);
    const handleSuccess = (0, react_1.useMemo)(() => (feedObject) => {
        setOpen(false);
        enqueueSnackbar((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.composerIconButton.composer.success", defaultMessage: "ui.composerIconButton.composer.success" }), {
            action: (snackbarId) => ((0, jsx_runtime_1.jsx)(react_core_1.Link, Object.assign({ to: scRoutingContext.url(react_core_1.SCRoutes[`${feedObject.type.toUpperCase()}_ROUTE_NAME`], (0, contribution_1.getRouteData)(feedObject)) }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.composerIconButton.composer.viewContribute", defaultMessage: "ui.composerIconButton.composer.viewContribute" }) }))),
            variant: 'success',
            autoHideDuration: 7000
        });
    }, []);
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { onClick: handleClick, ref: ref }, { children: (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "add_circle_outline" }) })), (0, jsx_runtime_1.jsx)(Composer_1.default, Object.assign({ open: open, fullWidth: true, onClose: handleClose, onSuccess: handleSuccess }, ComposerProps))] }));
});
