import { __rest } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { useState } from 'react';
import { styled } from '@mui/material/styles';
import { TextField } from '@mui/material';
import { defineMessages, useIntl } from 'react-intl';
import { EMAIL_REGEX, USERNAME_REGEX } from '../../constants/Account';
const messages = defineMessages({
    usernameError: {
        id: 'ui.common.error.username',
        defaultMessage: 'ui.common.error.username'
    },
    usernameOrEmail: {
        id: 'ui.common.error.usernameOrEmail',
        defaultMessage: 'ui.common.error.usernameOrEmail'
    }
});
const PREFIX = 'SCUsernameTextField';
const Root = styled(TextField, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({}));
const UsernameTextField = (props) => {
    // PROPS
    const { onChange, error = false, allowEmail = false, helperText = null } = props, rest = __rest(props, ["onChange", "error", "allowEmail", "helperText"]);
    // STATE
    const [errorMsg, setErrorMsg] = useState(null);
    // HOOKS
    const intl = useIntl();
    // HANDLERS
    const handleChange = (event) => {
        if (event.target.value) {
            if (allowEmail &&
                ((event.target.value.includes('@') && !EMAIL_REGEX.test(event.target.value)) ||
                    (!event.target.value.includes('@') && !USERNAME_REGEX.test(event.target.value)))) {
                setErrorMsg(intl.formatMessage(messages.usernameOrEmail));
            }
            else if (!allowEmail && !USERNAME_REGEX.test(event.target.value)) {
                setErrorMsg(intl.formatMessage(messages.usernameError));
            }
            else if (error !== null) {
                setErrorMsg(null);
            }
        }
        else if (error !== null) {
            setErrorMsg(null);
        }
        onChange && onChange(event);
    };
    // RENDER
    return _jsx(Root, Object.assign({}, rest, { onChange: handleChange, error: Boolean(errorMsg) || error, helperText: errorMsg || helperText }));
};
export default UsernameTextField;
