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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

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);
  };

  return (0, _recompose.compose)(_reactApollo.withApollo, (0, _reactApollo.graphql)(query, {
    options: function options(props) {
      var params = config(props);
      var id = (0, _get.default)(params, "id", "");
      return {
        skip: id === "",
        variables: {
          where: {
            id: id
          }
        }
      };
    },
    props: function props(_ref2) {
      var data = _ref2.data,
          ownProps = _ref2.ownProps;
      var propName = getPropName(ownProps);
      var resultProps;

      if (responseFun) {
        // custom response
        resultProps = responseFun({
          data: data,
          ownProps: ownProps
        });
      } else {
        // default response
        resultProps = (0, _defineProperty2.default)({}, propName, {
          data: (0, _get.default)(data, queryName) || {},
          loading: data.loading,
          refetch: data.refetch
        });
      }

      return resultProps;
    }
  }), (0, _recompose.withProps)(function (props) {
    if (functionName) {
      var prop = (0, _defineProperty2.default)({}, functionName, function () {
        var _ref3 = (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 _ref3.apply(this, arguments);
        };
      }());
      return prop;
    }

    return props;
  }));
};

exports.default = _default;