import _Select from "antd/es/select";
import React, { useState } from 'react';
import jsonp from 'fetch-jsonp';
import qs from 'qs';
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.stringify({
      code: 'utf-8',
      q: value
    });
    jsonp("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 = useState([]),
    data = _useState[0],
    setData = _useState[1];
  var _useState2 = 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.createElement(_Select, {
    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.createElement(SearchInput, {
    placeholder: "input search text",
    style: {
      width: 200
    }
  });
};
export default remoteSearch;