'use strict';

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

var vue = require('vue');
require('../../../utils/index.js');
var lodashEs = require('lodash-es');
var emailInput = require('./email-input.js');
var createNamespace = require('../../../utils/create-namespace.js');

"use strict";
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{
    name: "LlEmailInput"
  },
  __name: "email-input",
  props: emailInput.emailInputProps,
  emits: emailInput.emailInputEmits,
  setup(__props, { emit: __emit }) {
    const bem = createNamespace.createNamespace("email-input");
    const props = __props;
    const emit = __emit;
    const inputV = vue.ref("");
    const selectV = vue.ref();
    const processedOptions = vue.computed(() => {
      return lodashEs.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);
    };
    vue.watch(
      () => props.value,
      (newValue) => {
        const { prefix, suffix } = splitEmailValue(newValue);
        if (prefix !== inputV.value || suffix !== selectV.value) {
          inputV.value = prefix;
          selectV.value = suffix;
        }
      }
    );
    vue.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();
        }
      }
    );
    vue.onMounted(() => {
      initSplitValues();
    });
    return (_ctx, _cache) => {
      const _component_ll_input = vue.resolveComponent("ll-input");
      const _component_ll_select = vue.resolveComponent("ll-select");
      const _component_a_space_compact = vue.resolveComponent("a-space-compact");
      const _component_a_form_item_rest = vue.resolveComponent("a-form-item-rest");
      return vue.openBlock(), vue.createBlock(_component_a_form_item_rest, null, {
        default: vue.withCtx(() => [
          vue.createVNode(_component_a_space_compact, {
            block: "",
            class: vue.normalizeClass(vue.unref(bem).b())
          }, {
            default: vue.withCtx(() => [
              vue.createVNode(_component_ll_input, {
                value: inputV.value,
                "onUpdate:value": _cache[0] || (_cache[0] = ($event) => inputV.value = $event),
                class: vue.normalizeClass(vue.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 ? (vue.openBlock(), vue.createBlock(_component_ll_select, {
                key: 0,
                value: selectV.value,
                "onUpdate:value": _cache[1] || (_cache[1] = ($event) => selectV.value = $event),
                class: vue.normalizeClass(vue.unref(bem).e("select")),
                options: processedOptions.value,
                style: vue.normalizeStyle({ minWidth: _ctx.selectMinW }),
                placeholder: "\u8BF7\u9009\u62E9",
                disabled: _ctx.disabled,
                onChange: handleChange
              }, null, 8, ["value", "class", "options", "style", "disabled"])) : vue.createCommentVNode("v-if", true)
            ]),
            _: 1
            /* STABLE */
          }, 8, ["class"])
        ]),
        _: 1
        /* STABLE */
      });
    };
  }
});

exports.default = _sfc_main;
//# sourceMappingURL=email-input.vue2.js.map
