const GraphQLClient = require("graphql-request").GraphQLClient;

//New bug introduced with Graphql Request.
// https://github.com/prisma-labs/graphql-request/issues/206
// const { Headers } = require("cross-fetch");
// global.Headers = global.Headers || Headers;

const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
  headers: {
    "x-hasura-admin-secret": process.env.HASURA_ADMIN_SECRET
  }
});

const rpsClient =
  process.env.RPS_GRAPHQL_ENDPOINT && process.env.RPS_HASURA_ADMIN_SECRET
    ? new GraphQLClient(process.env.RPS_GRAPHQL_ENDPOINT, {
        headers: {
          "x-hasura-admin-secret": process.env.RPS_HASURA_ADMIN_SECRET
        }
      })
    : null;

if (!rpsClient) {
  //System log to disable RPS functions

  console.log(`RPS secrets are not set. Client is not configured.`, "WARN", "redis", "api", {});
}

const unauthorizedClient = new GraphQLClient(process.env.GRAPHQL_ENDPOINT);

module.exports = {
  client,
  rpsClient,
  unauthorizedClient
};
