import { defineComponent, shallowRef, computed, watch, createVNode, mergeProps } from 'vue';
import classNames from '../_util/classNames.mjs';
import PropTypes from '../_util/vue-types/index.mjs';
import '../vc-mentions/index.mjs';
import { mentionsProps as mentionsProps$1 } from '../vc-mentions/src/mentionsProps.mjs';
import '../hooks/index.mjs';
import { flattenChildren, getOptionProps } from '../_util/props-util/index.mjs';
import { useInjectFormItemContext, FormItemInputContext } from '../form/FormItemContext.mjs';
import omit from '../_util/omit.mjs';
import { optionOptions, optionProps } from '../vc-mentions/src/Option.mjs';
import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils.mjs';
import { useProvideOverride } from '../menu/src/OverrideContext.mjs';
import warning from '../_util/warning.mjs';
import '../spin/index.mjs';
import devWarning from '../vc-util/devWarning.mjs';
import useStyle from './style/index.mjs';
import useConfigInject from '../config-provider/hooks/useConfigInject.mjs';
import Spin from '../spin/Spin.mjs';
import Mentions$1 from '../vc-mentions/src/Mentions.mjs';

function loadingFilterOption() {
  return true;
}
const getMentions = (value = "", config = {}) => {
  const {
    prefix = "@",
    split = " "
  } = config;
  const prefixList = Array.isArray(prefix) ? prefix : [prefix];
  return value.split(split).map((str = "") => {
    let hitPrefix = null;
    prefixList.some((prefixStr) => {
      const startStr = str.slice(0, prefixStr.length);
      if (startStr === prefixStr) {
        hitPrefix = prefixStr;
        return true;
      }
      return false;
    });
    if (hitPrefix !== null) {
      return {
        prefix: hitPrefix,
        value: str.slice(hitPrefix.length)
      };
    }
    return null;
  }).filter((entity) => !!entity && !!entity.value);
};
const mentionsProps = () => ({
  ...mentionsProps$1,
  "loading": {
    type: Boolean,
    default: void 0
  },
  "onFocus": {
    type: Function
  },
  "onBlur": {
    type: Function
  },
  "onSelect": {
    type: Function
  },
  "onChange": {
    type: Function
  },
  "onPressenter": {
    type: Function
  },
  "onUpdate:value": {
    type: Function
  },
  "notFoundContent": PropTypes.any,
  "defaultValue": String,
  "id": String,
  "status": String
});
const Mentions = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "AMentions",
  inheritAttrs: false,
  props: mentionsProps(),
  slots: Object,
  setup(props, {
    slots,
    emit,
    attrs,
    expose
  }) {
    var _a, _b, _c;
    if (process.env.NODE_ENV !== "production") {
      devWarning(!flattenChildren(((_a = slots.default) == null ? void 0 : _a.call(slots)) || []).length, "Mentions", "`Mentions.Option` is deprecated. Please use `options` instead.");
    }
    const {
      prefixCls,
      renderEmpty,
      direction
    } = useConfigInject("mentions", props);
    const [wrapSSR, hashId] = useStyle(prefixCls);
    const focused = shallowRef(false);
    const vcMentions = shallowRef(null);
    const value = shallowRef((_c = (_b = props.value) != null ? _b : props.defaultValue) != null ? _c : "");
    const formItemContext = useInjectFormItemContext();
    const formItemInputContext = FormItemInputContext.useInject();
    const mergedStatus = computed(() => getMergedStatus(formItemInputContext.status, props.status));
    useProvideOverride({
      prefixCls: computed(() => `${prefixCls.value}-menu`),
      mode: computed(() => "vertical"),
      selectable: computed(() => false),
      onClick: () => {
      },
      validator: ({
        mode
      }) => {
        warning(!mode || mode === "vertical", "Mentions", `mode="${mode}" is not supported for Mentions's Menu.`);
      }
    });
    watch(() => props.value, (val) => {
      value.value = val;
    });
    const handleFocus = (e) => {
      focused.value = true;
      emit("focus", e);
    };
    const handleBlur = (e) => {
      focused.value = false;
      emit("blur", e);
      formItemContext.onFieldBlur();
    };
    const handleSelect = (...args) => {
      emit("select", ...args);
      focused.value = true;
    };
    const handleChange = (val) => {
      if (props.value === void 0)
        value.value = val;
      emit("update:value", val);
      emit("change", val);
      formItemContext.onFieldChange();
    };
    const getNotFoundContent = () => {
      const notFoundContent = props.notFoundContent;
      if (notFoundContent !== void 0)
        return notFoundContent;
      if (slots.notFoundContent)
        return slots.notFoundContent();
      return renderEmpty("Select");
    };
    const getOptions = () => {
      var _a2;
      return flattenChildren(((_a2 = slots.default) == null ? void 0 : _a2.call(slots)) || []).map((item) => {
        var _a3, _b2;
        return {
          ...getOptionProps(item),
          label: (_b2 = (_a3 = item.children) == null ? void 0 : _a3.default) == null ? void 0 : _b2.call(_a3)
        };
      });
    };
    const focus = () => {
      vcMentions.value.focus();
    };
    const blur = () => {
      vcMentions.value.blur();
    };
    expose({
      focus,
      blur
    });
    const mentionsfilterOption = computed(() => props.loading ? loadingFilterOption : props.filterOption);
    return () => {
      const {
        disabled,
        getPopupContainer,
        rows = 1,
        id = formItemContext.id.value,
        ...restProps
      } = props;
      const {
        hasFeedback,
        feedbackIcon
      } = formItemInputContext;
      const {
        class: className,
        ...otherAttrs
      } = attrs;
      const otherProps = omit(restProps, ["defaultValue", "onUpdate:value", "prefixCls"]);
      const mergedClassName = classNames({
        [`${prefixCls.value}-disabled`]: disabled,
        [`${prefixCls.value}-focused`]: focused.value,
        [`${prefixCls.value}-rtl`]: direction.value === "rtl"
      }, getStatusClassNames(prefixCls.value, mergedStatus.value), !hasFeedback && className, hashId.value);
      const mentionsProps2 = {
        prefixCls: prefixCls.value,
        ...otherProps,
        disabled,
        direction: direction.value,
        filterOption: mentionsfilterOption.value,
        getPopupContainer,
        options: props.loading ? [{
          value: "ANTDV_SEARCHING",
          disabled: true,
          label: createVNode(Spin, {
            "size": "small"
          }, null)
        }] : props.options || getOptions(),
        class: mergedClassName,
        ...otherAttrs,
        rows,
        onChange: handleChange,
        onSelect: handleSelect,
        onFocus: handleFocus,
        onBlur: handleBlur,
        ref: vcMentions,
        value: value.value,
        id
      };
      const mentions = createVNode(Mentions$1, mergeProps(mentionsProps2, {
        "dropdownClassName": hashId.value
      }), {
        notFoundContent: getNotFoundContent,
        option: slots.option
      });
      if (hasFeedback) {
        return wrapSSR(createVNode("div", {
          "class": classNames(`${prefixCls.value}-affix-wrapper`, getStatusClassNames(`${prefixCls.value}-affix-wrapper`, mergedStatus.value, hasFeedback), className, hashId.value)
        }, [mentions, createVNode("span", {
          "class": `${prefixCls.value}-suffix`
        }, [feedbackIcon])]));
      }
      return wrapSSR(mentions);
    };
  }
});
const MentionsOption = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  ...optionOptions,
  name: "AMentionsOption",
  props: optionProps
});
var index = Object.assign(Mentions, {
  Option: MentionsOption,
  getMentions,
  install: (app) => {
    app.component(Mentions.name, Mentions);
    app.component(MentionsOption.name, MentionsOption);
    return app;
  }
});

export { MentionsOption, index as default, mentionsProps };
//# sourceMappingURL=index.mjs.map
