'use client'
'use strict';

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

var React = require('react');
var client = require('@dynamic-labs-sdk/client');
var core = require('@dynamic-labs-sdk/client/core');
var client$1 = require('../../../client.cjs');
var constants = require('../../constants.cjs');
var ApiEndpoint = require('../../../../config/ApiEndpoint.cjs');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react/jsx-runtime');
require('../../../../context/ViewContext/ViewContext.cjs');
var logger = require('../../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('@dynamic-labs/utils');
require('../../../../utils/constants/colors.cjs');
require('../../../../utils/constants/values.cjs');
require('@dynamic-labs/sdk-api-core');
require('../../../../shared/consts/index.cjs');
var getApiHeaders = require('./getApiHeaders/getApiHeaders.cjs');
var syncEvents = require('./syncEvents/syncEvents.cjs');

const useInitializeSdkClient = ({ settings, client: clientFromProps, }) => {
    const lastClientDependencyKey = React.useRef(null);
    /**
     * Sets the client to the state and setup the extensions.
     */
    const setClient = (client$2) => {
        if (client.hasExtension({ extensionKey: constants.CLIENT_EXTENSION_NAME }, client$2)) {
            return;
        }
        client$1.setDynamicClient(client$2);
        core.registerExtension({ extensionKey: constants.CLIENT_EXTENSION_NAME }, client$2);
        syncEvents.syncEvents(client$2);
    };
    /**
     * If a client is provided by the props, we should use it instead of
     * creating a new one.
     */
    if (clientFromProps) {
        if (!client$1.hasDynamicClient() || client$1.getDynamicClient() !== clientFromProps) {
            setClient(clientFromProps);
        }
        return;
    }
    const clientDependencyKey = [
        settings.apiBaseUrl,
        settings.environmentId,
    ].join('-');
    logger.logger.logVerboseTroubleshootingMessage('[useInitializeSdkClient] Checking if DynamicClient needs to be created...', {
        clientDependencyKey,
        lastClientDependencyKey: lastClientDependencyKey.current,
    });
    if (lastClientDependencyKey.current === clientDependencyKey)
        return;
    lastClientDependencyKey.current = clientDependencyKey;
    logger.logger.logVerboseTroubleshootingMessage('[useInitializeSdkClient] Creating DynamicClient...');
    const client$2 = client.createDynamicClient({
        coreConfig: {
            apiBaseUrl: ApiEndpoint.getBaseUrl(),
            getApiHeaders: getApiHeaders.getApiHeaders,
        },
        environmentId: settings.environmentId,
        logLevel: settings.logLevel === 'DEBUG' ? 'debug' : undefined,
    });
    setClient(client$2);
};

exports.useInitializeSdkClient = useInitializeSdkClient;
