/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

import { h as helper } from '../../chunks/helper.js';
import { defineComponent, toRefs, ref, computed } from '@vue/composition-api';
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 { renderContent, renderLNodeJSX } from '../../utils/render-node.js';
import AutoCompleteOptionList from './option-list.js';
import props from './props.js';
import '../../chunks/_commonjsHelpers.js';
import '../../utils/map-props.js';
import 'vue';
import '../../utils/withInstall.js';
import 'lodash/capitalize';
import '../../config.js';
import '../input/input.js';
import '@ldesign/icons-vue';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../chunks/helper2.js';
import '../../chunks/defineProperty-19da3d4c.js';
import '../../chunks/index.js';
import '../../chunks/log.js';
import '../../chunks/_commonjsHelpers-fe796db6.js';
import '../../utils/helper.js';
import '../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../../utils/mixins.js';
import '../../utils/event.js';
import '../input/props.js';
import 'lodash';
import '../input/group.js';
import '../input/style/css.js';
import '../input/type.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../../chunks/set-style.js';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import '../popup/props.js';
import '../popup/container.js';
import '../popup/style/css.js';
import '../popup/type.js';
import 'lodash/isFunction';
import '../../hooks/useConfig.js';
import './highlight-option.js';

var _AutoComplete = defineComponent({
  name: "LAutoComplete",
  props,
  setup(props2, _ref) {
    let {
      emit
    } = _ref;
    const {
      value
    } = toRefs(props2);
    const [tValue, setTValue] = useVModel(value, props2.defaultValue, props2.onChange);
    const {
      classPrefix,
      sizeClassNames
    } = useCommonClassName();
    const {
      global
    } = useConfig("input");
    const popupVisible = ref();
    const getOverlayStyle = (trigger, popupElement) => {
      const triggerWidth = trigger.getBoundingClientRect().width || trigger.offsetWidth || trigger.clientWidth;
      const popupWidth = popupElement.getBoundingClientRect().width || popupElement.offsetWidth || popupElement.clientWidth;
      return {
        width: triggerWidth >= popupWidth ? `${triggerWidth}px` : "auto",
        ...props2.popupProps?.overlayInnerStyle
      };
    };
    const classes = computed(() => [`${classPrefix.value}-auto-complete`]);
    const popupClasses = computed(() => {
      let classes2 = [`${classPrefix.value}-select__dropdown`];
      if (props2.popupProps?.overlayClassName) classes2 = classes2.concat(props2.popupProps.overlayClassName);
      return classes2;
    });
    const popupInnerClasses = computed(() => {
      let classes2 = [`${classPrefix.value}-select__dropdown-inner`];
      if (props2.popupProps?.overlayInnerClassName) classes2 = classes2.concat(props2.popupProps.overlayInnerClassName);
      return classes2;
    });
    const onInputChange = (value2, context) => {
      setTValue(value2, context);
    };
    const innerInputProps = computed(() => {
      const tProps = {
        value: tValue.value,
        size: props2.size,
        ...props2.inputProps
      };
      return tProps;
    });
    const onInnerFocus = (value2, context) => {
      popupVisible.value = true;
      emit("focus", {
        ...context,
        value: value2
      });
      props2.onFocus?.({
        ...context,
        value: value2
      });
    };
    const inputListeners = computed(() => ({
      change: onInputChange,
      focus: onInnerFocus
    }));
    const onInnerSelect = (value2, context) => {
      if (props2.readonly || props2.disabled) return;
      popupVisible.value = false;
      setTValue(value2, context);
      emit("select", value2, context);
      props2.onSelect?.(value2, context);
    };
    const onPopupVisibleChange = (visible, _ref2) => {
      let {
        trigger
      } = _ref2;
      if (trigger !== "trigger-element-click") popupVisible.value = visible;
    };
    return {
      global,
      classes,
      classPrefix,
      popupClasses,
      popupInnerClasses,
      sizeClassNames,
      innerInputProps,
      inputListeners,
      tValue,
      popupVisible,
      onPopupVisibleChange,
      getOverlayStyle,
      onInnerSelect
    };
  },
  render() {
    const h = arguments[0];
    const triggerNode = renderContent(this, "default", "triggerElement") || h(Input, helper([{
      "attrs": {
        "placeholder": this.placeholder ?? this.global.placeholder
      }
    }, {
      "on": this.inputListeners
    }, {
      "attrs": {
        "tips": this.tips,
        "status": this.status,
        "readonly": this.readonly,
        "disabled": this.disabled
      }
    }, {
      "props": this.innerInputProps
    }, {
      "scopedSlots": this.$scopedSlots
    }]));
    const listContent = h(AutoCompleteOptionList, {
      "attrs": {
        "value": this.tValue,
        "options": this.options,
        "size": this.size,
        "sizeClassNames": this.sizeClassNames,
        "popupVisible": this.popupVisible,
        "highlightKeyword": this.highlightKeyword,
        "filterable": this.filterable,
        "filter": this.filter
      },
      "on": {
        "select": this.onInnerSelect
      },
      "scopedSlots": {
        option: this.$scopedSlots.option
      }
    });
    const topContent = renderLNodeJSX(this, "panelTopContent");
    const bottomContent = renderLNodeJSX(this, "panelBottomContent");
    const panelContent = topContent || listContent || bottomContent ? h("div", {
      "class": `${this.classPrefix}-autocomplete__panel`
    }, [topContent, listContent, bottomContent]) : null;
    const popupProps = {
      ...this.popupProps,
      overlayInnerStyle: this.getOverlayStyle,
      overlayInnerClassName: this.popupInnerClasses,
      overlayClassName: this.popupClasses
    };
    return h("div", {
      "class": this.classes
    }, [h(Popup, helper([{
      "attrs": {
        "visible": this.popupVisible
      }
    }, {
      "on": {
        "visible-change": this.onPopupVisibleChange
      }
    }, {
      "attrs": {
        "trigger": "focus",
        "placement": "bottom-left",
        "hideEmptyPopup": true,
        "content": panelContent ? () => panelContent : null
      }
    }, {
      "props": popupProps
    }]), [triggerNode])]);
  }
});

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