"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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 _react = _interopRequireWildcard(require("react"));

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

var _debounce = _interopRequireDefault(require("lodash/debounce"));

var _reactInstantsearchDom = require("react-instantsearch-dom");

var _Input = _interopRequireDefault(require("../../../../components/Input"));

var _useReadmeApi2 = _interopRequireDefault(require("../../../../../core/hooks/useReadmeApi"));

// 3rd-party libs
// Components
// Internals
// Style
var classes = {};
/* istanbul ignore next */

if (typeof window !== 'undefined') classes = require("./style.module.scss");

var CustomSearchBox = function CustomSearchBox(_ref) {
  var currentRefinement = _ref.currentRefinement,
      isSearchStalled = _ref.isSearchStalled,
      refine = _ref.refine;

  var _useState = (0, _react.useState)(''),
      _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
      query = _useState2[0],
      setQuery = _useState2[1];

  var _useReadmeApi = (0, _useReadmeApi2["default"])('https://metrics.readme.io'),
      initRequest = _useReadmeApi.initRequest; // delay to improve performance


  var _onChange = (0, _debounce["default"])(function (value) {
    return refine(value);
  }, 300);

  var publishQueryToMetrics = function publishQueryToMetrics(search) {
    var readmeSubdomain = typeof window !== 'undefined' && document.querySelector('meta[name=readme-subdomain]');

    if (readmeSubdomain && query !== search) {
      setQuery(search);
      initRequest({
        path: 'search',
        headers: {
          'Content-Type': 'application/json'
        },
        method: 'POST',
        body: JSON.stringify({
          search: search,
          subdomain: readmeSubdomain.getAttribute('content')
        })
      });
    }
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: classes.SearchBox
  }, /*#__PURE__*/_react["default"].createElement(_Input["default"], {
    autoCapitalize: "off",
    autoComplete: "off",
    autoCorrect: "off",
    autoFocus: true,
    className: classes['SearchBox-Input'],
    onBlur: function onBlur(e) {
      return publishQueryToMetrics(e.currentTarget.value);
    },
    onChange: function onChange(e) {
      return _onChange(e.currentTarget.value);
    },
    spellCheck: "false",
    tabIndex: 0,
    type: "search",
    value: currentRefinement
  }), !isSearchStalled && /*#__PURE__*/_react["default"].createElement("i", {
    className: [classes['SearchBox-Search'], 'fa fa-search'].join(' ')
  }), isSearchStalled && /*#__PURE__*/_react["default"].createElement("i", {
    className: classes['SearchBox-Spinner']
  }));
};

var SearchBox = (0, _reactInstantsearchDom.connectSearchBox)(CustomSearchBox);
CustomSearchBox.propTypes = {
  currentRefinement: _propTypes["default"].string,
  // Algolia says this should be boolean, but sometimes it’s undefined
  isSearchStalled: _propTypes["default"].any,
  refine: _propTypes["default"].func
};
var _default = SearchBox;
exports["default"] = _default;