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 } from '../../constants/Account';
const messages = defineMessages({
    emailError: {
        id: 'ui.common.error.email',
        defaultMessage: 'ui.common.error.email'
    }
});
const PREFIX = 'SCEmailTextField';
const Root = styled(TextField, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({}));
export default function PasswordTextField(props) {
    // PROPS
    const { onChange, error = false, helperText = null } = props, rest = __rest(props, ["onChange", "error", "helperText"]);
    // STATE
    const [errorMsg, setErrorMsg] = useState(null);
    // HOOKS
    const intl = useIntl();
    // HANDLERS
    const handleChange = (event) => {
        if (event.target.value && !EMAIL_REGEX.test(event.target.value)) {
            setErrorMsg(intl.formatMessage(messages.emailError));
        }
        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 }));
}
