"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const styles_1 = require("@mui/material/styles");
const system_1 = require("@mui/system");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const material_1 = require("@mui/material");
const react_intl_1 = require("react-intl");
const api_services_1 = require("@selfcommunity/api-services");
const types_1 = require("@selfcommunity/types");
const react_core_1 = require("@selfcommunity/react-core");
const PREFIX = 'SCLanguageSwitcher';
const classes = {
    root: `${PREFIX}-root`,
    label: `${PREFIX}-label`
};
const Root = (0, styles_1.styled)(material_1.FormControl, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => [styles.root]
})(() => ({}));
function LanguageSwitcher(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    // CONTEXT
    const { className = null, 
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    handleLanguageSwitch, languages = [types_1.SCLanguageType.EN, types_1.SCLanguageType.IT], minimized = false, LabelComponentProps = {}, SelectComponentProps = {} } = props, rest = tslib_1.__rest(props, ["className", "handleLanguageSwitch", "languages", "minimized", "LabelComponentProps", "SelectComponentProps"]);
    const scUserContext = (0, react_core_1.useSCUser)();
    const scLocaleContext = (0, react_core_1.useSCLocale)();
    const [updating, setUpdating] = (0, react_1.useState)(false);
    const intl = (0, react_intl_1.useIntl)();
    // Handle callback switch language
    const handleSwitchLanguage = (language) => {
        if (handleLanguageSwitch) {
            handleLanguageSwitch(language)
                .then(() => {
                scLocaleContext.selectLocale(language);
                setUpdating(false);
            })
                .catch((e) => {
                console.log(e);
                setUpdating(false);
            });
        }
        else {
            scLocaleContext.selectLocale(language);
            setUpdating(false);
        }
    };
    // Handle change language
    const handleChange = (e) => {
        var _a;
        const language = e.target.value;
        if (language !== intl.locale) {
            setUpdating(true);
            if (scUserContext.user) {
                api_services_1.UserService.userPatch((_a = scUserContext.user) === null || _a === void 0 ? void 0 : _a.id, {
                    language
                })
                    .then((user) => {
                    scUserContext.updateUser(user);
                    handleSwitchLanguage(language);
                })
                    .catch((error) => {
                    console.log(error);
                    setUpdating(false);
                });
            }
            else {
                handleSwitchLanguage(language);
            }
        }
    };
    return ((0, jsx_runtime_1.jsxs)(Root, Object.assign({ className: (0, classnames_1.default)(className, classes.root) }, rest, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "body1", className: classes.label }, LabelComponentProps, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.languageSwitcher.selectLanguage", defaultMessage: "ui.languageSwitcher.selectLanguage" }) })), (0, jsx_runtime_1.jsx)(material_1.Select, Object.assign({ disabled: updating, labelId: "language", id: "language", value: intl.locale, onChange: handleChange, size: "small" }, SelectComponentProps, { children: languages.map((l) => ((0, jsx_runtime_1.jsx)(material_1.MenuItem, Object.assign({ value: l }, { children: minimized ? l.toUpperCase() : (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: `ui.languageSwitcher.language.${l}`, defaultMessage: `ui.languageSwitcher.language.${l}` }) }), l))) }))] })));
}
exports.default = LanguageSwitcher;
