"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createClient = void 0;
var cross_fetch_1 = __importDefault(require("cross-fetch"));
var client_1 = require("@apollo/client");
var ws_1 = require("@apollo/client/link/ws");
var utilities_1 = require("@apollo/client/utilities");
var retry_1 = require("@apollo/client/link/retry");
function createClient(options) {
    if (options === void 0) { options = {}; }
    var cache = options.cache || new client_1.InMemoryCache({});
    if (typeof window !== 'undefined') {
        // @ts-ignore
        cache.restore(window.__APOLLO_STATE__);
    }
    var link = getLinks();
    return new client_1.ApolloClient({
        connectToDevTools: true,
        cache: cache,
        link: link,
    });
}
exports.createClient = createClient;
// set default here for deployment to prod
var _a = process.env, _b = _a.PUBLIC_API, PUBLIC_API = _b === void 0 ? '' : _b, _c = _a.PUBLIC_SUBGRAPH_SUFFIX, PUBLIC_SUBGRAPH_SUFFIX = _c === void 0 ? '' : _c, PUBLIC_SUBGRAPH = _a.PUBLIC_SUBGRAPH, _d = _a.PUBLIC_FORK_CHAIN, PUBLIC_FORK_CHAIN = _d === void 0 ? 'mainnet' : _d;
var getScionSubgraphUrl = function (chain) {
    var subgraphName = chain;
    switch (subgraphName) {
        case 'localhost':
            if (PUBLIC_SUBGRAPH === 'localhost')
                return "http://localhost:8000/subgraphs/name/sector-fi/sector-".concat(PUBLIC_SUBGRAPH);
            else
                return "https://api.thegraph.com/subgraphs/name/sector-fi/sector-".concat(PUBLIC_FORK_CHAIN).concat(PUBLIC_SUBGRAPH_SUFFIX);
        default:
            return "https://api.thegraph.com/subgraphs/name/sector-fi/sector-".concat(subgraphName).concat(PUBLIC_SUBGRAPH_SUFFIX);
    }
};
var getSubgraphUrl = function (subgraph) {
    switch (subgraph) {
        case 'uniswapV2':
            return "https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2";
        default:
            throw new Error("Missing subgraph url for ".concat(subgraph));
    }
};
var getSubgraphHealthUrl = function () {
    return "https://api.thegraph.com/index-node/graphql";
};
var getServerUrl = function () {
    return PUBLIC_API + '/graphql';
};
function getLinks() {
    var uri = '';
    var setUri = new client_1.ApolloLink(function (op, forward) {
        var _a = op.getContext(), chain = _a.chain, subgraph = _a.subgraph, health = _a.health, server = _a.server;
        var uri = subgraph
            ? getSubgraphUrl(subgraph)
            : health // This means subgraph health, not server health.
                ? getSubgraphHealthUrl()
                : server
                    ? getServerUrl()
                    : getScionSubgraphUrl(chain);
        op.setContext({ uri: uri });
        return forward(op);
    });
    var httpLink = new client_1.HttpLink({ uri: "".concat(uri), fetch: cross_fetch_1.default });
    var wsLink = getWsLink(uri);
    if (!wsLink)
        return (0, client_1.from)([setUri, httpLink]);
    return new retry_1.RetryLink().split(
    // split based on operation type
    function (_a) {
        var query = _a.query;
        var definition = (0, utilities_1.getMainDefinition)(query);
        return (definition.kind === 'OperationDefinition' &&
            definition.operation === 'subscription');
    }, (0, client_1.from)([setUri, wsLink]), (0, client_1.from)([setUri, httpLink]));
}
function getWsLink(uri) {
    try {
        var wsUri = uri.replace('http', 'ws');
        var wsLink = new ws_1.WebSocketLink({
            uri: "".concat(wsUri),
            options: {
                reconnect: true,
                reconnectionAttempts: 50,
                lazy: true,
                timeout: 20000,
            },
        });
        return wsLink;
    }
    catch (err) {
        return null;
    }
}
//# sourceMappingURL=apolloClient.js.map