/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { isVNode, defineComponent, toRefs, ref, computed, nextTick, createVNode, mergeProps } from 'vue';
import { Input } from '../input/index.js';
import { Popup } from '../popup/index.js';
import useCommonClassName from '../hooks/useCommonClassName.js';
import useVModel from '../hooks/useVModel.js';
import { useConfig } from '../config-provider/useConfig.js';
import '../hooks/index.js';
import AutoCompleteOptionList from './option-list.js';
import props from './props.js';
import { useContent, useTNodeJSX } from '../hooks/tnode.js';
import '../utils/withInstall.js';
import '../input/input.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@ldesign/icons-vue-next';
import 'lodash/isUndefined';
import '../form/hooks.js';
import '../hooks/useConfig.js';
import '../hooks/useGlobalIcon.js';
import '../input/props.js';
import '../input/useInput.js';
import '../form/const.js';
import '../input/useLengthLimit.js';
import 'lodash/isNumber';
import 'lodash/isObject';
import '../_common/js/log/index.js';
import '../_common/js/log/log.js';
import '../_common/js/utils/helper.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'lodash/isString';
import 'lodash/isNull';
import 'lodash/isArray';
import '../input/useInputEventHandler.js';
import '../input/useInputWidth.js';
import '../input/input-group.js';
import '../input/input-group-props.js';
import '../input/style';
import '../input/type.js';
import 'lodash/cloneDeep';
import 'lodash/isFunction';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-2e5d8948.js';
import 'dayjs';
import '../_chunks/dep-4580aabe.js';
import '../config-provider/type.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../utils/render-tnode.js';
import 'lodash/isEmpty';
import '../hooks/icon.js';
import '../hooks/slot.js';
import '../hooks/useDefaultValue.js';
import '../hooks/useDestroyOnClose.js';
import '../hooks/useKeepAnimation.js';
import '../hooks/useRipple.js';
import '../utils/set-style.js';
import '../hooks/useVirtualScroll.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../utils/dom.js';
import '../utils/easing.js';
import '../_common/js/utils/set-style.js';
import '../popup/container.js';
import '../hooks/useResizeObserver.js';
import '../popup/props.js';
import '../popup/style';
import '../popup/type.js';
import './highlight-option.js';

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; }
function _isSlot(s) {
  return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !isVNode(s);
}
var _AutoComplete = defineComponent({
  name: "TAutoComplete",
  props: props,
  setup: function setup(props2, _ref) {
    var slots = _ref.slots;
    var _toRefs = toRefs(props2),
      value = _toRefs.value,
      modelValue = _toRefs.modelValue;
    var _useVModel = useVModel(value, modelValue, props2.defaultValue, props2.onChange),
      _useVModel2 = _slicedToArray(_useVModel, 2),
      tValue = _useVModel2[0],
      setTValue = _useVModel2[1];
    var renderContent = useContent();
    var renderTNodeJSX = useTNodeJSX();
    var _useCommonClassName = useCommonClassName(),
      classPrefix = _useCommonClassName.classPrefix,
      sizeClassNames = _useCommonClassName.sizeClassNames;
    var _useConfig = useConfig("input"),
      global = _useConfig.globalConfig;
    var popupVisible = ref();
    var optionListRef = ref();
    var getOverlayStyle = function getOverlayStyle(trigger, popupElement) {
      var _props2$popupProps;
      var triggerWidth = trigger.getBoundingClientRect().width || trigger.offsetWidth || trigger.clientWidth;
      var popupWidth = popupElement.getBoundingClientRect().width || popupElement.offsetWidth || popupElement.clientWidth;
      return _objectSpread({
        width: triggerWidth >= popupWidth ? "".concat(triggerWidth, "px") : "auto"
      }, (_props2$popupProps = props2.popupProps) === null || _props2$popupProps === void 0 ? void 0 : _props2$popupProps.overlayInnerStyle);
    };
    var classes = computed(function () {
      return ["".concat(classPrefix.value, "-auto-complete")];
    });
    var popupClasses = computed(function () {
      var _props2$popupProps2;
      var classes2 = ["".concat(classPrefix.value, "-select__dropdown")];
      if ((_props2$popupProps2 = props2.popupProps) !== null && _props2$popupProps2 !== void 0 && _props2$popupProps2.overlayClassName) classes2 = classes2.concat(props2.popupProps.overlayClassName);
      return classes2;
    });
    var popupInnerClasses = computed(function () {
      var _props2$popupProps3;
      var classes2 = ["".concat(classPrefix.value, "-select__dropdown-inner")];
      if ((_props2$popupProps3 = props2.popupProps) !== null && _props2$popupProps3 !== void 0 && _props2$popupProps3.overlayInnerClassName) classes2 = classes2.concat(props2.popupProps.overlayInnerClassName);
      return classes2;
    });
    var onInputChange = function onInputChange(value2, context) {
      setTValue(value2, context);
    };
    var innerInputProps = computed(function () {
      var tProps = _objectSpread({
        value: tValue.value,
        size: props2.size
      }, props2.inputProps);
      return tProps;
    });
    var onInnerFocus = function onInnerFocus(value2, context) {
      var _props2$onFocus;
      popupVisible.value = true;
      (_props2$onFocus = props2.onFocus) === null || _props2$onFocus === void 0 ? void 0 : _props2$onFocus.call(props2, _objectSpread(_objectSpread({}, context), {}, {
        value: value2
      }));
      nextTick(function () {
        var _optionListRef$value;
        (_optionListRef$value = optionListRef.value) === null || _optionListRef$value === void 0 ? void 0 : _optionListRef$value.addKeyboardListener();
      });
    };
    var onInnerBlur = function onInnerBlur(value2, context) {
      var _props2$onBlur;
      (_props2$onBlur = props2.onBlur) === null || _props2$onBlur === void 0 ? void 0 : _props2$onBlur.call(props2, _objectSpread(_objectSpread({}, context), {}, {
        value: value2
      }));
    };
    var onInnerCompositionend = function onInnerCompositionend(value2, context) {
      var _props2$onComposition;
      (_props2$onComposition = props2.onCompositionend) === null || _props2$onComposition === void 0 ? void 0 : _props2$onComposition.call(props2, _objectSpread(_objectSpread({}, context), {}, {
        value: value2
      }));
    };
    var onInnerCompositionstart = function onInnerCompositionstart(value2, context) {
      var _props2$onComposition2;
      (_props2$onComposition2 = props2.onCompositionstart) === null || _props2$onComposition2 === void 0 ? void 0 : _props2$onComposition2.call(props2, _objectSpread(_objectSpread({}, context), {}, {
        value: value2
      }));
    };
    var onInnerEnter = function onInnerEnter(value2, context) {
      var _props2$onEnter;
      (_props2$onEnter = props2.onEnter) === null || _props2$onEnter === void 0 ? void 0 : _props2$onEnter.call(props2, _objectSpread(_objectSpread({}, context), {}, {
        value: value2
      }));
    };
    var onInnerSelect = function onInnerSelect(value2, context) {
      var _props2$onSelect;
      if (props2.readonly || props2.disabled) return;
      popupVisible.value = false;
      setTValue(value2, context);
      (_props2$onSelect = props2.onSelect) === null || _props2$onSelect === void 0 ? void 0 : _props2$onSelect.call(props2, value2, context);
    };
    var onPopupVisibleChange = function onPopupVisibleChange(visible, _ref2) {
      var trigger = _ref2.trigger;
      if (trigger !== "trigger-element-click") popupVisible.value = visible;
    };
    return function () {
      var _props2$placeholder, _props2$options;
      var triggerNode = renderContent("default", "triggerElement") || createVNode(Input, mergeProps({
        "placeholder": (_props2$placeholder = props2.placeholder) !== null && _props2$placeholder !== void 0 ? _props2$placeholder : global.value.placeholder,
        "tips": props2.tips,
        "status": props2.status,
        "readonly": props2.readonly,
        "disabled": props2.disabled,
        "autofocus": props2.autofocus,
        "clearable": props2.clearable,
        "onChange": onInputChange,
        "onFocus": onInnerFocus,
        "onBlur": onInnerBlur,
        "onClear": props2.onClear,
        "onCompositionend": onInnerCompositionend,
        "onCompositionstart": onInnerCompositionstart,
        "onEnter": onInnerEnter
      }, innerInputProps.value), slots);
      var listContent = createVNode(AutoCompleteOptionList, {
        "ref": optionListRef,
        "value": tValue.value,
        "options": props2.options,
        "size": props2.size,
        "sizeClassNames": sizeClassNames,
        "onSelect": onInnerSelect,
        "popupVisible": popupVisible.value,
        "highlightKeyword": props2.highlightKeyword,
        "filterable": props2.filterable,
        "filter": props2.filter
      }, {
        option: slots.option
      });
      var topContent = renderTNodeJSX("panelTopContent");
      var bottomContent = renderTNodeJSX("panelBottomContent");
      var panelContent = topContent || (_props2$options = props2.options) !== null && _props2$options !== void 0 && _props2$options.length || bottomContent ? createVNode("div", {
        "class": "".concat(classPrefix.value, "-autocomplete__panel")
      }, [topContent, listContent, bottomContent]) : null;
      var popupProps = _objectSpread(_objectSpread({}, props2.popupProps), {}, {
        overlayInnerStyle: getOverlayStyle,
        overlayInnerClassName: popupInnerClasses.value,
        overlayClassName: popupClasses.value
      });
      return createVNode("div", {
        "class": classes.value
      }, [panelContent && createVNode(Popup, mergeProps({
        "visible": popupVisible.value,
        "onVisibleChange": onPopupVisibleChange,
        "trigger": "focus",
        "placement": "bottom-left",
        "hideEmptyPopup": true,
        "content": panelContent ? function () {
          return panelContent;
        } : null
      }, popupProps), _isSlot(triggerNode) ? triggerNode : {
        "default": function _default() {
          return [triggerNode];
        }
      }), !panelContent && triggerNode]);
    };
  }
});

export { _AutoComplete as default };
//# sourceMappingURL=auto-complete.js.map
