"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _isUndefined = _interopRequireDefault(require("lodash/isUndefined"));

var _isNil = _interopRequireDefault(require("lodash/isNil"));

var _isFunction = _interopRequireDefault(require("lodash/isFunction"));

var _remove = _interopRequireDefault(require("lodash/remove"));

var _clone = _interopRequireDefault(require("lodash/clone"));

var _isArray = _interopRequireDefault(require("lodash/isArray"));

var _omit = _interopRequireDefault(require("lodash/omit"));

var _pick = _interopRequireDefault(require("lodash/pick"));

var _domLib = require("dom-lib");

var _shallowEqual = _interopRequireDefault(require("../utils/shallowEqual"));

var _treeUtils = require("../utils/treeUtils");

var _Plaintext = _interopRequireDefault(require("../Plaintext"));

var _utils = require("../utils");

var _Picker = require("../Picker");

var _Tag = _interopRequireDefault(require("../Tag"));

var _InputAutosize = _interopRequireDefault(require("./InputAutosize"));

var _InputSearch = _interopRequireDefault(require("./InputSearch"));

function _templateObject4() {
  var data = (0, _taggedTemplateLiteralLoose2.default)(["tag-wrapper"]);

  _templateObject4 = function _templateObject4() {
    return data;
  };

  return data;
}

function _templateObject3() {
  var data = (0, _taggedTemplateLiteralLoose2.default)(["focused"]);

  _templateObject3 = function _templateObject3() {
    return data;
  };

  return data;
}

function _templateObject2() {
  var data = (0, _taggedTemplateLiteralLoose2.default)(["tag"]);

  _templateObject2 = function _templateObject2() {
    return data;
  };

  return data;
}

function _templateObject() {
  var data = (0, _taggedTemplateLiteralLoose2.default)(["none"]);

  _templateObject = function _templateObject() {
    return data;
  };

  return data;
}

var defaultProps = {
  as: 'div',
  appearance: 'default',
  cleanable: true,
  cacheData: [],
  classPrefix: 'picker',
  data: [],
  disabledItemValues: [],
  valueKey: 'value',
  labelKey: 'label',
  placement: 'bottomStart',
  searchable: true,
  menuAutoWidth: true,
  menuMaxHeight: 320,
  tagProps: {}
};

