"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 _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));

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

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

var _react = _interopRequireDefault(require("react"));

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

var _reactApollo = require("react-apollo");

var _recompose = require("recompose");

var _default = function _default(_ref) {
  var _ref$config = _ref.config,
      config = _ref$config === void 0 ? function () {} : _ref$config,
      responseFun = _ref.responseFun,
      queryName = _ref.queryName,
      query = _ref.query,
      _ref$defaultPropName = _ref.defaultPropName,
      defaultPropName = _ref$defaultPropName === void 0 ? "data" : _ref$defaultPropName,
      functionName = _ref.functionName;

  var getPropName = function getPropName(props) {
    var params = config(props);
    return (0, _get.default)(params, "name", defaultPropName);
  };

  var handleResult = function handleResult(_ref2) {
    var data = _ref2.data,
        ownProps = _ref2.ownProps;
    var edges = (0, _get.default)(data, "".concat(queryName, ".edges"), []);
    var nodes = edges.map(function (_ref3) {
      var node = _ref3.node;
      return node;
    });
    var pageInfo = (0, _get.default)(data, "".concat(queryName, ".pageInfo"), {});
    var count = (0, _get.default)(data, "".concat(queryName, ".aggregate.count"), 0);
    return {
      data: nodes,
      pageInfo: pageInfo,
      count: count
    };
  };

  var handlePaginationResponse = function handlePaginationResponse(previousResult, _ref4) {
    var fetchMoreResult = _ref4.fetchMoreResult;
    var result1 = (0, _get.default)(fetchMoreResult, "".concat(queryName, ".edges")) || [];
    var result2 = (0, _get.default)(previousResult, "".concat(queryName, ".edges")) || [];
    var finalEdges = (0, _toConsumableArray2.default)(result2).concat((0, _toConsumableArray2.default)(result1));
    var pageInfo = (0, _get.default)(fetchMoreResult, "".concat(queryName, ".pageInfo"));
    return (0, _defineProperty2.default)({}, queryName, {
      __typename: (0, _get.default)(previousResult, "".concat(queryName, ".__typename")),
      edges: finalEdges,
      aggregate: {
        count: finalEdges.length,
        __typename: (0, _get.default)(previousResult, "".concat(queryName, ".aggregate.__typename"))
      },
      pageInfo: pageInfo
    });
  };

  return (0, _recompose.compose)(_reactApollo.withApollo, (0, _reactApollo.graphql)(query, {
    options: function options(props) {
      var params = config(props);
      return {
        variables: (0, _get.default)(params, "variables"),
        skip: (0, _get.default)(params, "skip", false) === true,
        notifyOnNetworkStatusChange: true
      };
    },
    props: function props(_ref6) {
      var data = _ref6.data,
          ownProps = _ref6.ownProps;
      var resultProps = {};

      if (responseFun) {
        // custom response
        resultProps = responseFun({
          data: data,
          ownProps: ownProps
        });
      } else {
        // default response
        var propName = getPropName(ownProps);
        var pageInfo = (0, _get.default)(data, "".concat(queryName, ".pageInfo"), {});
        var resultData = handleResult({
          data: data,
          ownProps: ownProps
        });
        resultProps = (0, _defineProperty2.default)({}, propName, (0, _objectSpread2.default)({
          loading: data.loading,
          refetch: data.refetch,
          error: data.error
        }, resultData, {
          loadNextPage: function loadNextPage(options) {
            if (!pageInfo.hasNextPage) {
              console.log("no next pages");
              return;
            }

            data.fetchMore({
              variables: (0, _objectSpread2.default)({}, options, {
                after: pageInfo.endCursor
              }),
              updateQuery: handlePaginationResponse
            });
          }
        }));
      }

      return resultProps;
    }
  }), (0, _recompose.withProps)(function (props) {
    if (functionName) {
      var prop = (0, _defineProperty2.default)({}, functionName, function () {
        var _ref7 = (0, _asyncToGenerator2.default)(
        /*#__PURE__*/
        _regenerator.default.mark(function _callee(options) {
          return _regenerator.default.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  _context.next = 2;
                  return props.client.query({
                    query: query,
                    variables: options.variables
                  });

                case 2:
                  return _context.abrupt("return", _context.sent);

                case 3:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee, this);
        }));

        return function (_x) {
          return _ref7.apply(this, arguments);
        };
      }());
      return prop;
    }

    return props;
  }));
};

exports.default = _default;