"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 system_1 = require("@mui/system");
const api_services_1 = require("@selfcommunity/api-services");
const react_intl_1 = require("react-intl");
const PREFIX = 'SCAccountVerify';
const classes = {
    root: `${PREFIX}-root`,
    success: `${PREFIX}-success`,
    error: `${PREFIX}-error`
};
const Root = (0, styles_1.styled)(material_1.Box, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({
    [`&.${classes.root}`]: {
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'center',
        justifyContent: 'center'
    }
}));
/**
 * > API documentation for the Community-JS AccountVerify component. Learn about the available props and the CSS API.

 #### Import

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

 #### Component Name

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


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCAccountVerify-root|Styles applied to the root element.|
 |success|.SCAccountVerify-success|Styles applied to the success Alert.|
 |error|.SCAccountVerify-error|Styles applied to the error Alert.|

 *
 * @param inProps
 */
function AccountVerify(inProps) {
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    // PROPS
    const { className, onSuccess, onError, validationCode, successAction = null, errorAction = null } = props, rest = tslib_1.__rest(props, ["className", "onSuccess", "onError", "validationCode", "successAction", "errorAction"]);
    // STATE
    const [succeed, setSucceed] = (0, react_1.useState)(false);
    const [error, setError] = (0, react_1.useState)(false);
    // CONTEXT
    const scUserContext = (0, react_core_1.useSCUser)();
    // EFFECTS
    (0, react_1.useEffect)(() => {
        if (!validationCode) {
            return;
        }
        setError(false);
        api_services_1.AccountService.verify({ validation_code: validationCode })
            .then((res) => {
            setSucceed(res.token ? true : res.user_awaiting_approval);
            onSuccess && onSuccess(res);
        })
            .catch((error) => {
            setError(true);
            onError && onError(error);
        });
    }, [validationCode]);
    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: succeed ? ((0, jsx_runtime_1.jsxs)(material_1.Alert, Object.assign({ severity: "success", className: classes.success }, { children: [typeof succeed === 'string' ? succeed : (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.accountVerify.success", defaultMessage: "ui.accountVerify.success" }), successAction] }))) : error ? ((0, jsx_runtime_1.jsxs)(material_1.Alert, Object.assign({ severity: "error", className: classes.error }, { children: [(0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.accountVerify.error", defaultMessage: "ui.accountVerify.error" }), errorAction] }))) : ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(material_1.CircularProgress, {}), (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.accountVerify.verifying", defaultMessage: "ui.accountVerify.verifying" })] })) })));
}
exports.default = AccountVerify;
