'use client'
import { jsx } from 'react/jsx-runtime';
import { createContext, useContext, useState, useCallback, useMemo } from 'react';
import { useViewContext } from '../ViewContext/ViewContext.js';
import '@dynamic-labs/iconic';
import '@dynamic-labs/wallet-connector-core';
import '../../shared/logger.js';
import '@dynamic-labs/wallet-book';
import '@dynamic-labs/utils';
import { CAPTCHA_TOKEN } from '../../utils/constants/localStorage.js';
import '../../utils/constants/colors.js';
import '../../utils/constants/values.js';
import '@dynamic-labs/sdk-api-core';
import { useLocalStorage } from '../../shared/utils/hooks/useLocalStorage/useLocalStorage.js';
import '../../shared/consts/index.js';

const CaptchaContext = createContext(undefined);
const CaptchaContextProvider = ({ children, }) => {
    const { pushView } = useViewContext();
    const [captchaToken, setCaptchaToken, removeCaptchaToken] = useLocalStorage(CAPTCHA_TOKEN, undefined);
    const [captchaAuthState, setCaptchaAuthState] = useState();
    const engageCaptcha = useCallback(({ authMethod, onCaptchaSuccess, }) => {
        setCaptchaAuthState({ authMethod, onCaptchaSuccess });
        pushView('captcha');
    }, [pushView, setCaptchaAuthState]);
    const getCaptchaToken = useCallback(() => {
        const token = captchaToken;
        removeCaptchaToken();
        return token;
    }, [captchaToken, removeCaptchaToken]);
    const value = useMemo(() => ({
        captchaAuthState,
        engageCaptcha,
        getCaptchaToken,
        removeCaptchaToken,
        setCaptchaAuthState,
        setCaptchaToken,
    }), [
        captchaAuthState,
        getCaptchaToken,
        engageCaptcha,
        setCaptchaAuthState,
        setCaptchaToken,
        removeCaptchaToken,
    ]);
    return (jsx(CaptchaContext.Provider, { value: value, children: children }));
};
const useCaptchaContext = () => {
    const context = useContext(CaptchaContext);
    if (context === undefined) {
        throw new Error('usage of useCaptchaContext not wrapped in `CaptchaContextProvider`.');
    }
    return context;
};

export { CaptchaContext, CaptchaContextProvider, useCaptchaContext };
