import { __rest } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { LoadingButton } from '@mui/lab';
import { styled } from '@mui/material/styles';
import { useThemeProps } from '@mui/system';
import { EventService } from '@selfcommunity/api-services';
import { useSCFetchEvent, useSCFetchUser } from '@selfcommunity/react-core';
import { Logger } from '@selfcommunity/utils';
import classNames from 'classnames';
import { useCallback, useState } from 'react';
import { FormattedMessage } from 'react-intl';
import { SCOPE_SC_UI } from '../../constants/Errors';
import ConfirmDialog from '../../shared/ConfirmDialog/ConfirmDialog';
const PREFIX = 'SCAcceptRequestUserEventButton';
const classes = {
    root: `${PREFIX}-root`
};
const AcceptButton = styled(LoadingButton, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (_props, styles) => styles.root
})(() => ({}));
/**
 * > API documentation for the Community-JS Accept Request User Event Button component. Learn about the available props and the CSS API.

 #### Import

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

 #### Component Name

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


 #### CSS

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

 * @param inProps
 */
export default function AcceptRequestUserEventButton(inProps) {
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { className, eventId, event, userId, user, handleConfirm = null } = props, rest = __rest(props, ["className", "eventId", "event", "userId", "user", "handleConfirm"]);
    // STATE
    const [loading, setLoading] = useState(false);
    const [openDialog, setOpenDialog] = useState(false);
    // HOOKS
    const { scEvent } = useSCFetchEvent({ id: eventId, event });
    const { scUser } = useSCFetchUser({ id: userId, user });
    const handleConfirmAction = useCallback(() => {
        setLoading(true);
        EventService.inviteOrAcceptEventRequest(scEvent.id, { users: [scUser.id] })
            .then(() => {
            handleConfirm === null || handleConfirm === void 0 ? void 0 : handleConfirm(scUser.id);
            setLoading(false);
            setOpenDialog(false);
        })
            .catch((error) => {
            handleConfirm === null || handleConfirm === void 0 ? void 0 : handleConfirm(null);
            Logger.error(SCOPE_SC_UI, error);
        });
    }, [scEvent, scUser]);
    return (_jsxs(_Fragment, { children: [_jsx(AcceptButton, Object.assign({ size: "small", variant: "outlined", onClick: () => setOpenDialog(true), loading: loading, className: classNames(classes.root, className) }, rest, { children: _jsx(FormattedMessage, { defaultMessage: "ui.acceptRequestUserEventButton.accept", id: "ui.acceptRequestUserEventButton.accept" }) })), openDialog && (_jsx(ConfirmDialog, { open: openDialog, title: _jsx(FormattedMessage, { id: "ui.acceptRequestUserEventButton.dialog.title", defaultMessage: "ui.acceptRequestUserEventButton.dialog.title" }), content: _jsx(FormattedMessage, { id: "ui.acceptRequestUserEventButton.dialog.msg", defaultMessage: "ui.acceptRequestUserEventButton.dialog.msg" }), btnConfirm: _jsx(FormattedMessage, { id: "ui.acceptRequestUserEventButton.dialog.confirm", defaultMessage: "ui.acceptRequestUserEventButton.dialog.confirm" }), isUpdating: loading, onConfirm: handleConfirmAction, onClose: () => setOpenDialog(false) }))] }));
}
