"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const material_1 = require("@mui/material");
const styles_1 = require("@mui/material/styles");
const system_1 = require("@mui/system");
const react_core_1 = require("@selfcommunity/react-core");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const react_1 = tslib_1.__importStar(require("react"));
const react_intl_1 = require("react-intl");
const EventFormDialog_1 = tslib_1.__importDefault(require("../EventFormDialog"));
const types_1 = require("@selfcommunity/types");
const PREFIX = 'SCCreateEventButton';
const classes = {
    root: `${PREFIX}-root`
};
const Root = (0, styles_1.styled)(material_1.Button, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(() => ({}));
/**
 *> API documentation for the Community-JS Create Group Button component. Learn about the available props and the CSS API.
 *
 #### Import
 ```jsx
 import {CreateEventButton} from '@selfcommunity/react-ui';
 ```

 #### Component Name
 The name `SCCreateEventButton` can be used when providing style overrides in the theme.

 #### CSS

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

 * @param inProps
 */
function CreateEventButton(inProps) {
    var _a, _b;
    //PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    const { className, EventFormDialogComponentProps = {}, children } = props, rest = tslib_1.__rest(props, ["className", "EventFormDialogComponentProps", "children"]);
    // CONTEXT
    const scUserContext = (0, react_1.useContext)(react_core_1.SCUserContext);
    // STATE
    const [open, setOpen] = react_1.default.useState(false);
    // CONST
    const authUserId = scUserContext.user ? scUserContext.user.id : null;
    const { preferences, features } = (0, react_core_1.useSCPreferences)();
    const eventsEnabled = (0, react_1.useMemo)(() => preferences &&
        features &&
        features.includes(types_1.SCFeatureName.TAGGING) &&
        react_core_1.SCPreferences.CONFIGURATIONS_EVENTS_ENABLED in preferences &&
        preferences[react_core_1.SCPreferences.CONFIGURATIONS_EVENTS_ENABLED].value, [preferences, features]);
    const onlyStaffEnabled = (0, react_1.useMemo)(() => preferences[react_core_1.SCPreferences.CONFIGURATIONS_EVENTS_ONLY_STAFF_ENABLED].value, [preferences]);
    // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
    // @ts-ignore
    const canCreateEvent = (0, react_1.useMemo)(() => { var _a, _b; return (_b = (_a = scUserContext === null || scUserContext === void 0 ? void 0 : scUserContext.user) === null || _a === void 0 ? void 0 : _a.permission) === null || _b === void 0 ? void 0 : _b.create_event; }, [(_a = scUserContext === null || scUserContext === void 0 ? void 0 : scUserContext.user) === null || _a === void 0 ? void 0 : _a.permission]);
    /**
     * Handle click on button
     */
    const handleClick = () => {
        setOpen((o) => !o);
    };
    /**
     * If there's no authUserId, component is hidden.
     */
    if (!eventsEnabled || (!canCreateEvent && onlyStaffEnabled) || !authUserId) {
        return null;
    }
    /**
     * Renders root object
     */
    return ((0, jsx_runtime_1.jsxs)(react_1.default.Fragment, { children: [(0, jsx_runtime_1.jsx)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className), onClick: handleClick, variant: "contained", color: "secondary", startIcon: !((_b = EventFormDialogComponentProps.EventFormComponentProps) === null || _b === void 0 ? void 0 : _b.event) && (0, jsx_runtime_1.jsx)(material_1.Icon, Object.assign({ fontSize: "small" }, { children: "add" })) }, rest, { children: children !== null && children !== void 0 ? children : (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.createEventButton", defaultMessage: "ui.createEventButton" }) })), open && (0, jsx_runtime_1.jsx)(EventFormDialog_1.default, Object.assign({}, EventFormDialogComponentProps, { open: true, onClose: handleClick }))] }));
}
exports.default = CreateEventButton;
