"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BinaryFetchClient = exports.BinaryNodejsHttpClient = void 0;
exports.binaryHttpClient = binaryHttpClient;
const NodejsHttpRequestUtils_js_1 = require("./NodejsHttpRequestUtils.js");
/**
 * Adapter for Node Https module to be used as HttpClient
 */
class BinaryNodejsHttpClient {
    constructor(https) {
        this.https = https;
    }
    async request(url, requestOptions) {
        return await (0, NodejsHttpRequestUtils_js_1.executeNodejsRequest)(this.https, url, requestOptions, (data) => Buffer.from(data));
    }
}
exports.BinaryNodejsHttpClient = BinaryNodejsHttpClient;
/**
 * Adapter for Node Https module to be used as HttpClient
 */
class BinaryFetchClient {
    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
        };
    }
}
exports.BinaryFetchClient = BinaryFetchClient;
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