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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var helper = require('../../chunks/helper.js');
var VueCompositionAPI = require('@vue/composition-api');
var components_input_index = require('../input/index.js');
var components_popup_index = require('../popup/index.js');
var hooks_useCommonClassName = require('../../hooks/useCommonClassName.js');
var hooks_useVModel = require('../../hooks/useVModel.js');
var components_configProvider_useConfig = require('../config-provider/useConfig.js');
var utils_renderNode = require('../../utils/render-node.js');
var components_autoComplete_optionList = require('./option-list.js');
var components_autoComplete_props = require('./props.js');
require('../../utils/map-props.js');
require('vue');
require('../../utils/withInstall.js');
require('lodash/capitalize');
require('../../config.js');
require('../input/input.js');
require('@ldesign/icons-vue');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../chunks/helper2.js');
require('../../chunks/log.js');
require('../../chunks/_commonjsHelpers-fe796db6.js');
require('../../utils/helper.js');
require('../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../../chunks/zh_CN.js');
require('../../utils/mixins.js');
require('../../utils/event.js');
require('../input/props.js');
require('lodash');
require('../input/group.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../../chunks/set-style.js');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('../popup/props.js');
require('../popup/container.js');
require('lodash/isFunction');
require('../../hooks/useConfig.js');
require('./highlight-option.js');

var _AutoComplete = VueCompositionAPI.defineComponent({
  name: "LAutoComplete",
  props: components_autoComplete_props["default"],
  setup(props2, _ref) {
    let {
      emit
    } = _ref;
    const {
      value
    } = VueCompositionAPI.toRefs(props2);
    const [tValue, setTValue] = hooks_useVModel["default"](value, props2.defaultValue, props2.onChange);
    const {
      classPrefix,
      sizeClassNames
    } = hooks_useCommonClassName["default"]();
    const {
      global
    } = components_configProvider_useConfig.useConfig("input");
    const popupVisible = VueCompositionAPI.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 = VueCompositionAPI.computed(() => [`${classPrefix.value}-auto-complete`]);
    const popupClasses = VueCompositionAPI.computed(() => {
      let classes2 = [`${classPrefix.value}-select__dropdown`];
      if (props2.popupProps?.overlayClassName) classes2 = classes2.concat(props2.popupProps.overlayClassName);
      return classes2;
    });
    const popupInnerClasses = VueCompositionAPI.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 = VueCompositionAPI.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 = VueCompositionAPI.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 = utils_renderNode.renderContent(this, "default", "triggerElement") || h(components_input_index.Input, helper.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(components_autoComplete_optionList["default"], {
      "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 = utils_renderNode.renderLNodeJSX(this, "panelTopContent");
    const bottomContent = utils_renderNode.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(components_popup_index.Popup, helper.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])]);
  }
});

exports["default"] = _AutoComplete;
//# sourceMappingURL=auto-complete.js.map
