"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _select = _interopRequireDefault(require("antd/lib/select"));
var _react = _interopRequireWildcard(require("react"));
var _fetchJsonp = _interopRequireDefault(require("fetch-jsonp"));
var _qs = _interopRequireDefault(require("qs"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
var timeout;
var currentValue;
var fetch = function fetch(value, callback) {
  if (timeout) {
    clearTimeout(timeout);
    timeout = null;
  }
  currentValue = value;
  var fake = function fake() {
    var str = _qs["default"].stringify({
      code: 'utf-8',
      q: value
    });
    (0, _fetchJsonp["default"])("https://suggest.taobao.com/sug?" + str).then(function (response) {
      return response.json();
    }).then(function (d) {
      if (currentValue === value) {
        var result = d.result;
        var data = result.map(function (item) {
          return {
            value: item[0],
            text: item[0]
          };
        });
        callback(data);
      }
    });
  };
  timeout = setTimeout(fake, 300);
};
var SearchInput = function SearchInput(props) {
  var _useState = (0, _react.useState)([]),
    data = _useState[0],
    setData = _useState[1];
  var _useState2 = (0, _react.useState)(),
    value = _useState2[0],
    setValue = _useState2[1];
  var handleSearch = function handleSearch(newValue) {
    if (newValue) {
      fetch(newValue, setData);
    } else {
      setData([]);
    }
  };
  var handleChange = function handleChange(newValue) {
    setValue(newValue);
  };
  return /*#__PURE__*/_react["default"].createElement(_select["default"], {
    showSearch: true,
    value: value,
    placeholder: props.placeholder,
    style: props.style,
    defaultActiveFirstOption: false,
    showArrow: false,
    filterOption: false,
    onSearch: handleSearch,
    onChange: handleChange,
    notFoundContent: null,
    options: (data || []).map(function (d) {
      return {
        value: d.value,
        label: d.text
      };
    })
  });
};
var remoteSearch = function remoteSearch() {
  return /*#__PURE__*/_react["default"].createElement(SearchInput, {
    placeholder: "input search text",
    style: {
      width: 200
    }
  });
};
var _default = remoteSearch;
exports["default"] = _default;