"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.DeliveryAutosuggest = void 0;

var _react = _interopRequireDefault(require("react"));

var _ = require("../..");

/* eslint-disable react-hooks/exhaustive-deps */
var DeliveryAutosuggest = function DeliveryAutosuggest(_ref) {
  var _method$data, _method$data$selected, _method$data2, _method$data2$results, _method$data3, _method$data4;

  var method = _ref.method,
      label = _ref.label,
      minLength = _ref.minLength;

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

  var onChange = function 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["default"].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(function (item) {
      return item.id !== 'id';
    })) || [],
    getValue: function getValue(option) {
      return formatName(option.name);
    },
    getLabel: function getLabel(option) {
      return 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
  });
};

exports.DeliveryAutosuggest = DeliveryAutosuggest;

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