import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from 'react';
import { styled } from '@mui/material/styles';
import { useThemeProps } from '@mui/system';
import classNames from 'classnames';
import { FormControl, MenuItem, Select, Typography } from '@mui/material';
import { FormattedMessage, useIntl } from 'react-intl';
import { UserService } from '@selfcommunity/api-services';
import { SCLanguageType } from '@selfcommunity/types';
import { useSCLocale, useSCUser } from '@selfcommunity/react-core';
const PREFIX = 'SCLanguageSwitcher';
const classes = {
    root: `${PREFIX}-root`,
    label: `${PREFIX}-label`
};
const Root = styled(FormControl, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => [styles.root]
})(() => ({}));
export default function LanguageSwitcher(inProps) {
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    // CONTEXT
    const { className = null, 
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    handleLanguageSwitch, languages = [SCLanguageType.EN, SCLanguageType.IT], minimized = false, LabelComponentProps = {}, SelectComponentProps = {} } = props, rest = __rest(props, ["className", "handleLanguageSwitch", "languages", "minimized", "LabelComponentProps", "SelectComponentProps"]);
    const scUserContext = useSCUser();
    const scLocaleContext = useSCLocale();
    const [updating, setUpdating] = useState(false);
    const intl = 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) {
                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 (_jsxs(Root, Object.assign({ className: classNames(className, classes.root) }, rest, { children: [_jsx(Typography, Object.assign({ variant: "body1", className: classes.label }, LabelComponentProps, { children: _jsx(FormattedMessage, { id: "ui.languageSwitcher.selectLanguage", defaultMessage: "ui.languageSwitcher.selectLanguage" }) })), _jsx(Select, Object.assign({ disabled: updating, labelId: "language", id: "language", value: intl.locale, onChange: handleChange, size: "small" }, SelectComponentProps, { children: languages.map((l) => (_jsx(MenuItem, Object.assign({ value: l }, { children: minimized ? l.toUpperCase() : _jsx(FormattedMessage, { id: `ui.languageSwitcher.language.${l}`, defaultMessage: `ui.languageSwitcher.language.${l}` }) }), l))) }))] })));
}
