'use client'
'use strict';

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

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var ViewContext = require('../ViewContext/ViewContext.cjs');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
var isSSR = require('../../shared/utils/functions/isSSR/isSSR.cjs');
require('@dynamic-labs/utils');
var localStorage = require('../../utils/constants/localStorage.cjs');
require('../../utils/constants/colors.cjs');
require('../../utils/constants/values.cjs');
require('@dynamic-labs/sdk-api-core');
var useLocalStorage = require('../../shared/utils/hooks/useLocalStorage/useLocalStorage.cjs');
require('../../shared/consts/index.cjs');
require('../DynamicContext/useDynamicContext/useDynamicContext.cjs');
var useInternalDynamicContext = require('../DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.cjs');

const SSR_SAFE_DEFAULT = {
    captchaAuthState: undefined,
    engageCaptcha: () => { },
    getCaptchaToken: () => undefined,
    setCaptchaAuthState: () => { },
    setCaptchaToken: () => { },
};
const CaptchaContext = React.createContext(undefined);
const CaptchaContextProvider = ({ children, }) => {
    const { pushView } = ViewContext.useViewContext();
    const { setShowAuthFlow } = useInternalDynamicContext.useInternalDynamicContext();
    const [captchaToken, setCaptchaToken, removeCaptchaToken] = useLocalStorage.useLocalStorage(localStorage.CAPTCHA_TOKEN, undefined);
    const [captchaAuthState, setCaptchaAuthState] = React.useState();
    const engageCaptcha = React.useCallback(({ authMethod, onCaptchaSuccess, onCaptchaCancel, }) => {
        setCaptchaAuthState({ authMethod, onCaptchaSuccess });
        pushView('captcha', undefined, {
            // Navigating back abandons the pending onCaptchaSuccess continuation,
            // so let the caller settle its flow (eg dismiss a pending view)
            // instead of leaving it waiting for a captcha token forever
            onBackClick: () => {
                setCaptchaAuthState(undefined);
                onCaptchaCancel === null || onCaptchaCancel === void 0 ? void 0 : onCaptchaCancel();
            },
        });
        setShowAuthFlow(true);
    }, [pushView, setCaptchaAuthState, setShowAuthFlow]);
    const getCaptchaToken = React.useCallback(() => {
        const token = captchaToken;
        removeCaptchaToken();
        return token;
    }, [captchaToken, removeCaptchaToken]);
    const value = React.useMemo(() => ({
        captchaAuthState,
        engageCaptcha,
        getCaptchaToken,
        removeCaptchaToken,
        setCaptchaAuthState,
        setCaptchaToken,
    }), [
        captchaAuthState,
        getCaptchaToken,
        engageCaptcha,
        setCaptchaAuthState,
        setCaptchaToken,
        removeCaptchaToken,
    ]);
    return (jsxRuntime.jsx(CaptchaContext.Provider, { value: value, children: children }));
};
const useCaptchaContext = () => {
    const context = React.useContext(CaptchaContext);
    if (context === undefined) {
        if (isSSR.isSSR()) {
            return SSR_SAFE_DEFAULT;
        }
        throw new Error('Hook must be used within <CaptchaContextProvider>.');
    }
    return context;
};

exports.CaptchaContext = CaptchaContext;
exports.CaptchaContextProvider = CaptchaContextProvider;
exports.useCaptchaContext = useCaptchaContext;
