"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _crossFetch = _interopRequireDefault(require("cross-fetch"));

var _apolloLinkHttp = require("apollo-link-http");

var _get = _interopRequireDefault(require("lodash/get"));

var _apolloLink = require("apollo-link");

var GraphQLRequest =
/*#__PURE__*/
function () {
  function GraphQLRequest() {
    var _this = this;

    (0, _classCallCheck2.default)(this, GraphQLRequest);
    var httpLink = (0, _apolloLinkHttp.createHttpLink)({
      //As sometimes process.env.GRAPHQL_API_URL results in undefined, Optional Url is added
      uri: process.env.GRAPHQL_API_URL || "http://localhost:4000/api",
      fetch: _crossFetch.default
    });
    var middlewareLink = new _apolloLink.ApolloLink(function (operation, forward) {
      var token = _this.userToken;
      var companyId = _this.companyToken;
      var headers = {};

      if (token) {
        headers["x-user-auth"] = "".concat(token);
      }

      if (companyId) {
        headers["x-company-auth"] = "".concat(companyId);
      }

      operation.setContext({
        headers: headers
      });
      return forward(operation);
    });
    this.link = _apolloLink.ApolloLink.from([middlewareLink, httpLink]);
  }

  (0, _createClass2.default)(GraphQLRequest, [{
    key: "call",
    value: function call(query, variables) {
      return (0, _apolloLink.makePromise)((0, _apolloLink.execute)(this.link, {
        query: query,
        variables: variables
      }));
    }
  }, {
    key: "getNode",
    value: function () {
      var _getNode = (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee(name, query, id) {
        var res;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.prev = 0;
                _context.next = 3;
                return this.call(query, {
                  where: {
                    id: id
                  }
                });

              case 3:
                res = _context.sent;

                if (!(0, _get.default)(res, "errors")) {
                  _context.next = 8;
                  break;
                }

                return _context.abrupt("return", {
                  errors: (0, _get.default)(res, "errors")
                });

              case 8:
                return _context.abrupt("return", (0, _get.default)(res, "data.".concat(name)));

              case 9:
                _context.next = 14;
                break;

              case 11:
                _context.prev = 11;
                _context.t0 = _context["catch"](0);
                return _context.abrupt("return", {
                  errors: _context.t0
                });

              case 14:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this, [[0, 11]]);
      }));

      return function getNode(_x, _x2, _x3) {
        return _getNode.apply(this, arguments);
      };
    }()
  }, {
    key: "getAll",
    value: function () {
      var _getAll = (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee2(name, query, variables) {
        var res, edges, nodes;
        return _regenerator.default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.prev = 0;
                _context2.next = 3;
                return this.call(query, variables);

              case 3:
                res = _context2.sent;

                if (!(0, _get.default)(res, "errors")) {
                  _context2.next = 8;
                  break;
                }

                return _context2.abrupt("return", {
                  errors: (0, _get.default)(res, "errors")
                });

              case 8:
                edges = (0, _get.default)(res, "data.".concat(name, ".edges"), []);
                nodes = edges.map(function (_ref) {
                  var node = _ref.node;
                  return node;
                });
                return _context2.abrupt("return", {
                  nodes: nodes,
                  pageInfo: (0, _get.default)(res, "data.".concat(name, ".pageInfo"), {}),
                  aggregate: (0, _get.default)(res, "data.".concat(name, ".aggregate"))
                });

              case 11:
                _context2.next = 16;
                break;

              case 13:
                _context2.prev = 13;
                _context2.t0 = _context2["catch"](0);
                return _context2.abrupt("return", {
                  errors: _context2.t0
                });

              case 16:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, this, [[0, 13]]);
      }));

      return function getAll(_x4, _x5, _x6) {
        return _getAll.apply(this, arguments);
      };
    }()
  }, {
    key: "userToken",
    get: function get() {
      return this._userToken;
    },
    set: function set(token) {
      this._userToken = token;
    }
  }, {
    key: "companyToken",
    get: function get() {
      return this._companyToken;
    },
    set: function set(token) {
      this._companyToken = token;
    }
  }]);
  return GraphQLRequest;
}();

var _default = new GraphQLRequest();

exports.default = _default;