'use client'
import { jsx } from 'react/jsx-runtime';
import { createContext, useState, useCallback, useRef, useEffect } from 'react';
import { createProviderHook } from '../../shared/utils/functions/createProviderHook/createProviderHook.js';
import '@dynamic-labs/sdk-api-core';
import '../../utils/constants/values.js';
import '@dynamic-labs/utils';
import '../../../../_virtual/_tslib.js';
import '@dynamic-labs/multi-wallet';
import '../../shared/logger.js';
import 'react-international-phone';
import '../../utils/constants/colors.js';
import '@dynamic-labs/iconic';
import '@dynamic-labs/wallet-connector-core';
import '../ViewContext/ViewContext.js';
import '@dynamic-labs/wallet-book';
import '../../shared/consts/index.js';
import '../../store/state/nonce/nonce.js';
import '@dynamic-labs-wallet/browser-wallet-client';
import '@dynamic-labs-sdk/client/core';
import 'eventemitter3';
import '@dynamic-labs-sdk/client';
import '../../config/ApiEndpoint.js';
import '../../events/dynamicEvents.js';
import { randomString } from '../../utils/functions/randomString/randomString.js';
import '@dynamic-labs/locale';
import '../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../store/state/primaryWalletId/primaryWalletId.js';
import '../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import '../../utils/functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.js';

const widgetRegistryContext = createContext(undefined);
const WidgetRegistryContextProvider = ({ children, }) => {
    const [registry, setRegistry] = useState({});
    const registerId = useCallback((widgetId, widgetType) => {
        setRegistry((prevRegistry) => (Object.assign(Object.assign({}, prevRegistry), { [widgetId]: {
                type: widgetType,
            } })));
    }, [setRegistry]);
    const removeId = useCallback((widgetId) => {
        setRegistry((prevRegistry) => {
            const newRegistry = Object.assign({}, prevRegistry);
            delete newRegistry[widgetId];
            return newRegistry;
        });
    }, [setRegistry]);
    const checkWidgetPresence = useCallback((widgetType) => Object.values(registry).some((widget) => widget.type === widgetType), [registry]);
    return (jsx(widgetRegistryContext.Provider, { value: { checkWidgetPresence, registerId, removeId }, children: children }));
};
const useWidgetRegistry = createProviderHook(widgetRegistryContext, 'WidgetRegistryContextProvider');
const useRegisterWidget = (widgetType, enabled = true) => {
    const idRef = useRef(randomString(8));
    const { registerId, removeId } = useWidgetRegistry();
    useEffect(() => {
        if (enabled === false) {
            return;
        }
        registerId(idRef.current, widgetType);
        return () => {
            // This is disabled because eslint think we are using state value
            // but this is fine because it is a ref
            // eslint-disable-next-line react-hooks/exhaustive-deps
            removeId(idRef.current);
        };
    }, [widgetType, enabled, registerId, removeId]);
};
const useWidgetRegistryCheck = (widgetType) => {
    const { checkWidgetPresence } = useWidgetRegistry();
    return checkWidgetPresence(widgetType);
};

export { WidgetRegistryContextProvider, useRegisterWidget, useWidgetRegistryCheck };
