'use client'
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var utils = require('@dynamic-labs/utils');
var logger = require('../../shared/logger.cjs');
var createProviderHook = require('../../shared/utils/functions/createProviderHook/createProviderHook.cjs');

const FALLBACK_ERROR_LOG_EVENT = 'ui.fallback_error_shown';
const DEFAULT_ERROR_MESSAGE = 'Something went wrong. Please try again.';
const MESSAGE_SIGNATURE_DENIED = 'Message signature denied.';
const UNLOCK_WALLET_MESSAGE = 'Please unlock your wallet extension and try again.';
const INTERNAL_ERROR_MESSAGE = 'There was an internal error. Please try again.';
const AUTHENTICATION_FAILED_MESSAGE = 'Authentication failed. Please try again.';
const SESSION_TIMEOUT_MESSAGE = 'Session time out. Please try again.';
const PHANTOM_SIGNING_FAILED_MESSAGE = "Phantom couldn't sign with the selected account. Switch accounts in Phantom or update the app, then try again.";
/**
 * Error code used when Phantom relays an internal signing failure via its
 * deep-link redirect (e.g. the active account has no usable key to sign).
 * Phantom's own code for these is generic (e.g. `500`), so we normalize to
 * this key to surface an actionable message instead of the default fallback.
 */
const PHANTOM_SIGNING_FAILED_ERROR_CODE = 'phantom_signing_failed';
let _errorMessageMap;
const getErrorMessageMap = () => {
    if (!_errorMessageMap) {
        _errorMessageMap = {
            '-1': DEFAULT_ERROR_MESSAGE,
            '-32000': MESSAGE_SIGNATURE_DENIED,
            '-32002': UNLOCK_WALLET_MESSAGE,
            '-32003': MESSAGE_SIGNATURE_DENIED,
            '-32602': 'Invalid parameters. Please try again.',
            '-32603': INTERNAL_ERROR_MESSAGE,
            0: MESSAGE_SIGNATURE_DENIED,
            4001: MESSAGE_SIGNATURE_DENIED,
            4100: UNLOCK_WALLET_MESSAGE,
            4900: INTERNAL_ERROR_MESSAGE,
            5001: MESSAGE_SIGNATURE_DENIED,
            5002: MESSAGE_SIGNATURE_DENIED,
            [PHANTOM_SIGNING_FAILED_ERROR_CODE]: PHANTOM_SIGNING_FAILED_MESSAGE,
            [utils.SdkErrorCode.CONNECTION_REJECTED]: 'Connection rejected. Please try again.',
            account_already_linked_to_different_profile: 'This social account is already linked to a different profile.',
            [utils.SdkErrorCode.INVALID_EMAIL_VERIFICATION]: 'The verification link is no longer valid. Please start again.',
            general_error: DEFAULT_ERROR_MESSAGE,
            invalid_provider: 'This sign-in provider is not supported.',
            [utils.SdkErrorCode.MISSING_NONCE]: DEFAULT_ERROR_MESSAGE,
            metamask_timeout: 'Whoops. Looks like something went wrong. Please try again.',
            'missing-public-address': 'Connection cancelled. Please try again',
            no_account_linked: 'No linked account was found for this provider.',
            no_auth_code: AUTHENTICATION_FAILED_MESSAGE,
            no_oauth_url: AUTHENTICATION_FAILED_MESSAGE,
            no_provider: 'A sign-in provider is required.',
            no_webauthn_support_error: 'Your browser does not support passkeys. Please try a different browser.',
            oauth_error: AUTHENTICATION_FAILED_MESSAGE,
            oauth_window_blocked: 'To connect to your social account, enable popups in your browser and then try again.',
            oauth_window_timeout: SESSION_TIMEOUT_MESSAGE,
            provider_not_enabled: 'This sign-in provider is not enabled. Please contact the app administrator.',
            session_timeout: SESSION_TIMEOUT_MESSAGE,
            signin_error: 'Sign-in failed. Please try again.',
            social_linking_not_enabled: 'Social account linking is not enabled for this application.',
            unlink_error: 'Failed to unlink your account. Please try again.',
            [utils.SdkErrorCode.TOO_MANY_EMAIL_VERIFICATION_ATTEMPTS]: 'Too many email verification attempts, please try again later',
            [utils.SdkErrorCode.UNKNOWN_ERROR]: 'An unexpected error occurred. Please try again.',
            [utils.SdkErrorCode.USER_CANCELLED]: '',
            [utils.SdkErrorCode.USER_CANCELLED_OAUTH]: '',
            verification_error: 'Verification failed. Please try again.',
            [utils.SdkErrorCode.WALLET_NOT_DEPLOYED]: 'Your smart wallet has not been deployed.',
            wrong_email_verification_token: 'The code you entered is incorrect. Please try again.',
        };
    }
    return _errorMessageMap;
};
/**
 * Returns the mapped, user-facing message for a given error code, or
 * `undefined` when the code is not recognized. Exported so callers can decide
 * whether to normalize an unmapped code before displaying it.
 */
