import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useState } from 'react';
import { styled } from '@mui/material/styles';
import { SwipeableDrawer, MenuItem, ListItemIcon, IconButton, Menu, useTheme, useMediaQuery, List, ListItem } from '@mui/material';
import { FormattedMessage } from 'react-intl';
import Icon from '@mui/material/Icon';
import classNames from 'classnames';
import { useThemeProps } from '@mui/system';
import { Link, SCRoutes, useSCRouting } from '@selfcommunity/react-core';
import ConfirmDialog from '../../shared/ConfirmDialog/ConfirmDialog';
import { PrivateMessageService } from '@selfcommunity/api-services';
import PubSub from 'pubsub-js';
const PREFIX = 'SCPrivateMessageSettingsIconButton';
const classes = {
    root: `${PREFIX}-root`,
    drawerRoot: `${PREFIX}-drawer-root`,
    menuRoot: `${PREFIX}-menu-root`,
    paper: `${PREFIX}-paper`,
    item: `${PREFIX}-item`
};
const Root = styled(IconButton, {
    name: PREFIX,
    slot: 'Root'
})(() => ({}));
const SwipeableDrawerRoot = styled(SwipeableDrawer, {
    name: PREFIX,
    slot: 'DrawerRoot'
})(() => ({}));
const MenuRoot = styled(Menu, {
    name: PREFIX,
    slot: 'MenuRoot'
})(() => ({}));
/**
 * > API documentation for the Community-JS PrivateMessageSettingsIconButton component. Learn about the available props and the CSS API.

 #### Import

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

 #### Component Name

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


 #### CSS

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

 * @param inProps
 */
export default function PrivateMessageSettingsIconButton(inProps) {
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { className = null, onMenuItemDeleteClick, user, group, onItemDeleteConfirm, threadToDelete } = props, rest = __rest(props, ["className", "onMenuItemDeleteClick", "user", "group", "onItemDeleteConfirm", "threadToDelete"]);
    // STATE
    const [anchorEl, setAnchorEl] = useState(null);
    const [openConfirmDialog, setOpenConfirmDialog] = useState(false);
    // HOOKS
    const theme = useTheme();
    const isMobile = useMediaQuery(theme.breakpoints.down('md'));
    const scRoutingContext = useSCRouting();
    // HANDLERS
    const handleOpen = (event) => {
        setAnchorEl(event.currentTarget);
    };
    const handleClose = () => {
        setAnchorEl(null);
    };
    const handleOpenDialog = () => {
        setOpenConfirmDialog(true);
        setAnchorEl(null);
    };
    const handleCloseDialog = () => {
        setOpenConfirmDialog(false);
        setAnchorEl(null);
        onItemDeleteConfirm && onItemDeleteConfirm();
    };
    /**
     * Handles thread deletion
     */
    function handleDeleteThread() {
        const params = group ? { group: threadToDelete } : { user: threadToDelete };
        PrivateMessageService.deleteAThread(params)
            .then(() => {
            if (group) {
                PubSub.publish('snippetsChannelDeleteGroup', threadToDelete);
            }
            else {
                PubSub.publish('snippetsChannelDelete', threadToDelete);
            }
            handleCloseDialog();
        })
            .catch((error) => {
            setOpenConfirmDialog(false);
            console.log(error);
        });
    }
    /**
     *
     */
    const renderList = () => {
        if (isMobile) {
            return [
                user && !group && (_jsxs(ListItem, Object.assign({ className: classes.item, component: Link, to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, user) }, { children: [_jsx(ListItemIcon, { children: _jsx(Icon, { children: "people_alt" }) }), _jsx(FormattedMessage, { id: "ui.privateMessageSettingsIconButton.item.profile", defaultMessage: "ui.privateMessageSettingsIconButton.item.profile" })] }), "profile")),
                group && !user && (_jsxs(ListItem, Object.assign({ className: classes.item, component: Link, to: scRoutingContext.url(SCRoutes.GROUP_ROUTE_NAME, group) }, { children: [_jsx(ListItemIcon, { children: _jsx(Icon, { children: "groups" }) }), _jsx(FormattedMessage, { id: "ui.privateMessageSettingsIconButton.item.group", defaultMessage: "ui.privateMessageSettingsIconButton.item.group" })] }), "group")),
                _jsxs(ListItem, Object.assign({ className: classes.item, onClick: threadToDelete ? handleOpenDialog : onMenuItemDeleteClick }, { children: [_jsx(ListItemIcon, { children: _jsx(Icon, { children: "delete" }) }), _jsx(FormattedMessage, { id: "ui.privateMessageSettingsIconButton.item.delete", defaultMessage: "ui.privateMessageSettingsIconButton.item.delete" })] }), "delete")
            ];
        }
        else {
            return [
                user && !group && (_jsxs(MenuItem, Object.assign({ className: classes.item, component: Link, to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, user), onClick: handleCloseDialog }, { children: [_jsx(ListItemIcon, { children: _jsx(Icon, { children: "people_alt" }) }), _jsx(FormattedMessage, { id: "ui.privateMessageSettingsIconButton.item.profile", defaultMessage: "ui.privateMessageSettingsIconButton.item.profile" })] }), "profile")),
                group && !user && (_jsxs(MenuItem, Object.assign({ className: classes.item, component: Link, to: scRoutingContext.url(SCRoutes.GROUP_ROUTE_NAME, group), onClick: handleCloseDialog }, { children: [_jsx(ListItemIcon, { children: _jsx(Icon, { children: "groups" }) }), _jsx(FormattedMessage, { id: "ui.privateMessageSettingsIconButton.item.group", defaultMessage: "ui.privateMessageSettingsIconButton.item.group" })] }), "group")),
                _jsxs(MenuItem, Object.assign({ className: classes.item, onClick: threadToDelete ? handleOpenDialog : onMenuItemDeleteClick }, { children: [_jsx(ListItemIcon, { children: _jsx(Icon, { children: "delete" }) }), _jsx(FormattedMessage, { id: "ui.privateMessageSettingsIconButton.item.delete", defaultMessage: "ui.privateMessageSettingsIconButton.item.delete" })] }), "delete")
            ];
        }
    };
    return (_jsxs(_Fragment, { children: [_jsx(Root, Object.assign({ className: classNames(classes.root, className) }, rest, { onClick: handleOpen }, { children: _jsx(Icon, { children: "more_vert" }) })), isMobile ? (_jsx(SwipeableDrawerRoot, Object.assign({ className: classes.drawerRoot, anchor: "bottom", open: Boolean(anchorEl), onClose: handleClose, onOpen: handleOpen, PaperProps: { className: classes.paper }, disableSwipeToOpen: true }, { children: _jsx(List, { children: renderList() }) }))) : (_jsx(MenuRoot, Object.assign({ className: classes.menuRoot, anchorEl: anchorEl, open: Boolean(anchorEl), onClose: handleClose, PaperProps: { className: classes.paper } }, { children: renderList() }))), openConfirmDialog && (_jsx(ConfirmDialog, { open: openConfirmDialog, title: _jsx(FormattedMessage, { id: "ui.privateMessageSettingsIconButton.dialog.msg", defaultMessage: "ui.privateMessageSettingsIconButton.dialog.msg" }), btnConfirm: _jsx(FormattedMessage, { id: "ui.privateMessageSettingsIconButton.dialog.confirm", defaultMessage: "ui.privateMessageSettingsIconButton.dialog.confirm" }), onConfirm: handleDeleteThread, onClose: handleCloseDialog }))] }));
}
