"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getArkiverSubgraph = 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");
var urlForProtocol_1 = require("./urlForProtocol");
var subgraph_1 = require("./subgraph");
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_ARKIVER_API_KEY, PUBLIC_ARKIVER_API_KEY = _c === void 0 ? '' : _c;
var getServerUrl = function () {
    return PUBLIC_API + '/graphql';
};
var getArkiverSubgraph = function (vaultType) {
    switch (vaultType) {
        case 'arrakis':
            return "https://data.staging.arkiver.net/robolabs/arrakis-vaults-stga/graphql?apiKey=".concat(PUBLIC_ARKIVER_API_KEY);
        case 'defiedge':
            return "https://data.staging.arkiver.net/robolabs/defiedge-vaults/graphql?apiKey=".concat(PUBLIC_ARKIVER_API_KEY);
        default:
            return "https://data.staging.arkiver.net/robolabs/arrakis-vaults-stga/graphql?apiKey=".concat(PUBLIC_ARKIVER_API_KEY);
    }
};
exports.getArkiverSubgraph = getArkiverSubgraph;
function getLinks() {
    var uri = '';
    var setUri = new client_1.ApolloLink(function (op, forward) {
        var _a = op.getContext(), chain = _a.chain, server = _a.server, scionRewards = _a.scionRewards, uniV2Dex = _a.uniV2Dex, uniV3Dex = _a.uniV3Dex, arkiver = _a.arkiver;
        var uri = uniV3Dex
            ? (0, urlForProtocol_1.urlForProtocol)(uniV3Dex)
            : uniV2Dex
                ? (0, urlForProtocol_1.urlForUniv2)(uniV2Dex)
                : server
                    ? getServerUrl()
                    : scionRewards
                        ? (0, subgraph_1.getScionRewardsSubgraphUrl)(chain)
                        : arkiver
                            ? (0, exports.getArkiverSubgraph)(arkiver)
                            : (0, subgraph_1.getSectorSubgraphUrl)(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({
        delay: {
            // Starting with initialDelay, the delay of each subsequent retry is increased exponentially,
            // meaning it's multiplied by 2 each time. For example, if initialDelay is 100, additional
            // retries will occur after delays of 200, 400, 800, etc.
            initial: 500,
            max: Infinity,
            jitter: true,
        },
        attempts: {
            max: 3,
        },
    }).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