var InputPicker = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var _merge;

  var Component = props.as,
      cleanable = props.cleanable,
      cacheData = props.cacheData,
      classPrefix = props.classPrefix,
      controlledData = props.data,
      disabled = props.disabled,
      readOnly = props.readOnly,
      plaintext = props.plaintext,
      defaultValue = props.defaultValue,
      defaultOpen = props.defaultOpen,
      disabledItemValues = props.disabledItemValues,
      overrideLocale = props.locale,
      toggleAs = props.toggleAs,
      style = props.style,
      searchable = props.searchable,
      multi = props.multi,
      controlledOpen = props.open,
      placeholder = props.placeholder,
      tagProps = props.tagProps,
      groupBy = props.groupBy,
      menuClassName = props.menuClassName,
      menuStyle = props.menuStyle,
      menuAutoWidth = props.menuAutoWidth,
      menuMaxHeight = props.menuMaxHeight,
      creatable = props.creatable,
      valueProp = props.value,
      valueKey = props.valueKey,
      virtualized = props.virtualized,
      labelKey = props.labelKey,
      listProps = props.listProps,
      id = props.id,
      tabIndex = props.tabIndex,
      sort = props.sort,
      renderMenu = props.renderMenu,
      renderExtraFooter = props.renderExtraFooter,
      renderValue = props.renderValue,
      renderMenuItem = props.renderMenuItem,
      renderMenuGroup = props.renderMenuGroup,
      onEnter = props.onEnter,
      onEntered = props.onEntered,
      onExit = props.onExit,
      onExited = props.onExited,
      onChange = props.onChange,
      onClean = props.onClean,
      onCreate = props.onCreate,
      onSearch = props.onSearch,
      onSelect = props.onSelect,
      onOpen = props.onOpen,
      onClose = props.onClose,
      onBlur = props.onBlur,
      onFocus = props.onFocus,
      searchBy = props.searchBy,
      placement = props.placement,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["as", "cleanable", "cacheData", "classPrefix", "data", "disabled", "readOnly", "plaintext", "defaultValue", "defaultOpen", "disabledItemValues", "locale", "toggleAs", "style", "searchable", "multi", "open", "placeholder", "tagProps", "groupBy", "menuClassName", "menuStyle", "menuAutoWidth", "menuMaxHeight", "creatable", "value", "valueKey", "virtualized", "labelKey", "listProps", "id", "tabIndex", "sort", "renderMenu", "renderExtraFooter", "renderValue", "renderMenuItem", "renderMenuGroup", "onEnter", "onEntered", "onExit", "onExited", "onChange", "onClean", "onCreate", "onSearch", "onSelect", "onOpen", "onClose", "onBlur", "onFocus", "searchBy", "placement"]);

  if (groupBy === valueKey || groupBy === labelKey) {
    throw Error('`groupBy` can not be equal to `valueKey` and `labelKey`');
  }

  var overlayRef = (0, _react.useRef)();
  var targetRef = (0, _react.useRef)();
  var triggerRef = (0, _react.useRef)();
  var inputRef = (0, _react.useRef)();

  var _useCustom = (0, _utils.useCustom)(['Picker', 'InputPicker'], overrideLocale),
      locale = _useCustom.locale;

  var _useClassNames = (0, _utils.useClassNames)(classPrefix),
      prefix = _useClassNames.prefix,
      merge = _useClassNames.merge;

  var _useState = (0, _react.useState)(controlledData),
      uncontrolledData = _useState[0],
      setData = _useState[1];

  var _useState2 = (0, _react.useState)(100),
      maxWidth = _useState2[0],
      setMaxWidth = _useState2[1];

  var _useState3 = (0, _react.useState)([]),
      newData = _useState3[0],
      setNewData = _useState3[1];

  var _useState4 = (0, _react.useState)(defaultOpen),
      uncontrolledOpen = _useState4[0],
      setOpen = _useState4[1];

  var open = (0, _isUndefined.default)(controlledOpen) ? uncontrolledOpen : controlledOpen;
  var getAllData = (0, _react.useCallback)(function () {
    return [].concat(uncontrolledData, newData);
  }, [uncontrolledData, newData]);
  var getAllDataAndCache = (0, _react.useCallback)(function () {
    return [].concat(getAllData(), cacheData);
  }, [getAllData, cacheData]);

  var _useControlled = (0, _utils.useControlled)(valueProp, multi ? defaultValue || [] : defaultValue),
      value = _useControlled[0],
      setValue = _useControlled[1],
      isControlled = _useControlled[2];

  var cloneValue = (0, _react.useCallback)(function () {
    return multi ? (0, _clone.default)(value) || [] : value;
  }, [multi, value]);
  var handleClose = (0, _react.useCallback)(function () {
    var _triggerRef$current;

    triggerRef === null || triggerRef === void 0 ? void 0 : (_triggerRef$current = triggerRef.current) === null || _triggerRef$current === void 0 ? void 0 : _triggerRef$current.close();
  }, [triggerRef]); // Used to hover the focuse item  when trigger `onKeydown`

  var _useFocusItemValue = (0, _Picker.useFocusItemValue)(multi ? value === null || value === void 0 ? void 0 : value[0] : value, {
    data: getAllDataAndCache(),
    valueKey: valueKey,
    target: function target() {
      return overlayRef.current;
    }
  }),
      focusItemValue = _useFocusItemValue.focusItemValue,
      setFocusItemValue = _useFocusItemValue.setFocusItemValue,
      onKeyDown = _useFocusItemValue.onKeyDown;

  var handleSearchCallback = (0, _react.useCallback)(function (searchKeyword, filteredData, event) {
    var _filteredData$;

    // The first option after filtering is the focus.
    setFocusItemValue((filteredData === null || filteredData === void 0 ? void 0 : (_filteredData$ = filteredData[0]) === null || _filteredData$ === void 0 ? void 0 : _filteredData$[valueKey]) || searchKeyword);
    onSearch === null || onSearch === void 0 ? void 0 : onSearch(searchKeyword, event);
  }, [setFocusItemValue, onSearch, valueKey]); // Use search keywords to filter options.

  var _useSearch = (0, _Picker.useSearch)({
    labelKey: labelKey,
    data: getAllData(),
    searchBy: searchBy,
    callback: handleSearchCallback
  }),
      searchKeyword = _useSearch.searchKeyword,
      setSearchKeyword = _useSearch.setSearchKeyword,
      checkShouldDisplay = _useSearch.checkShouldDisplay,
      handleSearch = _useSearch.handleSearch; // Update the state when the data in props changes


  (0, _react.useEffect)(function () {
    if (controlledData && !(0, _shallowEqual.default)(controlledData, uncontrolledData)) {
      var _controlledData$;

      setData(controlledData);
      setNewData([]);
      setFocusItemValue(controlledData === null || controlledData === void 0 ? void 0 : (_controlledData$ = controlledData[0]) === null || _controlledData$ === void 0 ? void 0 : _controlledData$[valueKey]);
    }
  }, [setFocusItemValue, controlledData, uncontrolledData, valueKey]);
  (0, _react.useEffect)(function () {
    var _triggerRef$current2;

    // In multiple selection, you need to set a maximum width for the input.
    if ((_triggerRef$current2 = triggerRef.current) === null || _triggerRef$current2 === void 0 ? void 0 : _triggerRef$current2.root) {
      setMaxWidth((0, _domLib.getWidth)(triggerRef.current.root));
    }
  }, []); // Update the position of the menu when the search keyword and value change

  (0, _react.useEffect)(function () {
    var _triggerRef$current3, _triggerRef$current3$;

    (_triggerRef$current3 = triggerRef.current) === null || _triggerRef$current3 === void 0 ? void 0 : (_triggerRef$current3$ = _triggerRef$current3.updatePosition) === null || _triggerRef$current3$ === void 0 ? void 0 : _triggerRef$current3$.call(_triggerRef$current3);
  }, [searchKeyword, value]);

  var getDateItem = function getDateItem(value) {
    // Find active `MenuItem` by `value`
    var allData = getAllDataAndCache();
    var activeItem = allData.find(function (item) {
      return (0, _shallowEqual.default)(item[valueKey], value);
    });
    var displayElement = placeholder;

    if (activeItem === null || activeItem === void 0 ? void 0 : activeItem[labelKey]) {
      displayElement = activeItem === null || activeItem === void 0 ? void 0 : activeItem[labelKey];
    }

    return {
      isValid: !!activeItem,
      activeItem: activeItem,
      displayElement: displayElement
    };
  };

  var getInput = (0, _react.useCallback)(function () {
    var _inputRef$current;

    return multi ? (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.input : inputRef.current;
  }, [inputRef, multi]);
  var focusInput = (0, _react.useCallback)(function () {
    var _getInput;

    return (_getInput = getInput()) === null || _getInput === void 0 ? void 0 : _getInput.focus();
  }, [getInput]);
  var blurInput = (0, _react.useCallback)(function () {
    var _getInput2;

    return (_getInput2 = getInput()) === null || _getInput2 === void 0 ? void 0 : _getInput2.blur();
  }, [getInput]);
  /**
   * Convert the string of the newly created option into an object.
   */

  var createOption = (0, _react.useCallback)(function (value) {
    var _ref2;

    if (groupBy) {
      var _ref;

      return _ref = {
        create: true
      }, _ref[groupBy] = locale === null || locale === void 0 ? void 0 : locale.newItem, _ref[valueKey] = value, _ref[labelKey] = value, _ref;
    }

    return _ref2 = {
      create: true
    }, _ref2[valueKey] = value, _ref2[labelKey] = value, _ref2;
  }, [groupBy, valueKey, labelKey, locale]);
  var handleChange = (0, _react.useCallback)(function (value, event) {
    onChange === null || onChange === void 0 ? void 0 : onChange(value, event);
  }, [onChange]);
  var handleRemoveItemByTag = (0, _react.useCallback)(function (tag, event) {
    event.stopPropagation();
    var val = cloneValue();
    (0, _remove.default)(val, function (itemVal) {
      return (0, _shallowEqual.default)(itemVal, tag);
    });
    setValue(val);
    handleChange(val, event);
  }, [setValue, cloneValue, handleChange]);
  var handleSelect = (0, _react.useCallback)(function (value, item, event) {
    onSelect === null || onSelect === void 0 ? void 0 : onSelect(value, item, event);

    if (creatable && item.create) {
      delete item.create;
      onCreate === null || onCreate === void 0 ? void 0 : onCreate(value, item, event);
      setNewData(newData.concat(item));
    }
  }, [creatable, newData, onSelect, onCreate]);
  /**
   * Callback triggered by single selection
   * @param value
   * @param item
   * @param event
   */

  var handleItemSelect = function handleItemSelect(value, item, event) {
    setValue(value);
    setFocusItemValue(value);
    setSearchKeyword('');
    handleSelect(value, item, event);
    handleChange(value, event);
    handleClose();
  };
  /**
   * Callback triggered by multiple selection
   * @param nextItemValue
   * @param item
   * @param event
   * @param checked
   */


  var handleCheckItemSelect = function handleCheckItemSelect(nextItemValue, item, event, checked) {
    var val = cloneValue();

    if (checked) {
      val.push(nextItemValue);
    } else {
      (0, _remove.default)(val, function (itemVal) {
        return (0, _shallowEqual.default)(itemVal, nextItemValue);
      });
    }

    setValue(val);
    setSearchKeyword('');
    setFocusItemValue(nextItemValue);
    handleSelect(val, item, event);
    handleChange(val, event);
    focusInput();
  };

  var selectFocusMenuCheckItem = (0, _react.useCallback)(function (event) {
    var val = cloneValue();
    var data = getAllData();

    if (!focusItemValue || !data) {
      return;
    } // If the value is disabled in this option, it is returned.


    if (disabledItemValues === null || disabledItemValues === void 0 ? void 0 : disabledItemValues.some(function (item) {
      return item === focusItemValue;
    })) {
      return;
    }

    if (!val.some(function (v) {
      return (0, _shallowEqual.default)(v, focusItemValue);
    })) {
      val.push(focusItemValue);
    } else {
      (0, _remove.default)(val, function (itemVal) {
        return (0, _shallowEqual.default)(itemVal, focusItemValue);
      });
    }

    var focusItem = data.find(function (item) {
      return (0, _shallowEqual.default)(item === null || item === void 0 ? void 0 : item[valueKey], focusItemValue);
    });

    if (!focusItem) {
      focusItem = createOption(focusItemValue);
    }

    setValue(val);
    setSearchKeyword('');
    handleSelect(val, focusItem, event);
    handleChange(val, event);
  }, [setValue, cloneValue, getAllData, handleChange, handleSelect, createOption, setSearchKeyword, disabledItemValues, focusItemValue, valueKey]);
  var selectFocusMenuItem = (0, _react.useCallback)(function (event) {
    if (!focusItemValue || !controlledData) {
      return;
    } // If the value is disabled in this option, it is returned.


    if (disabledItemValues === null || disabledItemValues === void 0 ? void 0 : disabledItemValues.some(function (item) {
      return item === focusItemValue;
    })) {
      return;
    } // Find active `MenuItem` by `value`


    var allData = getAllData();
    var focusItem = allData.find(function (item) {
      return (0, _shallowEqual.default)(item[valueKey], focusItemValue);
    });

    if (!focusItem && focusItemValue === searchKeyword) {
      focusItem = createOption(searchKeyword);
    }

    setValue(focusItemValue);
    setSearchKeyword('');
    handleSelect(focusItemValue, focusItem, event);
    handleChange(focusItemValue, event);
    handleClose();
  }, [setValue, disabledItemValues, controlledData, focusItemValue, valueKey, searchKeyword, handleClose, setSearchKeyword, createOption, getAllData, handleChange, handleSelect]);
  (0, _Picker.usePublicMethods)(ref, {
    triggerRef: triggerRef,
    overlayRef: overlayRef,
    targetRef: targetRef
  });
  /**
   * Remove the last item, after pressing the back key on the keyboard.
   * @param event
   */

  var removeLastItem = (0, _react.useCallback)(function (event) {
    var target = event === null || event === void 0 ? void 0 : event.target;

    if ((target === null || target === void 0 ? void 0 : target.tagName) !== 'INPUT') {
      focusInput();
      return;
    }

    if ((target === null || target === void 0 ? void 0 : target.tagName) === 'INPUT' && (target === null || target === void 0 ? void 0 : target.value)) {
      return;
    }

    var val = cloneValue();
    val.pop();
    setValue(val);
    handleChange(val, event);
  }, [setValue, focusInput, handleChange, cloneValue]);
  var handleClean = (0, _react.useCallback)(function (event) {
    if (disabled || searchKeyword !== '') {
      return;
    }

    setValue(null);
    setFocusItemValue(null);
    setSearchKeyword('');
    handleChange(null, event);
    onClean === null || onClean === void 0 ? void 0 : onClean(event);
  }, [disabled, searchKeyword, onClean, handleChange, setValue, setFocusItemValue, setSearchKeyword]);
  var onPickerKeyDown = (0, _Picker.useToggleKeyDownEvent)((0, _extends2.default)({
    triggerRef: triggerRef,
    targetRef: targetRef,
    overlayRef: overlayRef,
    onMenuPressEnter: multi ? selectFocusMenuCheckItem : selectFocusMenuItem,
    onMenuPressBackspace: multi ? removeLastItem : handleClean,
    onMenuKeyDown: onKeyDown
  }, rest));
  var handleExited = (0, _react.useCallback)(function () {
    setFocusItemValue(multi ? value === null || value === void 0 ? void 0 : value[0] : value);
    setSearchKeyword('');
    onClose === null || onClose === void 0 ? void 0 : onClose();
  }, [setFocusItemValue, setSearchKeyword, onClose, value, multi]);
  var handleFocus = (0, _react.useCallback)(function () {
    var _triggerRef$current4;

    setOpen(true);
    (_triggerRef$current4 = triggerRef.current) === null || _triggerRef$current4 === void 0 ? void 0 : _triggerRef$current4.open();
  }, []);
  var handleBlur = (0, _react.useCallback)(function () {
    setOpen(false);
  }, []);
  var handleEnter = (0, _react.useCallback)(function () {
    focusInput();
    setOpen(true);
  }, [focusInput]);
  var handleExit = (0, _react.useCallback)(function () {
    blurInput();
    setOpen(false);
  }, [blurInput]);

  var renderDropdownMenuItem = function renderDropdownMenuItem(label, item) {
    // 'Create option "{0}"' =>  Create option "xxxxx"
    var newLabel = item.create ? /*#__PURE__*/_react.default.createElement("span", null, (0, _utils.tplTransform)(locale === null || locale === void 0 ? void 0 : locale.createOption, label)) : label;
    return renderMenuItem ? renderMenuItem(newLabel, item) : newLabel;
  };

  var renderSingleValue = function renderSingleValue() {
    if (multi) {
      return {
        isValid: false,
        displayElement: placeholder
      };
    }

    var dataItem = getDateItem(value);
    var displayElement = dataItem.displayElement;

    if (!(0, _isNil.default)(value) && (0, _isFunction.default)(renderValue)) {
      displayElement = renderValue(value, dataItem.activeItem, displayElement);
    }

    return {
      isValid: dataItem.isValid,
      displayElement: displayElement
    };
  };

  var renderMultiValue = function renderMultiValue() {
    if (!multi) {
      return null;
    }

    var _tagProps$closable = tagProps.closable,
        closable = _tagProps$closable === void 0 ? true : _tagProps$closable,
        onClose = tagProps.onClose,
        tagRest = (0, _objectWithoutPropertiesLoose2.default)(tagProps, ["closable", "onClose"]);
    var tags = value || [];
    var items = [];
    var tagElements = tags.map(function (tag) {
      var _getDateItem = getDateItem(tag),
          isValid = _getDateItem.isValid,
          displayElement = _getDateItem.displayElement,
          activeItem = _getDateItem.activeItem;

      items.push(activeItem);

      if (!isValid) {
        return null;
      }

      return /*#__PURE__*/_react.default.createElement(_Tag.default, (0, _extends2.default)({}, tagRest, {
        key: tag,
        closable: !disabled && closable && !readOnly && !plaintext,
        title: typeof displayElement === 'string' ? displayElement : undefined,
        onClose: (0, _utils.createChainedFunction)(handleRemoveItemByTag.bind(null, tag), onClose)
      }), displayElement);
    }).filter(function (item) {
      return item !== null;
    });

    if ((tags.length > 0 || isControlled) && (0, _isFunction.default)(renderValue)) {
      return renderValue(value, items, tagElements);
    }

    return tagElements;
  };

  var renderDropdownMenu = function renderDropdownMenu(positionProps, speakerRef) {
    var left = positionProps.left,
        top = positionProps.top,
        className = positionProps.className;
    var menuClassPrefix = multi ? 'picker-check-menu' : 'picker-select-menu';
    var classes = merge(className, menuClassName, prefix(menuClassPrefix));
    var styles = (0, _extends2.default)({}, menuStyle, {
      left: left,
      top: top
    });
    var items = (0, _treeUtils.filterNodesOfTree)(getAllData(), checkShouldDisplay);

    if (creatable && searchKeyword && !items.find(function (item) {
      return item[valueKey] === searchKeyword;
    })) {
      items = [].concat(items, [createOption(searchKeyword)]);
    } // Create a tree structure data when set `groupBy`


    if (groupBy) {
      items = (0, _utils.getDataGroupBy)(items, groupBy, sort);
    } else if (typeof sort === 'function') {
      items = items.sort(sort(false));
    }

    var menu = items.length ? /*#__PURE__*/_react.default.createElement(_Picker.DropdownMenu, {
      id: id ? id + "-listbox" : undefined,
      listProps: listProps,
      disabledItemValues: disabledItemValues,
      valueKey: valueKey,
      labelKey: labelKey,
      classPrefix: menuClassPrefix,
      dropdownMenuItemClassPrefix: multi ? undefined : menuClassPrefix + "-item",
      dropdownMenuItemAs: multi ? _Picker.DropdownMenuCheckItem : _Picker.DropdownMenuItem,
      activeItemValues: multi ? value : [value],
      focusItemValue: focusItemValue,
      maxHeight: menuMaxHeight,
      data: items,
      group: !(0, _isUndefined.default)(groupBy),
      onSelect: multi ? handleCheckItemSelect : handleItemSelect,
      renderMenuGroup: renderMenuGroup,
      renderMenuItem: renderDropdownMenuItem,
      virtualized: virtualized
    }) : /*#__PURE__*/_react.default.createElement("div", {
      className: prefix(_templateObject())
    }, locale === null || locale === void 0 ? void 0 : locale.noResultsText);
    return /*#__PURE__*/_react.default.createElement(_Picker.PickerOverlay, {
      ref: (0, _utils.mergeRefs)(overlayRef, speakerRef),
      autoWidth: menuAutoWidth,
      className: classes,
      style: styles,
      target: triggerRef,
      onKeyDown: onPickerKeyDown
    }, renderMenu ? renderMenu(menu) : menu, renderExtraFooter === null || renderExtraFooter === void 0 ? void 0 : renderExtraFooter());
  };

  var _renderSingleValue = renderSingleValue(),
      isValid = _renderSingleValue.isValid,
      displayElement = _renderSingleValue.displayElement;

  var tagElements = renderMultiValue();
  /**
   * 1.Have a value and the value is valid.
   * 2.Regardless of whether the value is valid, as long as renderValue is set, it is judged to have a value.
   * 3.If renderValue returns null or undefined, hasValue is false.
   */

  var hasSingleValue = !(0, _isNil.default)(value) && (0, _isFunction.default)(renderValue) && !(0, _isNil.default)(displayElement);
  var hasMultiValue = (0, _isArray.default)(value) && value.length > 0 && (0, _isFunction.default)(renderValue) && !(0, _isNil.default)(tagElements);
  var hasValue = multi ? !!(tagElements === null || tagElements === void 0 ? void 0 : tagElements.length) || hasMultiValue : isValid || hasSingleValue;

  var _usePickerClassName = (0, _Picker.usePickerClassName)((0, _extends2.default)({}, props, {
    hasValue: hasValue,
    name: 'input'
  })),
      pickerClasses = _usePickerClassName[0],
      usedClassNamePropKeys = _usePickerClassName[1];

  var classes = merge(pickerClasses, (_merge = {}, _merge[prefix(_templateObject2())] = multi, _merge[prefix(_templateObject3())] = open, _merge));
  var searching = !!searchKeyword && open;
  var displaySearchInput = searchable && !disabled;
  var inputProps = multi ? {
    inputStyle: {
      maxWidth: maxWidth - 63
    },
    as: _InputAutosize.default
  } : {
    as: 'input'
  };

  if (plaintext) {
    return /*#__PURE__*/_react.default.createElement(_Plaintext.default, {
      localeKey: "notSelected"
    }, displayElement || (tagElements === null || tagElements === void 0 ? void 0 : tagElements.length) ? tagElements : null);
  }

  return /*#__PURE__*/_react.default.createElement(_Picker.PickerToggleTrigger, {
    pickerProps: (0, _pick.default)(props, _Picker.pickTriggerPropKeys),
    ref: triggerRef,
    trigger: "active",
    onEnter: (0, _utils.createChainedFunction)(handleEnter, onEnter),
    onEntered: (0, _utils.createChainedFunction)(onEntered, onOpen),
    onExit: (0, _utils.createChainedFunction)(handleExit, onExit),
    onExited: (0, _utils.createChainedFunction)(handleExited, onExited),
    speaker: renderDropdownMenu,
    placement: placement
  }, /*#__PURE__*/_react.default.createElement(Component, {
    className: classes,
    style: style,
    onClick: focusInput,
    onKeyDown: onPickerKeyDown
  }, /*#__PURE__*/_react.default.createElement(_Picker.PickerToggle, (0, _extends2.default)({}, (0, _omit.default)(rest, [].concat(_Picker.omitTriggerPropKeys, usedClassNamePropKeys)), {
    id: id,
    readOnly: readOnly,
    plaintext: plaintext,
    ref: targetRef,
    as: toggleAs,
    tabIndex: null,
    onClean: handleClean,
    cleanable: cleanable && !disabled,
    hasValue: hasValue,
    active: open,
    disabled: disabled,
    placement: placement,
    inputValue: value
  }), searching || multi && hasValue ? null : displayElement || (locale === null || locale === void 0 ? void 0 : locale.placeholder)), /*#__PURE__*/_react.default.createElement("div", {
    className: prefix(_templateObject4())
  }, tagElements, displaySearchInput && /*#__PURE__*/_react.default.createElement(_InputSearch.default, (0, _extends2.default)({}, inputProps, {
    tabIndex: tabIndex,
    readOnly: readOnly,
    onBlur: (0, _utils.createChainedFunction)(handleBlur, onBlur),
    onFocus: (0, _utils.createChainedFunction)(handleFocus, onFocus),
    inputRef: inputRef,
    onChange: handleSearch,
    value: open ? searchKeyword : ''
  })))));
});

InputPicker.displayName = 'InputPicker';
InputPicker.defaultProps = defaultProps;
InputPicker.propTypes = (0, _extends2.default)({}, _Picker.listPickerPropTypes, {
  locale: _propTypes.default.any,
  appearance: _propTypes.default.oneOf(['default', 'subtle']),
  cacheData: _propTypes.default.array,
  menuAutoWidth: _propTypes.default.bool,
  menuMaxHeight: _propTypes.default.number,
  searchable: _propTypes.default.bool,
  creatable: _propTypes.default.bool,
  multi: _propTypes.default.bool,
  groupBy: _propTypes.default.any,
  sort: _propTypes.default.func,
  renderMenu: _propTypes.default.func,
  renderMenuItem: _propTypes.default.func,
  renderMenuGroup: _propTypes.default.func,
  onCreate: _propTypes.default.func,
  onSelect: _propTypes.default.func,
  onGroupTitleClick: _propTypes.default.func,
  onSearch: _propTypes.default.func,
  virtualized: _propTypes.default.bool,
  searchBy: _propTypes.default.func,
  tagProps: _propTypes.default.object
});
var _default = InputPicker;
exports.default = _default;