const getMappedErrorMessage = (status) => getErrorMessageMap()[status];
const ErrorContext = React.createContext(undefined);
const ErrorContextProvider = ({ children }) => {
    /** The error message as a string, used as a fallback if translation is not available */
    const [error, _setError] = React.useState(undefined);
    /** The [ErrorCode] of the error, used for translation. */
    const [errorCode, _setErrorCode] = React.useState(undefined);
    const setError = React.useCallback((error, errorCode) => {
        _setError(error);
        _setErrorCode(errorCode);
    }, []);
    /** Uses the default error message for fallbacks */
    const setDefaultError = React.useCallback((error) => {
        logger.logger.instrument(FALLBACK_ERROR_LOG_EVENT, {
            error: error instanceof Error ? error.message : undefined,
            errorCode: utils.ErrorCode.DEFAULT,
            key: FALLBACK_ERROR_LOG_EVENT,
            source: 'setDefaultError',
            time: 0,
        });
        setError(DEFAULT_ERROR_MESSAGE, utils.ErrorCode.DEFAULT);
    }, [setError]);
    /** @deprecated Prefer setError with a translation key instead */
    const setErrorMessage = React.useCallback((status, error, source) => {
        const mappedMessage = getErrorMessageMap()[status];
        if (mappedMessage === undefined) {
            logger.logger.instrument(FALLBACK_ERROR_LOG_EVENT, {
                error: error instanceof Error ? error.message : undefined,
                errorCode: status,
                key: FALLBACK_ERROR_LOG_EVENT,
                source: source !== null && source !== void 0 ? source : 'setErrorMessage',
                time: 0,
            });
        }
        setError(mappedMessage !== null && mappedMessage !== void 0 ? mappedMessage : DEFAULT_ERROR_MESSAGE);
    }, [setError]);
    const clearError = React.useCallback(() => {
        _setError(undefined);
        _setErrorCode(undefined);
    }, []);
    const value = React.useMemo(() => ({
        clearError,
        error,
        errorCode,
        setDefaultError,
        setError,
        setErrorMessage,
    }), [clearError, error, errorCode, setDefaultError, setError, setErrorMessage]);
    return (jsxRuntime.jsx(ErrorContext.Provider, { value: value, children: children }));
};
const useErrorContext = createProviderHook.createProviderHook(ErrorContext, 'ErrorContextProvider');

exports.ErrorContext = ErrorContext;
exports.ErrorContextProvider = ErrorContextProvider;
exports.PHANTOM_SIGNING_FAILED_ERROR_CODE = PHANTOM_SIGNING_FAILED_ERROR_CODE;
exports.getMappedErrorMessage = getMappedErrorMessage;
exports.useErrorContext = useErrorContext;
