"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createHasuraSubscriptionsClient = createHasuraSubscriptionsClient;
const subscriptions_1 = require("@gqty/subscriptions");
const constants_1 = require("../constants");
// Create subscriptions client
function createHasuraSubscriptionsClient(uri, getHeaders, enabled = true) {
    // Ensure we strictly check for false, not falsy values
    if (enabled === false) {
        // We must return undefined here, not a client when disabled
        return undefined;
    }
    try {
        const wsUrl = new URL(uri);
        wsUrl.protocol = wsUrl.protocol
            .replace('https', 'wss')
            .replace('http', 'ws');
        return (0, subscriptions_1.createSubscriptionsClient)({
            wsEndpoint: wsUrl.href,
            connectionInitPayload: async () => ({
                headers: await getHeaders({ [constants_1.hasuraQueryTypeHeader]: 'subscription' }),
            }),
        });
    }
    catch (error) {
        console.error('Failed to create subscriptions client:', error);
        return undefined;
    }
}
