"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importStar(require("react"));
const styles_1 = require("@mui/material/styles");
const system_1 = require("@mui/system");
const react_core_1 = require("@selfcommunity/react-core");
const types_1 = require("@selfcommunity/types");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const react_intl_1 = require("react-intl");
const BaseDialog_1 = tslib_1.__importDefault(require("../../shared/BaseDialog"));
const constants_1 = require("./constants");
const EventForm_1 = tslib_1.__importDefault(require("../EventForm"));
const LiveStreamSelector_1 = tslib_1.__importDefault(require("./LiveStreamSelector/LiveStreamSelector"));
const types_2 = require("./types");
const LiveStreamForm_1 = tslib_1.__importDefault(require("../LiveStreamForm"));
const Slide_1 = tslib_1.__importDefault(require("@mui/material/Slide"));
const material_1 = require("@mui/material");
const classes = {
    root: `${constants_1.PREFIX}-root`,
    title: `${constants_1.PREFIX}-title`,
    content: `${constants_1.PREFIX}-content`
};
const Root = (0, styles_1.styled)(BaseDialog_1.default, {
    name: constants_1.PREFIX,
    slot: 'Root'
})(({ theme }) => ({
    paddingBottom: '0px !important',
    [`& .${classes.title}`]: {
        display: 'flex'
    },
    [`& .${classes.content}`]: {
        paddingBottom: 0
    }
}));
const Transition = react_1.default.forwardRef(function Transition(props, ref) {
    return (0, jsx_runtime_1.jsx)(Slide_1.default, Object.assign({ direction: "up", ref: ref }, props));
});
/**
 *> API documentation for the Community-JS CreateLiveStreamDialog component. Learn about the available props and the CSS API.
 *
 #### Import
 ```jsx
 import {CreateLivestreamDialog} from '@selfcommunity/react-ui';
 ```

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

 #### CSS

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

 * @param inProps
 */
function CreateLiveStreamDialog(inProps) {
    var _a, _b;
    //PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: constants_1.PREFIX
    });
    const { className, open = true, onClose, onSuccess } = props, rest = tslib_1.__rest(props, ["className", "open", "onClose", "onSuccess"]);
    // CONTEXT
    const scUserContext = (0, react_core_1.useSCUser)();
    const canCreateLiveStream = (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_live_stream; }, [(_a = scUserContext === null || scUserContext === void 0 ? void 0 : scUserContext.user) === null || _a === void 0 ? void 0 : _a.permission]);
    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; }, [(_b = scUserContext === null || scUserContext === void 0 ? void 0 : scUserContext.user) === null || _b === void 0 ? void 0 : _b.permission]);
    // STATE
    const [step, setStep] = (0, react_1.useState)(canCreateEvent ? types_2.CreateLiveStreamStep.SELECT_TYPE : types_2.CreateLiveStreamStep.CREATE_LIVE);
    const [liveType, setLiveType] = (0, react_1.useState)(canCreateEvent ? null : types_2.LiveStreamType.DIRECT_LIVE);
    // HANDLER
    const handleLiveTypeSelected = (0, react_1.useCallback)((l) => {
        setLiveType(l);
    }, []);
    const handleLiveTypeSelectedNext = (0, react_1.useCallback)((l) => {
        setLiveType(l);
        setStep(types_2.CreateLiveStreamStep.CREATE_LIVE);
    }, []);
    const handleBack = (0, react_1.useCallback)((l) => {
        setStep(types_2.CreateLiveStreamStep.SELECT_TYPE);
    }, []);
    const handleSubmit = (0, react_1.useCallback)((e) => {
        onSuccess && onSuccess(e);
    }, []);
    (0, react_1.useEffect)(() => {
        if (!canCreateEvent) {
            setLiveType(types_2.LiveStreamType.DIRECT_LIVE);
        }
    }, [canCreateEvent]);
    // user must be logged
    if (!scUserContext.user || !canCreateLiveStream) {
        return null;
    }
    /**
     * Renders root object
     */
    return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ DialogContentProps: { dividers: false }, maxWidth: 'md', title: (0, jsx_runtime_1.jsxs)(material_1.Box, Object.assign({ className: classes.title, component: 'span' }, { children: [Boolean(step === types_2.CreateLiveStreamStep.CREATE_LIVE && canCreateEvent) && ((0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ variant: "text", onClick: handleBack, startIcon: (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "arrow_back" }) }, { children: "Back" }))), (0, jsx_runtime_1.jsx)(material_1.Box, Object.assign({ component: 'span' }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.createLivestreamDialog.title", defaultMessage: "ui.createLivestreamDialog.title" }) }))] })), fullWidth: true, open: open, scroll: 'paper', onClose: onClose, className: (0, classnames_1.default)(classes.root, className), TransitionComponent: Transition, PaperProps: { elevation: 0 } }, rest, { children: (0, jsx_runtime_1.jsxs)(material_1.Box, Object.assign({ className: classes.content }, { children: [step === types_2.CreateLiveStreamStep.SELECT_TYPE && ((0, jsx_runtime_1.jsx)(LiveStreamSelector_1.default, { liveSelected: liveType, onLiveSelected: handleLiveTypeSelected, onNext: handleLiveTypeSelectedNext })), step === types_2.CreateLiveStreamStep.CREATE_LIVE && ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: liveType === types_2.LiveStreamType.EVENT_LIVE ? ((0, jsx_runtime_1.jsx)(EventForm_1.default, { EventAddressComponentProps: { locations: [types_1.SCEventLocationType.LIVESTREAM] }, onSuccess: handleSubmit })) : ((0, jsx_runtime_1.jsx)(LiveStreamForm_1.default, { onSuccess: handleSubmit })) }))] })) })));
}
exports.default = CreateLiveStreamDialog;
