var _excluded = ["children"];
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; }
import React, { useEffect, useState } from "react";
import fetchTopics from "./api";
var DiscourseSearch = function DiscourseSearch(_ref) {
  var children = _ref.children,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useState = useState([]),
    topics = _useState[0],
    setTopics = _useState[1];
  var searchTerm = rest.searchTerm,
    results = rest.results;
  useEffect(function () {
    if (!searchTerm) return;
    var isSubscribed = true;
    var onSuccess = function onSuccess(_ref2) {
      var data = _ref2.data;
      return isSubscribed && setTopics(data);
    };
    fetchTopics(searchTerm, onSuccess);
    return function () {
      return isSubscribed = false;
    };
  }, [searchTerm]);
  return children(_extends({}, rest, {
    results: _extends({}, results, {
      discourse: topics
    })
  }));
};
export default DiscourseSearch;