import * as React from 'react';
import PrimeReact from 'primereact/api';
import { Button } from 'primereact/button';
import { useOverlayListener, useMountEffect, useUpdateEffect, useUnmountEffect } from 'primereact/hooks';
import { InputText } from 'primereact/inputtext';
import { OverlayService } from 'primereact/overlayservice';
import { Tooltip } from 'primereact/tooltip';
import { classNames, ObjectUtils, UniqueComponentId, ZIndexUtils, DomHandler, IconUtils } from 'primereact/utils';
import { CSSTransition } from 'primereact/csstransition';
import { Portal } from 'primereact/portal';
import { Ripple } from 'primereact/ripple';
import { VirtualScroller } from 'primereact/virtualscroller';

function _extends() {
  _extends = Object.assign || function (target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];

      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }

    return target;
  };

  return _extends.apply(this, arguments);
}

function _arrayLikeToArray(arr, len) {
  if (len == null || len > arr.length) len = arr.length;

  for (var i = 0, arr2 = new Array(len); i < len; i++) {
    arr2[i] = arr[i];
  }

  return arr2;
}

function _arrayWithoutHoles(arr) {
  if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}

function _iterableToArray(iter) {
  if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}

function _unsupportedIterableToArray(o, minLen) {
  if (!o) return;
  if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  var n = Object.prototype.toString.call(o).slice(8, -1);
  if (n === "Object" && o.constructor) n = o.constructor.name;
  if (n === "Map" || n === "Set") return Array.from(o);
  if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}

function _nonIterableSpread() {
  throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}

function _toConsumableArray(arr) {
  return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
}

function _arrayWithHoles(arr) {
  if (Array.isArray(arr)) return arr;
}

function _iterableToArrayLimit(arr, i) {
  var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];

  if (_i == null) return;
  var _arr = [];
  var _n = true;
  var _d = false;

  var _s, _e;

  try {
    for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
      _arr.push(_s.value);

      if (i && _arr.length === i) break;
    }
  } catch (err) {
    _d = true;
    _e = err;
  } finally {
    try {
      if (!_n && _i["return"] != null) _i["return"]();
    } finally {
      if (_d) throw _e;
    }
  }

  return _arr;
}

function _nonIterableRest() {
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}

