"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const lab_1 = require("@mui/lab");
const styles_1 = require("@mui/material/styles");
const system_1 = require("@mui/system");
const api_services_1 = require("@selfcommunity/api-services");
const react_core_1 = require("@selfcommunity/react-core");
const utils_1 = require("@selfcommunity/utils");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const react_1 = require("react");
const react_intl_1 = require("react-intl");
const Errors_1 = require("../../constants/Errors");
const ConfirmDialog_1 = tslib_1.__importDefault(require("../../shared/ConfirmDialog/ConfirmDialog"));
const PREFIX = 'SCAcceptRequestUserEventButton';
const classes = {
    root: `${PREFIX}-root`
};
const AcceptButton = (0, styles_1.styled)(lab_1.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
 */
function AcceptRequestUserEventButton(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    const { className, eventId, event, userId, user, handleConfirm = null } = props, rest = tslib_1.__rest(props, ["className", "eventId", "event", "userId", "user", "handleConfirm"]);
    // STATE
    const [loading, setLoading] = (0, react_1.useState)(false);
    const [openDialog, setOpenDialog] = (0, react_1.useState)(false);
    // HOOKS
    const { scEvent } = (0, react_core_1.useSCFetchEvent)({ id: eventId, event });
    const { scUser } = (0, react_core_1.useSCFetchUser)({ id: userId, user });
    const handleConfirmAction = (0, react_1.useCallback)(() => {
        setLoading(true);
        api_services_1.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);
            utils_1.Logger.error(Errors_1.SCOPE_SC_UI, error);
        });
    }, [scEvent, scUser]);
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(AcceptButton, Object.assign({ size: "small", variant: "outlined", onClick: () => setOpenDialog(true), loading: loading, className: (0, classnames_1.default)(classes.root, className) }, rest, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.acceptRequestUserEventButton.accept", id: "ui.acceptRequestUserEventButton.accept" }) })), openDialog && ((0, jsx_runtime_1.jsx)(ConfirmDialog_1.default, { open: openDialog, title: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.acceptRequestUserEventButton.dialog.title", defaultMessage: "ui.acceptRequestUserEventButton.dialog.title" }), content: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.acceptRequestUserEventButton.dialog.msg", defaultMessage: "ui.acceptRequestUserEventButton.dialog.msg" }), btnConfirm: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.acceptRequestUserEventButton.dialog.confirm", defaultMessage: "ui.acceptRequestUserEventButton.dialog.confirm" }), isUpdating: loading, onConfirm: handleConfirmAction, onClose: () => setOpenDialog(false) }))] }));
}
exports.default = AcceptRequestUserEventButton;
