"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _api = _interopRequireDefault(require("./api"));
var _excluded = ["children"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
var DiscourseSearch = function DiscourseSearch(_ref) {
  var children = _ref.children,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useState = (0, _react.useState)([]),
    topics = _useState[0],
    setTopics = _useState[1];
  var searchTerm = rest.searchTerm,
    results = rest.results;
  (0, _react.useEffect)(function () {
    if (!searchTerm) return;
    var isSubscribed = true;
    var onSuccess = function onSuccess(_ref2) {
      var data = _ref2.data;
      return isSubscribed && setTopics(data);
    };
    (0, _api["default"])(searchTerm, onSuccess);
    return function () {
      return isSubscribed = false;
    };
  }, [searchTerm]);
  return children(_extends({}, rest, {
    results: _extends({}, results, {
      discourse: topics
    })
  }));
};
var _default = exports["default"] = DiscourseSearch;