import { executeNodejsRequest } from './NodejsHttpRequestUtils.js';
/**
 * Adapter for Node Https module to be used as HttpClient
 */
export class BinaryNodejsHttpClient {
    https;
    constructor(https) {
        this.https = https;
    }
    async request(url, requestOptions) {
        return await executeNodejsRequest(this.https, url, requestOptions, (data) => Buffer.from(data));
    }
}
/**
 * Adapter for Node Https module to be used as HttpClient
 */
export class BinaryFetchClient {
    fetch;
    constructor(fetch) {
        this.fetch = fetch;
    }
    async request(url, options) {
        const fetchOptions = {
            method: options.method,
            headers: options.headers,
            body: options.data
        };
        const res = await this.fetch(url, fetchOptions);
        const data = await res.text();
        return {
            ok: res.ok,
            status: res.status,
            statusText: res.statusText,
            data: data
        };
    }
}
export function binaryHttpClient() {
    const noHttpClient = {
        async request(..._) {
            throw new Error('No method available to perform HTTP request');
        }
    };
    if (globalThis.window !== undefined && typeof globalThis.window.fetch === 'function') {
        // Browser tab/page context
        return new BinaryFetchClient(globalThis.window.fetch.bind(globalThis.window));
    }
    else if (typeof globalThis.fetch === 'function') {
        // Service workers, Deno, Node 18+ (any environment with global fetch)
        return new BinaryFetchClient(globalThis.fetch.bind(globalThis));
    }
    else if (typeof require === 'undefined') {
        return noHttpClient;
    }
    else {
        // Older Node.js — use https module
        try {
            // eslint-disable-next-line @typescript-eslint/no-var-requires
            const https = require('node:https');
            return new BinaryNodejsHttpClient(https);
        }
        catch (_httpsModuleUnavailable) {
            // node:https not available in this runtime; fall through to noHttpClient
            return noHttpClient;
        }
    }
}
//# sourceMappingURL=BinaryFetchClient.js.map