import { omitUser } from '../utils/index.mjs';

const DefaultDynamicConfig$2 = Promise.resolve({
    siweRequiredForCryptoWallets: false
});
class HeadlessCryptoWalletClient {
    _networkClient;
    _apiNetworkClient;
    _subscriptionService;
    executeRecaptcha;
    dfpProtectedAuth;
    _config;
    authenticate;
    constructor(_networkClient, _apiNetworkClient, _subscriptionService, executeRecaptcha = ()=>Promise.resolve(undefined), dfpProtectedAuth, _config = DefaultDynamicConfig$2){
        this._networkClient = _networkClient;
        this._apiNetworkClient = _apiNetworkClient;
        this._subscriptionService = _subscriptionService;
        this.executeRecaptcha = executeRecaptcha;
        this.dfpProtectedAuth = dfpProtectedAuth;
        this._config = _config;
        this.authenticate = this._subscriptionService.withUpdateSession(async (options)=>{
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            const resp = await this._apiNetworkClient.retriableFetchSDK({
                url: '/crypto_wallets/authenticate',
                method: 'POST',
                body: {
                    session_duration_minutes: options.session_duration_minutes,
                    crypto_wallet_address: options.crypto_wallet_address,
                    crypto_wallet_type: options.crypto_wallet_type,
                    signature: options.signature,
                    captcha_token,
                    dfp_telemetry_id
                },
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
            return omitUser(resp);
        });
    }
    async authenticateStart(options) {
        if (options.siwe_params) ;
        const isLoggedIn = !!this._subscriptionService.getSession();
        const captcha_token = await this.executeRecaptcha();
        const { siweRequiredForCryptoWallets } = await this._config;
        const body = {
            crypto_wallet_address: options.crypto_wallet_address,
            crypto_wallet_type: options.crypto_wallet_type
        };
        if (siweRequiredForCryptoWallets && options.crypto_wallet_type == 'ethereum') {
            body.siwe_params = {
                ...options.siwe_params,
                uri: options.siwe_params?.uri || window.location.origin
            };
        }
        const endpoint = isLoggedIn ? '/crypto_wallets/authenticate/start/secondary' : '/crypto_wallets/authenticate/start/primary';
        const requestBody = {
            ...body,
            captcha_token
        };
        return this._apiNetworkClient.fetchSDK({
            url: endpoint,
            method: 'POST',
            body: requestBody
        });
    }
}

export { HeadlessCryptoWalletClient };
//# sourceMappingURL=HeadlessCryptoWalletClient.mjs.map
