"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 react_core_1 = require("@selfcommunity/react-core");
const material_1 = require("@mui/material");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const react_intl_1 = require("react-intl");
const EmailTextField_1 = tslib_1.__importDefault(require("../../shared/EmailTextField"));
const system_1 = require("@mui/system");
const api_services_1 = require("@selfcommunity/api-services");
const PREFIX = 'SCAccountRecover';
const classes = {
    root: `${PREFIX}-root`,
    form: `${PREFIX}-form`,
    email: `${PREFIX}-email`,
    success: `${PREFIX}-success`
};
const Root = (0, styles_1.styled)(material_1.Box, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({
    [`& .${classes.form} .MuiTextField-root, &.${classes.root} .MuiButton-root`]: {
        margin: theme.spacing(1, 0, 1, 0)
    },
    [`& .${classes.form} .MuiTypography-root`]: {
        margin: theme.spacing(1, 0, 1, 0)
    }
}));
/**
 * > API documentation for the Community-JS Categories component. Learn about the available props and the CSS API.
 *
 *
 * The Categories component renders the list of all available categories.
 * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/AccountRecover)

 #### Import

 ```jsx
 import {AccountVerify} from '@selfcommunity/react-ui';
 ```

 #### Component Name

 The name `SCAccountRecover` can be used when providing style overrides in the theme.


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCAccountRecover-root|Styles applied to the root element.|
 |form|.SCAccountRecover-form|Styles applied to the form element.|
 |email|.SCAccountRecover-email|Styles applied to the email TextField.|
 |success|.SCAccountRecover-success|Styles applied to the success Typography.|

 *
 * @param inProps
 */
function AccountRecover(inProps) {
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    // PROPS
    const { className, onSuccess = null, TextFieldProps = { variant: 'outlined', fullWidth: true }, ButtonProps = { variant: 'contained' }, successAction = null } = props, rest = tslib_1.__rest(props, ["className", "onSuccess", "TextFieldProps", "ButtonProps", "successAction"]);
    // STATE
    const [email, setEmail] = (0, react_1.useState)('');
    const [emailError, setEmailError] = (0, react_1.useState)('');
    const [isSubmitting, setIsSubmitting] = (0, react_1.useState)(false);
    const [isSucceed, setIsSucceed] = (0, react_1.useState)(false);
    // CONTEXT
    const scUserContext = (0, react_core_1.useSCUser)();
    const intl = (0, react_intl_1.useIntl)();
    // HANDLERS
    const handleChange = (event) => {
        setEmail(event.target.value);
        setEmailError('');
    };
    const handleSubmit = (event) => {
        event.preventDefault();
        event.stopPropagation();
        setIsSubmitting(true);
        api_services_1.AccountService.recover({ email })
            .then(() => {
            setIsSucceed(true);
            onSuccess && onSuccess();
        })
            .catch((error) => {
            const _error = (0, api_services_1.formatHttpErrorCode)(error);
            if (_error.emailError) {
                setEmailError(_error.emailError.error);
            }
        })
            .then(() => setIsSubmitting(false));
        return false;
    };
    if (scUserContext.user !== null) {
        // User already logged in
        return null;
    }
    // RENDER
    return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { children: isSucceed ? ((0, jsx_runtime_1.jsxs)(material_1.Alert, Object.assign({ severity: "success", className: classes.success }, { children: [intl.formatMessage({ id: 'ui.accountRecover.success', defaultMessage: 'ui.accountRecover.success' }, {
                    email,
                    // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
                    // @ts-ignore
                    b: (chunks) => (0, jsx_runtime_1.jsx)("b", { children: chunks })
                }), successAction] }))) : ((0, jsx_runtime_1.jsxs)("form", Object.assign({ className: classes.form, onSubmit: handleSubmit }, { children: [(0, jsx_runtime_1.jsx)(EmailTextField_1.default, Object.assign({ className: classes.email, disabled: isSubmitting, label: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.accountRecover.email.label", defaultMessage: "ui.accountRecover.email.label" }) }, TextFieldProps, { value: email, onChange: handleChange, error: Boolean(emailError), helperText: emailError && ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: `ui.accountRecover.email.error.${emailError}`, defaultMessage: `ui.accountRecover.email.error.${emailError}` })) })), (0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ type: "submit" }, ButtonProps, { disabled: !email || Boolean(emailError) || isSubmitting }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.accountRecover.submit", defaultMessage: "ui.accountRecover.submit" }) }))] }))) })));
}
exports.default = AccountRecover;
