/* eslint-disable react-hooks/exhaustive-deps */
import React from 'react';
import { Autosuggest } from '../..';
export var DeliveryAutosuggest = (_ref) => {
  var _method$data, _method$data$selected, _method$data2, _method$data2$results, _method$data3, _method$data4;

  var {
    method,
    label,
    minLength
  } = _ref;

  var fetchItems = query => {
    var searchQuery = {
      text: query
    };
    method == null ? void 0 : method.find(searchQuery);
  };

  var onChange = value => {
    if (!value) return method == null ? void 0 : method.unset();
    method == null ? void 0 : method.set(value);
  };

  var selected = formatName(method == null ? void 0 : (_method$data = method.data) == null ? void 0 : (_method$data$selected = _method$data.selected) == null ? void 0 : _method$data$selected.name);
  return /*#__PURE__*/React.createElement(Autosuggest, {
    options: (method == null ? void 0 : (_method$data2 = method.data) == null ? void 0 : (_method$data2$results = _method$data2.results) == null ? void 0 : _method$data2$results.filter(item => item.id !== 'id')) || [],
    getValue: option => formatName(option.name),
    getLabel: option => formatName(option.name),
    withRemoveIconWhenSelected: true,
    onRemove: method == null ? void 0 : method.unset,
    isFetching: Boolean(method == null ? void 0 : (_method$data3 = method.data) == null ? void 0 : _method$data3.isLoading),
    onOptionSelect: onChange,
    updateOptions: fetchItems,
    selectedOption: selected,
    disabled: method == null ? void 0 : (_method$data4 = method.data) == null ? void 0 : _method$data4.isDisabled,
    label: label,
    minLength: minLength
  });
};

function formatName(str) {
  if (!str) return '';
  return str.toLowerCase().split(' ').map(s => s.charAt(0).toUpperCase() + s.slice(1)).join(' ');
}
//# sourceMappingURL=index.js.map