"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
    for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createStreamrGraphQLClient = exports.createQueryFetcher = void 0;
/**
 * GQty: You can safely modify this file based on your needs.
 */
const gqty_1 = require("gqty");
const graphql_ws_1 = require("graphql-ws");
const node_fetch_1 = require("node-fetch");
const schema_generated_1 = require("./schema.generated");
const createQueryFetcher = (url, fetcher) => async function ({ query, variables, operationName }, fetchOptions) {
    const response = await fetcher.fetch(url, {
        method: 'POST',
        headers: new node_fetch_1.Headers({
            'Content-Type': 'application/json',
        }),
        body: JSON.stringify({
            query,
            variables,
            operationName,
        }),
        ...fetchOptions,
    });
    if (response.status >= 400) {
        throw new gqty_1.GQtyError(`GraphQL endpoint responded with HTTP status ${response.status}.`);
    }
    const text = await response.text();
    try {
        return JSON.parse(text);
    }
    catch {
        throw new gqty_1.GQtyError(`Malformed JSON response: ${text.length > 50 ? text.slice(0, 50) + '...' : text}`);
    }
};
exports.createQueryFetcher = createQueryFetcher;
const subscriptionsClient = typeof window !== 'undefined'
    ? (0, graphql_ws_1.createClient)({
        lazy: true,
        url: () => {
            // Modify if needed
            const url = new URL('/api/graphql', window.location.href);
            url.protocol = url.protocol.replace('http', 'ws');
            return url.href;
        },
    })
    : undefined;
const cache = new gqty_1.Cache(undefined, 
/**
 * Default option is immediate cache expiry but keep it for 5 minutes,
 * allowing soft refetches in background.
 */
{
    maxAge: 0,
    staleWhileRevalidate: 5 * 60 * 1000,
    normalization: true,
});
const createStreamrGraphQLClient = (url, fetcher) => (0, gqty_1.createClient)({
    schema: schema_generated_1.generatedSchema,
    scalars: schema_generated_1.scalarsEnumsHash,
    cache,
    fetchOptions: {
        fetcher: (0, exports.createQueryFetcher)(url, fetcher),
        subscriber: subscriptionsClient,
        cache: 'no-store',
    },
});
exports.createStreamrGraphQLClient = createStreamrGraphQLClient;
__exportStar(require("./schema.generated"), exports);
//# sourceMappingURL=index.js.map