"use strict";

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

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

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

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

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

var _react = require("react");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function fetchWrapper(baseUrl, _ref) {
  var path = _ref.path,
      headers = _ref.headers,
      options = (0, _objectWithoutProperties2["default"])(_ref, ["path", "headers"]);
  return fetch("".concat(baseUrl, "/").concat(path), _objectSpread(_objectSpread({}, options), {}, {
    headers: _objectSpread({
      'Content-Type': 'application/json'
    }, headers)
  })).then(function (response) {
    if (response.ok) return response.json();
    return response.text().then(function (err) {
      throw new Error(err);
    });
  });
}

function useReadmeApi(baseUrl) {
  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
      pendingRequest = _useState2[0],
      setPendingRequest = _useState2[1];

  var _useState3 = (0, _react.useState)(null),
      _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
      request = _useState4[0],
      initRequest = _useState4[1];

  var _useState5 = (0, _react.useState)({}),
      _useState6 = (0, _slicedToArray2["default"])(_useState5, 2),
      response = _useState6[0],
      setResponse = _useState6[1];

  var _useState7 = (0, _react.useState)(null),
      _useState8 = (0, _slicedToArray2["default"])(_useState7, 2),
      error = _useState8[0],
      setError = _useState8[1];

  (0, _react.useEffect)(function () {
    if (request) {
      fetchWrapper(baseUrl, request).then(function (json) {
        setPendingRequest(!!json.success);
        setResponse(json);
      })["catch"](function (_ref2) {
        var message = _ref2.message;
        setError(message);
      });
    }
  }, [request]);
  return {
    response: response,
    error: error,
    setError: setError,
    pendingRequest: pendingRequest,
    initRequest: initRequest
  };
}

var _default = useReadmeApi;
exports["default"] = _default;