text: var __extends = (this && this.__extends) || (function () {
      var extendStatics = Object.setPrototypeOf ||
          ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
          function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
      return function (d, b) {
          extendStatics(d, b);
          function __() { this.constructor = d; }
          d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
      };
  })();
  var __assign = (this && this.__assign) || Object.assign || function(t) {
      for (var s, i = 1, n = arguments.length; i < n; i++) {
          s = arguments[i];
          for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
              t[p] = s[p];
      }
      return t;
  };
  import { Injectable } from '@angular/core';
  import { HttpClient, HttpParams } from '@angular/common/http';
  import { ApolloLink, Observable as LinkObservable, } from 'apollo-link';
  import { print } from 'graphql/language/printer';
  import { mergeHeaders, prioritize } from './utils';
  // XXX find a better name for it
  var HttpLinkHandler = /** @class */ (function (_super) {
      __extends(HttpLinkHandler, _super);
      function HttpLinkHandler(httpClient, options) {
          var _this = _super.call(this) || this;
          _this.httpClient = httpClient;
          _this.options = options;
          _this.requester = function (operation) {
              return new LinkObservable(function (observer) {
                  var context = operation.getContext();
                  // decides which value to pick, Context, Options or to just use the default
                  var pick = function (key, init) {
                      return prioritize(context[key], _this.options[key], init);
                  };
                  var includeQuery = pick('includeQuery', true);
                  var includeExtensions = pick('includeExtensions', false);
                  var method = pick('method', 'POST');
                  var url = pick('uri', 'graphql');
                  var withCredentials = pick('withCredentials');
                  var req = {
                      method: method,
                      url: url,
                      body: {
                          operationName: operation.operationName,
                          variables: operation.variables,
                      },
                      options: {
                          withCredentials: withCredentials,
                          headers: _this.options.headers,
                      },
                  };
                  if (includeExtensions) {
                      req.body.extensions = operation.extensions;
                  }
                  if (includeQuery) {
                      req.body.query = print(operation.query);
                  }
                  if (context.headers) {
                      req.options.headers = mergeHeaders(req.options.headers, context.headers);
                  }
                  var sub = _this.fetch(req).subscribe({
                      next: function (result) { return observer.next(result.body); },
                      error: function (err) { return observer.error(err); },
                      complete: function () { return observer.complete(); },
                  });
                  return function () {
                      if (!sub.closed) {
                          sub.unsubscribe();
                      }
                  };
              });
          };
          return _this;
      }
      HttpLinkHandler.prototype.request = function (op) {
          return this.requester(op);
      };
      // XXX make it as a separate package so it can be used in BatchLink
      HttpLinkHandler.prototype.fetch = function (req) {
          var shouldUseBody = ['POST', 'PUT', 'PATCH'].indexOf(req.method.toUpperCase()) !== -1;
          var shouldStringify = function (param) {
              return ['variables', 'extensions'].indexOf(param.toLowerCase()) !== -1;
          };
          // `body` for some, `params` for others
          var bodyOrParams = {};
          if (shouldUseBody) {
              bodyOrParams = {
                  body: req.body,
              };
          }
          else {
              var params = Object.keys(req.body).reduce(function (httpParams, param) {
                  var val = req.body[param];
                  if (shouldStringify(param.toLowerCase())) {
                      val = JSON.stringify(val);
                  }
                  return httpParams.set(param, val);
              }, new HttpParams());
              bodyOrParams = { params: params };
          }
          // create a request
          return this.httpClient.request(req.method, req.url, __assign({ observe: 'response', responseType: 'json', reportProgress: false }, bodyOrParams, req.options));
      };
      return HttpLinkHandler;
  }(ApolloLink));
  export { HttpLinkHandler };
  var HttpLink = /** @class */ (function () {
      function HttpLink(httpClient) {
          this.httpClient = httpClient;
      }
      HttpLink.prototype.create = function (options) {
          return new HttpLinkHandler(this.httpClient, options);
      };
      HttpLink.decorators = [
          { type: Injectable },
      ];
      /** @nocollapse */
      HttpLink.ctorParameters = function () { return [
          { type: HttpClient, },
      ]; };
      return HttpLink;
  }());
  export { HttpLink };
  //# sourceMappingURL=HttpLink.js.map