"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 PREFIX = 'SCInviteUserEventButton';
const classes = {
    root: `${PREFIX}-root`
};
const InviteButton = (0, styles_1.styled)(lab_1.LoadingButton, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (_props, styles) => styles.root
})(() => ({}));
/**
 * > API documentation for the Community-JS Invite Event Button component. Learn about the available props and the CSS API.

 #### Import

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

 #### Component Name

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


 #### CSS

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

 * @param inProps
 */
function InviteUserEventButton(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    const { className, eventId, event, userId, user, handleInvitations } = props, rest = tslib_1.__rest(props, ["className", "eventId", "event", "userId", "user", "handleInvitations"]);
    // STATE
    const [invited, setInvited] = (0, react_1.useState)(true);
    //HOOKS
    const { scEvent } = (0, react_core_1.useSCFetchEvent)({ id: eventId, event });
    const { scUser } = (0, react_core_1.useSCFetchUser)({ id: userId, user });
    const handleInviteAction = (0, react_1.useCallback)(() => {
        setInvited(null);
        if (invited) {
            api_services_1.EventService.removeInviteEvent(scEvent.id, { users: [scUser.id] })
                .then(() => {
                setInvited(false);
                handleInvitations === null || handleInvitations === void 0 ? void 0 : handleInvitations(true);
            })
                .catch((_error) => {
                utils_1.Logger.error(Errors_1.SCOPE_SC_UI, _error);
            });
        }
        else {
            api_services_1.EventService.inviteOrAcceptEventRequest(scEvent.id, { users: [scUser.id] })
                .then(() => {
                setInvited(true);
                handleInvitations === null || handleInvitations === void 0 ? void 0 : handleInvitations(false);
            })
                .catch((_error) => {
                utils_1.Logger.error(Errors_1.SCOPE_SC_UI, _error);
            });
        }
    }, [scEvent, scUser, invited]);
    return ((0, jsx_runtime_1.jsx)(InviteButton, Object.assign({ size: "small", variant: "outlined", onClick: handleInviteAction, loading: invited === null, className: (0, classnames_1.default)(classes.root, className) }, rest, { children: invited ? ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.inviteUserEventButton.remove", id: "ui.inviteUserEventButton.remove" })) : ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.inviteUserEventButton.invite", id: "ui.inviteUserEventButton.invite" })) })));
}
exports.default = InviteUserEventButton;
