import { createMultipartFetcher, createSimpleFetcher, isSubscriptionWithName, getWsFetcher, } from './lib';
export function createGraphiQLFetcher(options) {
    let httpFetch;
    if (typeof window !== 'undefined' && window.fetch) {
        httpFetch = window.fetch;
    }
    if ((options === null || options === void 0 ? void 0 : options.enableIncrementalDelivery) === null ||
        options.enableIncrementalDelivery !== false) {
        options.enableIncrementalDelivery = true;
    }
    if (options.fetch) {
        httpFetch = options.fetch;
    }
    if (!httpFetch) {
        throw new Error('No valid fetcher implementation available');
    }
    const simpleFetcher = createSimpleFetcher(options, httpFetch);
    const httpFetcher = options.enableIncrementalDelivery
        ? createMultipartFetcher(options, httpFetch)
        : simpleFetcher;
    return (graphQLParams, fetcherOpts) => {
        if (graphQLParams.operationName === 'IntrospectionQuery') {
            return (options.schemaFetcher || simpleFetcher)(graphQLParams, fetcherOpts);
        }
        const isSubscription = (fetcherOpts === null || fetcherOpts === void 0 ? void 0 : fetcherOpts.documentAST)
            ? isSubscriptionWithName(fetcherOpts.documentAST, graphQLParams.operationName || undefined)
            : false;
        if (isSubscription) {
            const wsFetcher = getWsFetcher(options, fetcherOpts);
            if (!wsFetcher) {
                throw new Error(`Your GraphiQL createFetcher is not properly configured for websocket subscriptions yet. ${options.subscriptionUrl
                    ? `Provided URL ${options.subscriptionUrl} failed`
                    : 'Please provide subscriptionUrl, wsClient or legacyClient option first.'}`);
            }
            return wsFetcher(graphQLParams);
        }
        return httpFetcher(graphQLParams, fetcherOpts);
    };
}
//# sourceMappingURL=createFetcher.js.map