import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Button, Icon } from '@mui/material';
import { styled } from '@mui/material/styles';
import { useThemeProps } from '@mui/system';
import { SCPreferences, SCUserContext, UserUtils, useSCPreferences } from '@selfcommunity/react-core';
import classNames from 'classnames';
import React, { useContext, useMemo } from 'react';
import { FormattedMessage } from 'react-intl';
import { SCCommunitySubscriptionTier } from '@selfcommunity/types';
import CreateLivestreamDialog from '../CreateLiveStreamDialog';
const PREFIX = 'SCCreateLivestreamButton';
const classes = {
    root: `${PREFIX}-root`
};
const Root = styled(Button, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(() => ({}));
/**
 *> API documentation for the Community-JS CreateLiveStreamButton component. Learn about the available props and the CSS API.
 *
 #### Import
 ```jsx
 import {CreateLiveStreamButton} from '@selfcommunity/react-ui';
 ```

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

 #### CSS

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

 * @param inProps
 */
export default function CreateLiveStreamButton(inProps) {
    //PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { className, CreateLiveStreamDialogComponentProps = {}, onSuccess, children } = props, rest = __rest(props, ["className", "CreateLiveStreamDialogComponentProps", "onSuccess", "children"]);
    // CONTEXT
    const scUserContext = useContext(SCUserContext);
    const { preferences, features } = useSCPreferences();
    // STATE
    const [open, setOpen] = React.useState(false);
    // CONST
    const authUserId = useMemo(() => (scUserContext.user ? scUserContext.user.id : null), [scUserContext.user]);
    const isStaff = useMemo(() => scUserContext.user && UserUtils.isStaff(scUserContext.user), [scUserContext.user]);
    const isCommunityOwner = useMemo(() => authUserId === 1, [authUserId]);
    const isFreeTrialTier = useMemo(() => preferences &&
        SCPreferences.CONFIGURATIONS_SUBSCRIPTION_TIER in preferences &&
        preferences[SCPreferences.CONFIGURATIONS_SUBSCRIPTION_TIER].value &&
        preferences[SCPreferences.CONFIGURATIONS_SUBSCRIPTION_TIER].value === SCCommunitySubscriptionTier.FREE_TRIAL, [preferences]);
    const liveStreamEnabled = useMemo(() => preferences &&
        features &&
        SCPreferences.CONFIGURATIONS_LIVE_STREAM_ENABLED in preferences &&
        preferences[SCPreferences.CONFIGURATIONS_LIVE_STREAM_ENABLED].value, [preferences, features]);
    const onlyStaffEnabled = useMemo(() => preferences[SCPreferences.CONFIGURATIONS_LIVE_STREAM_ONLY_STAFF_ENABLED].value, [preferences]);
    /**
     * Handle close
     */
    const handleClose = () => {
        setOpen((o) => !o);
    };
    /**
     * Handle close
     */
    const handleSuccess = (data) => {
        onSuccess && onSuccess(data);
        setOpen((o) => !o);
    };
    /**
     * If there's no authUserId, component is hidden.
     */
    if (!liveStreamEnabled || !authUserId || (onlyStaffEnabled && !isStaff) || (isFreeTrialTier && !isCommunityOwner)) {
        return null;
    }
    /**
     * Renders root object
     */
    return (_jsxs(React.Fragment, { children: [_jsx(Root, Object.assign({ className: classNames(classes.root, className), onClick: handleClose, variant: "contained", color: "secondary", startIcon: _jsx(Icon, { children: "photo_camera" }) }, rest, { children: children !== null && children !== void 0 ? children : _jsx(FormattedMessage, { id: "ui.createEventButton.goLive", defaultMessage: "ui.createEventButton.goLive" }) })), open && _jsx(CreateLivestreamDialog, Object.assign({ open: true, onClose: handleClose, onSuccess: handleSuccess }, CreateLiveStreamDialogComponentProps))] }));
}
