import { defineComponent, ref, computed, watch, onMounted, resolveComponent, createBlock, openBlock, withCtx, createVNode, normalizeClass, unref, createCommentVNode, normalizeStyle } from 'vue';
import '../../../utils/index.mjs';
import { uniqBy } from 'lodash-es';
import { emailInputEmits, emailInputProps } from './email-input.mjs';
import { createNamespace } from '../../../utils/create-namespace.mjs';

"use strict";
var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{
    name: "LlEmailInput"
  },
  __name: "email-input",
  props: emailInputProps,
  emits: emailInputEmits,
  setup(__props, { emit: __emit }) {
    const bem = createNamespace("email-input");
    const props = __props;
    const emit = __emit;
    const inputV = ref("");
    const selectV = ref();
    const processedOptions = computed(() => {
      return uniqBy(
        props.options.map((option) => {
          const processedValue = option.startsWith("@") ? option : `@${option}`;
          return {
            label: processedValue,
            value: processedValue
          };
        }),
        "value"
        // 根据 value 属性去重
      );
    });
    const splitEmailValue = (value) => {
      if (!props.showSelect || !value) {
        return { prefix: value, suffix: null };
      }
      for (const option of processedOptions.value) {
        const suffix = option.value;
        if (value.endsWith(suffix)) {
          return {
            prefix: value.slice(0, value.length - suffix.length),
            suffix
          };
        }
      }
      return { prefix: value, suffix: null };
    };
    const initSplitValues = () => {
      const { prefix, suffix } = splitEmailValue(props.value);
      inputV.value = prefix;
      selectV.value = suffix;
    };
    const handleChange = () => {
      let newValue = inputV.value;
      if (props.showSelect && selectV.value) {
        newValue += selectV.value;
      }
      emit("update:value", newValue);
      emit("change", newValue);
    };
    watch(
      () => props.value,
      (newValue) => {
        const { prefix, suffix } = splitEmailValue(newValue);
        if (prefix !== inputV.value || suffix !== selectV.value) {
          inputV.value = prefix;
          selectV.value = suffix;
        }
      }
    );
    watch(
      () => props.showSelect,
      (newShowSelect, oldShowSelect) => {
        if (oldShowSelect && !newShowSelect) {
          if (selectV.value) {
            inputV.value += selectV.value;
            selectV.value = null;
          }
          emit("update:value", inputV.value);
          emit("change", inputV.value);
        } else if (!oldShowSelect && newShowSelect) {
          initSplitValues();
        }
      }
    );
    onMounted(() => {
      initSplitValues();
    });
    return (_ctx, _cache) => {
      const _component_ll_input = resolveComponent("ll-input");
      const _component_ll_select = resolveComponent("ll-select");
      const _component_a_space_compact = resolveComponent("a-space-compact");
      const _component_a_form_item_rest = resolveComponent("a-form-item-rest");
      return openBlock(), createBlock(_component_a_form_item_rest, null, {
        default: withCtx(() => [
          createVNode(_component_a_space_compact, {
            block: "",
            class: normalizeClass(unref(bem).b())
          }, {
            default: withCtx(() => [
              createVNode(_component_ll_input, {
                value: inputV.value,
                "onUpdate:value": _cache[0] || (_cache[0] = ($event) => inputV.value = $event),
                class: normalizeClass(unref(bem).e("input")),
                placeholder: _ctx.placeholder,
                disabled: _ctx.disabled,
                "allow-clear": _ctx.allowClear,
                onChange: handleChange
              }, null, 8, ["value", "class", "placeholder", "disabled", "allow-clear"]),
              _ctx.showSelect ? (openBlock(), createBlock(_component_ll_select, {
                key: 0,
                value: selectV.value,
                "onUpdate:value": _cache[1] || (_cache[1] = ($event) => selectV.value = $event),
                class: normalizeClass(unref(bem).e("select")),
                options: processedOptions.value,
                style: normalizeStyle({ minWidth: _ctx.selectMinW }),
                placeholder: "\u8BF7\u9009\u62E9",
                disabled: _ctx.disabled,
                onChange: handleChange
              }, null, 8, ["value", "class", "options", "style", "disabled"])) : createCommentVNode("v-if", true)
            ]),
            _: 1
            /* STABLE */
          }, 8, ["class"])
        ]),
        _: 1
        /* STABLE */
      });
    };
  }
});

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