"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const styles_1 = require("@mui/material/styles");
const material_1 = require("@mui/material");
const react_1 = require("react");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const react_intl_1 = require("react-intl");
const react_core_1 = require("@selfcommunity/react-core");
const Composer_1 = require("../../../constants/Composer");
const constants_1 = require("../constants");
const classes = {
    root: `${constants_1.PREFIX}-typeswitch-root`
};
const Root = (0, styles_1.styled)(material_1.ToggleButtonGroup, {
    name: constants_1.PREFIX,
    slot: 'TypeSwitchButtonGroupRoot'
})(() => ({}));
function ComposerTypeButtonGroup(props) {
    // Context
    const { preferences } = (0, react_core_1.useSCPreferences)();
    const scUserContext = (0, react_core_1.useSCUser)();
    // MEMO
    const hasPollType = (0, react_1.useMemo)(() => preferences[react_core_1.SCPreferences.ADDONS_POLLS_ENABLED].value || react_core_1.UserUtils.isStaff(scUserContext.user), [preferences, scUserContext.user]);
    const hasPostType = (0, react_1.useMemo)(() => preferences[react_core_1.SCPreferences.CONFIGURATIONS_POST_TYPE_ENABLED].value, [preferences]);
    const hasDiscussionType = (0, react_1.useMemo)(() => preferences[react_core_1.SCPreferences.CONFIGURATIONS_DISCUSSION_TYPE_ENABLED].value, [preferences]);
    // PROPS
    const { className, onChange, value } = props, rest = tslib_1.__rest(props, ["className", "onChange", "value"]);
    // EFFECTS
    (0, react_1.useEffect)(() => {
        if (value === null) {
            onChange && onChange(hasPostType ? Composer_1.COMPOSER_TYPE_POST : Composer_1.COMPOSER_TYPE_DISCUSSION);
        }
    }, [value]);
    // HANDLERS
    const handleChange = (0, react_1.useCallback)((event, value) => {
        onChange && onChange(value);
    }, [onChange]);
    return ((0, jsx_runtime_1.jsxs)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className), onChange: handleChange, exclusive: true, value: value }, rest, { children: [hasPostType && ((0, jsx_runtime_1.jsx)(material_1.ToggleButton, Object.assign({ value: Composer_1.COMPOSER_TYPE_POST }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.composer.typeSwitch.post", defaultMessage: "ui.composer.typeSwitch.post" }) }))), hasDiscussionType && ((0, jsx_runtime_1.jsx)(material_1.ToggleButton, Object.assign({ value: Composer_1.COMPOSER_TYPE_DISCUSSION }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.composer.typeSwitch.discussion", defaultMessage: "ui.composer.typeSwitch.discussion" }) }))), hasPostType && hasPollType && ((0, jsx_runtime_1.jsx)(material_1.ToggleButton, Object.assign({ value: Composer_1.COMPOSER_TYPE_POLL }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.composer.typeSwitch.poll", defaultMessage: "ui.composer.typeSwitch.poll" }) })))] })));
}
exports.default = ComposerTypeButtonGroup;
