import React, { useEffect, useState } from 'react';
import { clearStoredPath } from '../../utilities/redirect/LocalRedirectUrlStorage';
export const PasswordResetRedirect = (props) => {
    var _a;
    const [errorDescription, setErrorDescription] = useState(null);
    useEffect(() => {
        var _a, _b;
        // Get the hash portion of the URL
        const hash = window.location.hash || ''; // Fallback to empty string if no hash
        if ((_a = props === null || props === void 0 ? void 0 : props.error) === null || _a === void 0 ? void 0 : _a.errorMessage) {
            formatError((_b = props === null || props === void 0 ? void 0 : props.error) === null || _b === void 0 ? void 0 : _b.errorMessage);
        }
        else if (hash) {
            // Parse the hash to get query parameters
            const params = new URLSearchParams(hash.substring(1)); // Remove "#" and parse
            // Get and decode the error_description
            const errorDescription = params.get('error_description');
            if (errorDescription) {
                formatError(errorDescription);
                // Decode the URL-encoded string
            }
        }
    }, [(_a = props === null || props === void 0 ? void 0 : props.error) === null || _a === void 0 ? void 0 : _a.errorMessage]);
    const formatError = (errorDescription) => {
        const decodedDescription = decodeURIComponent(errorDescription).replace(/%0d%0a/g, '\n'); // Replace %0d%0a with newlines for clarity
        if (decodedDescription.includes('AADB2C90208')) {
            // Hardcoded message for AADB2C90208 (a specific error code, often related to user not found or invalid credentials)
            setErrorDescription('The password reset link has expired.');
            // Check for "AADB2C" or "AADB2C90208" in the decoded description
        }
        else if (decodedDescription.includes('AADB2C')) {
            // Hardcoded message for generic AADB2C errors
            setErrorDescription('Unable to find an account associated to that email.');
        }
        else {
            // Fallback message if neither error is found (optional)
            setErrorDescription('An unexpected error occurred.');
        }
    };
    const handleGoHome = () => {
        clearStoredPath();
        window.location.href = '/';
    };
    return (React.createElement("div", null, React.createElement("div", { style: { textAlign: 'center', marginTop: '50px' } }, React.createElement("img", { src: "https://customb2cpolicydevtest.blob.core.windows.net/root/unitylogo.svg", alt: "Unity Logo", width: "300", style: { height: 'auto', marginBottom: 50 } }), errorDescription ? (React.createElement("h2", null, errorDescription)) : (React.createElement("h2", null, "Your password has been successfully rese")), errorDescription !== 'The password reset link has expired.' && (React.createElement("p", null, "Please click below to return to the sign in page")), errorDescription !== 'The password reset link has expired.' && (React.createElement("button", { onClick: handleGoHome, style: {
            marginTop: '10px',
            backgroundColor: '#0C2F50',
            color: 'white',
            border: 'none',
            padding: '10px 20px',
            cursor: 'pointer',
            borderRadius: '5px',
            fontSize: '16px'
        } }, "Sign I")))));
};
export default PasswordResetRedirect;
//# sourceMappingURL=PasswordResetRedirect.js.map