import { defineComponent, useAttrs, ref, computed, resolveComponent, createBlock, openBlock, mergeProps, unref, createSlots, withCtx, renderSlot, renderList, normalizeProps, guardReactiveProps } from 'vue';
import '../../../utils/index.mjs';
import { autoCompleteProps } from './auto-complete.mjs';
import { createNamespace } from '../../../utils/create-namespace.mjs';

"use strict";
var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{ name: "LlAutoComplete" },
  __name: "auto-complete",
  props: autoCompleteProps,
  emits: ["update:value", "change", "blur", "focus", "select"],
  setup(__props, { emit: __emit }) {
    const attrs = useAttrs();
    const bem = createNamespace("auto-complete");
    const props = __props;
    const emits = __emit;
    const isBlur = ref(true);
    const innerValue = computed({
      get() {
        return props.value || "";
      },
      set(val) {
        emits("update:value", val);
        emits("change", val);
      }
    });
    const open = computed(() => {
      if (isBlur.value && props.expandOpportunity !== "focus") return false;
      if (props.expandOpportunity === "character") {
        if (!props.triggerCharacter || isBlur.value) return false;
        const result = props.triggerCharacter.split(/[,，\s]+/).some((char) => innerValue.value.endsWith(char));
        return result;
      } else if (props.expandOpportunity === "change") {
        if (isBlur.value) return false;
        return !!innerValue.value;
      } else {
        return void 0;
      }
    });
    const newFilterOption = computed(() => {
      if (typeof props.filterOption === "boolean") {
        return props.filterOption ? defaultFilterOption : () => true;
      }
      if (typeof props.filterOption === "function") {
        return props.filterOption;
      }
      return defaultFilterOption;
    });
    const defaultFilterOption = (input, option) => {
      const compare = (str) => props.caseSensitive ? str : str.toUpperCase();
      return compare(option.value).includes(compare(input));
    };
    const filterOption = (input, option) => {
      return newFilterOption.value(input, option);
    };
    const handleKeyDown = (e, type) => {
      if (e.key === "Enter" && type === "textarea") {
        if (e.shiftKey) {
          e.stopPropagation();
        } else {
          e.preventDefault();
        }
      }
      if (props.ignoreSelection && e.key === "Enter" && innerValue.value) {
        if (!props.options.some(
          (item) => String(item.value).includes(String(innerValue.value))
        )) {
          e.stopPropagation();
        }
      }
    };
    const onSelect = (value) => {
      if (props.appendMode) {
        const suffix = props.appendCharacter + value;
        if (innerValue.value.includes(suffix)) {
          innerValue.value = innerValue.value?.trim() || "";
        } else {
          innerValue.value = `${innerValue.value}${innerValue.value.trim().endsWith(props.appendCharacter) ? "" : props.appendCharacter}${value}`;
        }
      } else {
        innerValue.value = value;
      }
      emits("select", value);
    };
    const handleBlur = () => {
      isBlur.value = true;
      emits("blur");
    };
    const handleFocus = () => {
      isBlur.value = false;
      emits("focus");
    };
    return (_ctx, _cache) => {
      const _component_ll_input = resolveComponent("ll-input");
      const _component_ll_textarea = resolveComponent("ll-textarea");
      const _component_a_auto_complete = resolveComponent("a-auto-complete");
      return openBlock(), createBlock(_component_a_auto_complete, mergeProps(unref(attrs), {
        value: innerValue.value,
        "onUpdate:value": _cache[2] || (_cache[2] = ($event) => innerValue.value = $event),
        open: open.value,
        class: [unref(bem).b(), unref(bem).is(_ctx.isGray ? "gray-auto-complete" : "")],
        "filter-option": filterOption,
        options: _ctx.options,
        onSelect,
        onBlur: handleBlur,
        onFocus: handleFocus
      }), createSlots({
        default: withCtx(() => [
          renderSlot(_ctx.$slots, "default", {}, () => [
            !_ctx.isMultiLine ? (openBlock(), createBlock(_component_ll_input, {
              key: 0,
              "allow-clear": _ctx.allowClear,
              placeholder: _ctx.placeholder,
              bordered: _ctx.bordered,
              "is-gray": _ctx.isGray,
              onPressEnter: _cache[0] || (_cache[0] = (e) => handleKeyDown(e, "input"))
            }, null, 8, ["allow-clear", "placeholder", "bordered", "is-gray"])) : (openBlock(), createBlock(_component_ll_textarea, {
              key: 1,
              "allow-clear": _ctx.allowClear,
              placeholder: _ctx.placeholder,
              "is-gray": _ctx.isGray,
              bordered: _ctx.bordered,
              onPressEnter: _cache[1] || (_cache[1] = (e) => handleKeyDown(e, "textarea"))
            }, null, 8, ["allow-clear", "placeholder", "is-gray", "bordered"]))
          ])
        ]),
        _: 2
        /* DYNAMIC */
      }, [
        renderList(Object.keys(_ctx.$slots).filter((name) => name !== "default"), (item) => {
          return {
            name: item,
            fn: withCtx((data) => [
              renderSlot(_ctx.$slots, item, normalizeProps(guardReactiveProps(data)))
            ])
          };
        })
      ]), 1040, ["value", "open", "class", "options"]);
    };
  }
});

export { _sfc_main as default };
//# sourceMappingURL=auto-complete.vue2.mjs.map