function _slicedToArray(arr, i) {
  return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var AutoCompletePanel = /*#__PURE__*/React.memo( /*#__PURE__*/React.forwardRef(function (props, ref) {
  var getOptionGroupRenderKey = function getOptionGroupRenderKey(optionGroup) {
    return ObjectUtils.resolveFieldData(optionGroup, props.optionGroupLabel);
  };

  var createGroupChildren = function createGroupChildren(optionGroup, i) {
    var groupChildren = props.getOptionGroupChildren(optionGroup);
    return groupChildren.map(function (item, j) {
      var key = i + '_' + j;
      var selected = props.selectedItem === item;
      var content = props.itemTemplate ? ObjectUtils.getJSXElement(props.itemTemplate, item, j) : props.field ? ObjectUtils.resolveFieldData(item, props.field) : item;
      return /*#__PURE__*/React.createElement("li", {
        key: key,
        role: "option",
        "aria-selected": selected,
        className: "p-autocomplete-item",
        onClick: function onClick(e) {
          return props.onItemClick(e, item);
        },
        "data-group": i,
        "data-index": j
      }, content, /*#__PURE__*/React.createElement(Ripple, null));
    });
  };

  var createItem = function createItem(suggestion, index) {
    if (props.optionGroupLabel) {
      var content = props.optionGroupTemplate ? ObjectUtils.getJSXElement(props.optionGroupTemplate, suggestion, index) : props.getOptionGroupLabel(suggestion);
      var childrenContent = createGroupChildren(suggestion, index);
      var key = index + '_' + getOptionGroupRenderKey(suggestion);
      return /*#__PURE__*/React.createElement(React.Fragment, {
        key: key
      }, /*#__PURE__*/React.createElement("li", {
        className: "p-autocomplete-item-group"
      }, content), childrenContent);
    } else {
      var _content = props.itemTemplate ? ObjectUtils.getJSXElement(props.itemTemplate, suggestion, index) : props.field ? ObjectUtils.resolveFieldData(suggestion, props.field) : suggestion;

      return /*#__PURE__*/React.createElement("li", {
        key: index,
        role: "option",
        "aria-selected": props.selectedItem === suggestion,
        className: "p-autocomplete-item",
        onClick: function onClick(e) {
          return props.onItemClick(e, suggestion);
        }
      }, _content, /*#__PURE__*/React.createElement(Ripple, null));
    }
  };

  var createItems = function createItems() {
    return props.suggestions ? props.suggestions.map(createItem) : null;
  };

  var createContent = function createContent() {
    if (props.virtualScrollerOptions) {
      var virtualScrollerProps = _objectSpread(_objectSpread({}, props.virtualScrollerOptions), {
        style: _objectSpread(_objectSpread({}, props.virtualScrollerOptions.style), {
          height: props.scrollHeight
        }),
        items: props.suggestions,
        itemTemplate: function itemTemplate(item, options) {
          return item && createItem(item, options.index);
        },
        contentTemplate: function contentTemplate(options) {
          var className = classNames('p-autocomplete-items', options.className);
          return /*#__PURE__*/React.createElement("ul", {
            ref: options.contentRef,
            className: className,
            role: "listbox",
            id: props.listId
          }, options.children);
        }
      });

      return /*#__PURE__*/React.createElement(VirtualScroller, _extends({
        ref: props.virtualScrollerRef
      }, virtualScrollerProps));
    } else {
      var items = createItems();
      return /*#__PURE__*/React.createElement("ul", {
        className: "p-autocomplete-items",
        role: "listbox",
        id: props.listId
      }, items);
    }
  };

  var createElement = function createElement() {
    var className = classNames('p-autocomplete-panel p-component', props.panelClassName);

    var style = _objectSpread({
      maxHeight: props.scrollHeight
    }, props.panelStyle || {});

    var content = createContent();
    return /*#__PURE__*/React.createElement(CSSTransition, {
      nodeRef: ref,
      classNames: "p-connected-overlay",
      "in": props["in"],
      timeout: {
        enter: 120,
        exit: 100
      },
      options: props.transitionOptions,
      unmountOnExit: true,
      onEnter: props.onEnter,
      onEntering: props.onEntering,
      onEntered: props.onEntered,
      onExit: props.onExit,
      onExited: props.onExited
    }, /*#__PURE__*/React.createElement("div", {
      ref: ref,
      className: className,
      style: style,
      onClick: props.onClick
    }, content));
  };

  var element = createElement();
  return /*#__PURE__*/React.createElement(Portal, {
    element: element,
    appendTo: props.appendTo
  });
}));
AutoCompletePanel.displayName = 'AutoCompletePanel';

var AutoComplete = /*#__PURE__*/React.memo( /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _React$useState = React.useState(props.id),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      idState = _React$useState2[0],
      setIdState = _React$useState2[1];

  var _React$useState3 = React.useState(false),
      _React$useState4 = _slicedToArray(_React$useState3, 2),
      searchingState = _React$useState4[0],
      setSearchingState = _React$useState4[1];

  var _React$useState5 = React.useState(false),
      _React$useState6 = _slicedToArray(_React$useState5, 2),
      focusedState = _React$useState6[0],
      setFocusedState = _React$useState6[1];

  var _React$useState7 = React.useState(false),
      _React$useState8 = _slicedToArray(_React$useState7, 2),
      overlayVisibleState = _React$useState8[0],
      setOverlayVisibleState = _React$useState8[1];

  var elementRef = React.useRef(null);
  var overlayRef = React.useRef(null);
  var inputRef = React.useRef(props.inputRef);
  var multiContainerRef = React.useRef(null);
  var virtualScrollerRef = React.useRef(null);
  var timeout = React.useRef(null);
  var selectedItem = React.useRef(null);

  var _useOverlayListener = useOverlayListener({
    target: elementRef,
    overlay: overlayRef,
    listener: function listener(event, _ref) {
      var type = _ref.type,
          valid = _ref.valid;

      if (valid) {
        type === 'outside' ? !isInputClicked(event) && hide() : hide();
      }
    },
    when: overlayVisibleState
  }),
      _useOverlayListener2 = _slicedToArray(_useOverlayListener, 2),
      bindOverlayListener = _useOverlayListener2[0],
      unbindOverlayListener = _useOverlayListener2[1];

  var isInputClicked = function isInputClicked(event) {
    return props.multiple ? event.target === multiContainerRef.current || multiContainerRef.current.contains(event.target) : event.target === inputRef.current;
  };

  var onInputChange = function onInputChange(event) {
    //Cancel the search request if user types within the timeout
    if (timeout.current) {
      clearTimeout(timeout.current);
    }

    var query = event.target.value;

    if (!props.multiple) {
      updateModel(event, query);
    }

    if (ObjectUtils.isEmpty(query)) {
      hide();
      props.onClear && props.onClear(event);
    } else {
      if (query.length >= props.minLength) {
        timeout.current = setTimeout(function () {
          search(event, query, 'input');
        }, props.delay);
      } else {
        hide();
      }
    }
  };

  var search = function search(event, query, source) {
    //allow empty string but not undefined or null
    if (query === undefined || query === null) {
      return;
    } //do not search blank values on input change


    if (source === 'input' && query.trim().length === 0) {
      return;
    }

    if (props.completeMethod) {
      setSearchingState(true);
      props.completeMethod({
        originalEvent: event,
        query: query
      });
    }
  };

  var selectItem = function selectItem(event, option, preventInputFocus) {
    if (props.multiple) {
      inputRef.current.value = '';

      if (!isSelected(option)) {
        var newValue = props.value ? [].concat(_toConsumableArray(props.value), [option]) : [option];
        updateModel(event, newValue);
      }
    } else {
      updateInputField(option);
      updateModel(event, option);
    }

    if (props.onSelect) {
      props.onSelect({
        originalEvent: event,
        value: option
      });
    }

    if (!preventInputFocus) {
      inputRef.current.focus();
      hide();
    }
  };

  var updateModel = function updateModel(event, value) {
    // #2176 only call change if value actually changed
    if (selectedItem && selectedItem.current === value) {
      return;
    }

    if (props.onChange) {
      props.onChange({
        originalEvent: event,
        value: value,
        stopPropagation: function stopPropagation() {},
        preventDefault: function preventDefault() {},
        target: {
          name: props.name,
          id: idState,
          value: value
        }
      });
    }

    selectedItem.current = value;
  };

  var formatValue = function formatValue(value) {
    if (value) {
      if (props.selectedItemTemplate && (props.multiple ? isSelected(value) : findOptionIndex(value) > -1)) {
        var resolvedFieldData = ObjectUtils.getJSXElement(props.selectedItemTemplate, value);
        return resolvedFieldData ? resolvedFieldData : value;
      } else if (props.field) {
        var _resolvedFieldData = ObjectUtils.resolveFieldData(value, props.field);

        return _resolvedFieldData !== null && _resolvedFieldData !== undefined ? _resolvedFieldData : value;
      } else {
        return value;
      }
    }

    return '';
  };

  var updateInputField = function updateInputField(value) {
    inputRef.current.value = formatValue(value);
  };

  var show = function show() {
    setOverlayVisibleState(true);
  };

  var hide = function hide() {
    setOverlayVisibleState(false);
    setSearchingState(false);
  };

  var onOverlayEnter = function onOverlayEnter() {
    ZIndexUtils.set('overlay', overlayRef.current, PrimeReact.autoZIndex, PrimeReact.zIndex['overlay']);
    alignOverlay();
  };

  var onOverlayEntering = function onOverlayEntering() {
    if (props.autoHighlight && props.suggestions && props.suggestions.length) {
      DomHandler.addClass(overlayRef.current.firstChild.firstChild, 'p-highlight');
    }
  };

  var onOverlayEntered = function onOverlayEntered() {
    bindOverlayListener();
    props.onShow && props.onShow();
  };

  var onOverlayExit = function onOverlayExit() {
    unbindOverlayListener();
  };

  var onOverlayExited = function onOverlayExited() {
    ZIndexUtils.clear(overlayRef.current);
    props.onHide && props.onHide();
  };

  var alignOverlay = function alignOverlay() {
    var target = props.multiple ? multiContainerRef.current : inputRef.current;
    DomHandler.alignOverlay(overlayRef.current, target, props.appendTo || PrimeReact.appendTo);
  };

  var onPanelClick = function onPanelClick(event) {
    OverlayService.emit('overlay-click', {
      originalEvent: event,
      target: elementRef.current
    });
  };

  var onDropdownClick = function onDropdownClick(event) {
    if (props.dropdownAutoFocus) {
      inputRef.current.focus();
    }

    if (props.dropdownMode === 'blank') search(event, '', 'dropdown');else if (props.dropdownMode === 'current') search(event, inputRef.current.value, 'dropdown');

    if (props.onDropdownClick) {
      props.onDropdownClick({
        originalEvent: event,
        query: inputRef.current.value
      });
    }
  };

  var removeItem = function removeItem(event, index) {
    var removedValue = props.value[index];
    var newValue = props.value.filter(function (_, i) {
      return index !== i;
    });
    updateModel(event, newValue);

    if (props.onUnselect) {
      props.onUnselect({
        originalEvent: event,
        value: removedValue
      });
    }
  };

  var onInputKeyDown = function onInputKeyDown(event) {
    if (overlayVisibleState) {
      var highlightItem = DomHandler.findSingle(overlayRef.current, 'li.p-highlight');

      switch (event.which) {
        //down
        case 40:
          if (highlightItem) {
            var nextElement = findNextItem(highlightItem);

            if (nextElement) {
              DomHandler.addClass(nextElement, 'p-highlight');
              DomHandler.removeClass(highlightItem, 'p-highlight');
              DomHandler.scrollInView(overlayRef.current, nextElement);
            }
          } else {
            highlightItem = DomHandler.findSingle(overlayRef.current, 'li');

            if (DomHandler.hasClass(highlightItem, 'p-autocomplete-item-group')) {
              highlightItem = findNextItem(highlightItem);
            }

            if (highlightItem) {
              DomHandler.addClass(highlightItem, 'p-highlight');
            }
          }

          event.preventDefault();
          break;
        //up

        case 38:
          if (highlightItem) {
            var previousElement = findPrevItem(highlightItem);

            if (previousElement) {
              DomHandler.addClass(previousElement, 'p-highlight');
              DomHandler.removeClass(highlightItem, 'p-highlight');
              DomHandler.scrollInView(overlayRef.current, previousElement);
            }
          }

          event.preventDefault();
          break;
        //enter

        case 13:
          if (highlightItem) {
            selectHighlightItem(event, highlightItem);
            hide();
          }

          event.preventDefault();
          break;
        //escape

        case 27:
          hide();
          event.preventDefault();
          break;
        //tab

        case 9:
          if (highlightItem) {
            selectHighlightItem(event, highlightItem);
          }

          hide();
          break;
      }
    }

    if (props.multiple) {
      switch (event.which) {
        //backspace
        case 8:
          if (props.value && props.value.length && !inputRef.current.value) {
            var removedValue = props.value[props.value.length - 1];
            var newValue = props.value.slice(0, -1);
            updateModel(event, newValue);

            if (props.onUnselect) {
              props.onUnselect({
                originalEvent: event,
                value: removedValue
              });
            }
          }

          break;
      }
    }
  };

  var selectHighlightItem = function selectHighlightItem(event, item) {
    if (props.optionGroupLabel) {
      var optionGroup = props.suggestions[item.dataset.group];
      selectItem(event, getOptionGroupChildren(optionGroup)[item.dataset.index]);
    } else {
      selectItem(event, props.suggestions[DomHandler.index(item)]);
    }
  };

  var findNextItem = function findNextItem(item) {
    var nextItem = item.nextElementSibling;
    return nextItem ? DomHandler.hasClass(nextItem, 'p-autocomplete-item-group') ? findNextItem(nextItem) : nextItem : null;
  };

  var findPrevItem = function findPrevItem(item) {
    var prevItem = item.previousElementSibling;
    return prevItem ? DomHandler.hasClass(prevItem, 'p-autocomplete-item-group') ? findPrevItem(prevItem) : prevItem : null;
  };

  var onInputFocus = function onInputFocus(event) {
    setFocusedState(true);
    props.onFocus && props.onFocus(event);
  };

  var forceItemSelection = function forceItemSelection(event) {
    var inputValue = event.target.value.trim();
    var item = (props.suggestions || []).find(function (it) {
      var value = props.field ? ObjectUtils.resolveFieldData(it, props.field) : it;
      return value && inputValue === value.trim();
    });

    if (item) {
      selectItem(event, item, true);
    } else {
      inputRef.current.value = '';
      updateModel(event, null);
      props.onClear && props.onClear(event);
    }
  };

  var onInputBlur = function onInputBlur(event) {
    setFocusedState(false);

    if (props.forceSelection) {
      forceItemSelection(event);
    }

    props.onBlur && props.onBlur(event);
  };

  var onMultiContainerClick = function onMultiContainerClick(event) {
    inputRef.current.focus();
    props.onClick && props.onClick(event);
  };

  var onMultiInputFocus = function onMultiInputFocus(event) {
    onInputFocus(event);
    DomHandler.addClass(multiContainerRef.current, 'p-focus');
  };

  var onMultiInputBlur = function onMultiInputBlur(event) {
    onInputBlur(event);
    DomHandler.removeClass(multiContainerRef.current, 'p-focus');
  };

  var isSelected = function isSelected(val) {
    return props.value ? props.value.some(function (v) {
      return ObjectUtils.equals(v, val);
    }) : false;
  };

  var findOptionIndex = function findOptionIndex(option) {
    return props.suggestions ? props.suggestions.findIndex(function (s) {
      return ObjectUtils.equals(s, option);
    }) : -1;
  };

  var getOptionGroupLabel = function getOptionGroupLabel(optionGroup) {
    return props.optionGroupLabel ? ObjectUtils.resolveFieldData(optionGroup, props.optionGroupLabel) : optionGroup;
  };

  var getOptionGroupChildren = function getOptionGroupChildren(optionGroup) {
    return ObjectUtils.resolveFieldData(optionGroup, props.optionGroupChildren);
  };

  React.useEffect(function () {
    ObjectUtils.combinedRefs(inputRef, props.inputRef);
  }, [inputRef, props.inputRef]);
  useMountEffect(function () {
    if (!idState) {
      setIdState(UniqueComponentId());
    }

    if (props.autoFocus && inputRef.current) {
      inputRef.current.focus();
    }
  });
  useUpdateEffect(function () {
    if (searchingState) {
      ObjectUtils.isNotEmpty(props.suggestions) ? show() : hide();
      setSearchingState(false);
    }
  }, [props.suggestions]);
  useUpdateEffect(function () {
    if (inputRef.current && !props.multiple) {
      updateInputField(props.value);
    }
  });
  useUnmountEffect(function () {
    if (timeout.current) {
      clearTimeout(timeout.current);
    }

    ZIndexUtils.clear(overlayRef.current);
  });
  React.useImperativeHandle(ref, function () {
    return {
      search: search
    };
  });

  var createSimpleAutoComplete = function createSimpleAutoComplete() {
    var value = formatValue(props.value);
    var ariaControls = idState + '_list';
    var className = classNames('p-autocomplete-input', props.inputClassName, {
      'p-autocomplete-dd-input': props.dropdown
    });
    return /*#__PURE__*/React.createElement(InputText, {
      ref: inputRef,
      id: props.inputId,
      type: props.type,
      name: props.name,
      defaultValue: value,
      role: "searchbox",
      "aria-autocomplete": "list",
      "aria-controls": ariaControls,
      "aria-labelledby": props.ariaLabelledBy,
      className: className,
      style: props.inputStyle,
      autoComplete: "off",
      readOnly: props.readOnly,
      disabled: props.disabled,
      placeholder: props.placeholder,
      size: props.size,
      maxLength: props.maxLength,
      tabIndex: props.tabIndex,
      onBlur: onInputBlur,
      onFocus: onInputFocus,
      onChange: onInputChange,
      onMouseDown: props.onMouseDown,
      onKeyUp: props.onKeyUp,
      onKeyDown: onInputKeyDown,
      onKeyPress: props.onKeyPress,
      onContextMenu: props.onContextMenu,
      onClick: props.onClick,
      onDoubleClick: props.onDblClick
    });
  };

  var createChips = function createChips() {
    if (ObjectUtils.isNotEmpty(props.value)) {
      return props.value.map(function (val, index) {
        var key = index + 'multi-item';
        return /*#__PURE__*/React.createElement("li", {
          key: key,
          className: "p-autocomplete-token p-highlight"
        }, /*#__PURE__*/React.createElement("span", {
          className: "p-autocomplete-token-label"
        }, formatValue(val)), !props.disabled && IconUtils.getJSXIcon(props.removeIcon, {
          className: 'p-autocomplete-token-icon',
          onClick: function onClick(e) {
            return removeItem(e, index);
          }
        }, {
          props: props
        }));
      });
    }

    return null;
  };

  var createMultiInput = function createMultiInput() {
    var ariaControls = idState + '_list';
    return /*#__PURE__*/React.createElement("li", {
      className: "p-autocomplete-input-token"
    }, /*#__PURE__*/React.createElement("input", {
      ref: inputRef,
      type: props.type,
      disabled: props.disabled,
      placeholder: props.placeholder,
      role: "searchbox",
      "aria-autocomplete": "list",
      "aria-controls": ariaControls,
      "aria-labelledby": props.ariaLabelledBy,
      autoComplete: "off",
      tabIndex: props.tabIndex,
      onChange: onInputChange,
      id: props.inputId,
      name: props.name,
      style: props.inputStyle,
      className: props.inputClassName,
      maxLength: props.maxLength,
      onKeyUp: props.onKeyUp,
      onKeyDown: onInputKeyDown,
      onKeyPress: props.onKeyPress,
      onFocus: onMultiInputFocus,
      onBlur: onMultiInputBlur
    }));
  };

  var createMultipleAutoComplete = function createMultipleAutoComplete() {
    var className = classNames('p-autocomplete-multiple-container p-component p-inputtext', {
      'p-disabled': props.disabled
    });
    var tokens = createChips();
    var input = createMultiInput();
    return /*#__PURE__*/React.createElement("ul", {
      ref: multiContainerRef,
      className: className,
      onContextMenu: props.onContextMenu,
      onMouseDown: props.onMouseDown,
      onClick: onMultiContainerClick,
      onDoubleClick: props.onDblClick
    }, tokens, input);
  };

  var createDropdown = function createDropdown() {
    if (props.dropdown) {
      return /*#__PURE__*/React.createElement(Button, {
        type: "button",
        icon: props.dropdownIcon,
        className: "p-autocomplete-dropdown",
        disabled: props.disabled,
        onClick: onDropdownClick
      });
    }

    return null;
  };

  var createLoader = function createLoader() {
    if (searchingState) {
      return /*#__PURE__*/React.createElement("i", {
        className: "p-autocomplete-loader pi pi-spinner pi-spin"
      });
    }

    return null;
  };

  var createInput = function createInput() {
    return props.multiple ? createMultipleAutoComplete() : createSimpleAutoComplete();
  };

  var listId = idState + '_list';
  var hasTooltip = ObjectUtils.isNotEmpty(props.tooltip);
  var otherProps = ObjectUtils.findDiffKeys(props, AutoComplete.defaultProps);
  var className = classNames('p-autocomplete p-component p-inputwrapper', {
    'p-autocomplete-dd': props.dropdown,
    'p-autocomplete-multiple': props.multiple,
    'p-inputwrapper-filled': props.value,
    'p-inputwrapper-focus': focusedState
  }, props.className);
  var loader = createLoader();
  var input = createInput();
  var dropdown = createDropdown();
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", _extends({
    ref: elementRef,
    id: idState,
    style: props.style,
    className: className,
    "aria-haspopup": "listbox",
    "aria-expanded": overlayVisibleState,
    "aria-owns": listId
  }, otherProps), input, loader, dropdown, /*#__PURE__*/React.createElement(AutoCompletePanel, _extends({
    ref: overlayRef,
    virtualScrollerRef: virtualScrollerRef
  }, props, {
    listId: listId,
    onItemClick: selectItem,
    selectedItem: selectedItem,
    onClick: onPanelClick,
    getOptionGroupLabel: getOptionGroupLabel,
    getOptionGroupChildren: getOptionGroupChildren,
    "in": overlayVisibleState,
    onEnter: onOverlayEnter,
    onEntering: onOverlayEntering,
    onEntered: onOverlayEntered,
    onExit: onOverlayExit,
    onExited: onOverlayExited
  }))), hasTooltip && /*#__PURE__*/React.createElement(Tooltip, _extends({
    target: elementRef,
    content: props.tooltip
  }, props.tooltipOptions)));
}));
AutoComplete.displayName = 'AutoComplete';
AutoComplete.defaultProps = {
  __TYPE: 'AutoComplete',
  id: null,
  inputRef: null,
  value: null,
  name: null,
  type: 'text',
  suggestions: null,
  field: null,
  optionGroupLabel: null,
  optionGroupChildren: null,
  optionGroupTemplate: null,
  forceSelection: false,
  autoHighlight: false,
  virtualScrollerOptions: null,
  scrollHeight: '200px',
  dropdown: false,
  dropdownMode: 'blank',
  dropdownAutoFocus: true,
  multiple: false,
  minLength: 1,
  delay: 300,
  style: null,
  className: null,
  inputId: null,
  inputStyle: null,
  inputClassName: null,
  panelClassName: null,
  panelStyle: null,
  placeholder: null,
  readOnly: false,
  disabled: false,
  maxLength: null,
  size: null,
  appendTo: null,
  tabIndex: null,
  autoFocus: false,
  tooltip: null,
  tooltipOptions: null,
  ariaLabelledBy: null,
  completeMethod: null,
  itemTemplate: null,
  selectedItemTemplate: null,
  transitionOptions: null,
  dropdownIcon: 'pi pi-chevron-down',
  removeIcon: 'pi pi-times-circle',
  onChange: null,
  onFocus: null,
  onBlur: null,
  onSelect: null,
  onUnselect: null,
  onDropdownClick: null,
  onClick: null,
  onDblClick: null,
  onMouseDown: null,
  onKeyUp: null,
  onKeyPress: null,
  onContextMenu: null,
  onClear: null,
  onShow: null,
  onHide: null
};

export { AutoComplete };
