import { createVNode, defineComponent, computed, watchEffect, ref } from 'vue';
import { LeftOutlined, RightOutlined, LoadingOutlined } from '@ant-design/icons-vue';
import '../../../vue-components/index.mjs';
import '../../../utils/index.mjs';
import { useCompactItemContext } from '../../space/src/context.mjs';
import getIcons from '../../select/src/iconUtil.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import '../../form/index.mjs';
import { useInjectFormItemContext, FormItemInputContext } from '../../form/src/FormItemContext.mjs';
import useSelectStyle from '../../select/style/index.mjs';
import '../../config-provider/src/context/index.mjs';
import useStyle from '../style/index.mjs';
import { cascaderProps } from './props.mjs';
import { initDefaultProps } from '../../../utils/vue/initDefaultProps.mjs';
import { devWarning } from '../../../utils/log.mjs';
import { getMergedStatus, getStatusClassNames } from '../../../utils/statusUtils.mjs';
import { useInjectDisabled } from '../../config-provider/src/context/disabledContext.mjs';
import { classNames } from '../../../utils/dom/class.mjs';
import VcCascader from '../../../vue-components/vc-cascader/src/Cascader.mjs';
import { getTransitionName, getTransitionDirection } from '../../../vue-components/transition/src/transition.mjs';

function highlightKeyword(str, lowerKeyword, prefixCls) {
  const cells = str.toLowerCase().split(lowerKeyword).reduce((list, cur, index) => index === 0 ? [cur] : [...list, lowerKeyword, cur], []);
  const fillCells = [];
  let start = 0;
  cells.forEach((cell, index) => {
    const end = start + cell.length;
    let originWorld = str.slice(start, end);
    start = end;
    if (index % 2 === 1) {
      const _originWorld = function() {
        return originWorld;
      }();
      originWorld = createVNode("span", {
        "class": `${prefixCls}-menu-item-keyword`,
        "key": "seperator"
      }, [originWorld]);
    }
    fillCells.push(originWorld);
  });
  return fillCells;
}
const defaultSearchRender = ({
  inputValue,
  path,
  prefixCls,
  fieldNames
}) => {
  const optionList = [];
  const lower = inputValue.toLowerCase();
  path.forEach((node, index) => {
    if (index !== 0)
      optionList.push(" / ");
    let label = node[fieldNames.label];
    const type = typeof label;
    if (type === "string" || type === "number")
      label = highlightKeyword(String(label), lower, prefixCls);
    optionList.push(label);
  });
  return optionList;
};
var ACascader = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ACascader",
  inheritAttrs: false,
  props: initDefaultProps(cascaderProps(), {
    bordered: true,
    choiceTransitionName: "",
    allowClear: true
  }),
  setup(props, {
    attrs,
    expose,
    slots,
    emit
  }) {
    if (process.env.NODE_ENV !== "production") {
      devWarning(!props.dropdownClassName, "Cascader", "`dropdownClassName` is deprecated. Please use `popupClassName` instead.");
    }
    const formItemContext = useInjectFormItemContext();
    const formItemInputContext = FormItemInputContext.useInject();
    const mergedStatus = computed(() => getMergedStatus(formItemInputContext.status, props.status));
    const {
      prefixCls: cascaderPrefixCls,
      rootPrefixCls,
      getPrefixCls,
      direction,
      getPopupContainer,
      renderEmpty,
      size: contextSize,
      disabled
    } = useConfigInject("cascader", props);
    const prefixCls = computed(() => getPrefixCls("select", props.prefixCls));
    const {
      compactSize,
      compactItemClassnames
    } = useCompactItemContext(prefixCls, direction);
    const mergedSize = computed(() => compactSize.value || contextSize.value);
    const contextDisabled = useInjectDisabled();
    const mergedDisabled = computed(() => {
      var _a;
      return (_a = disabled.value) != null ? _a : contextDisabled.value;
    });
    const [wrapSelectSSR, hashId] = useSelectStyle(prefixCls);
    const [wrapCascaderSSR] = useStyle(cascaderPrefixCls);
    const isRtl = computed(() => direction.value === "rtl");
    if (process.env.NODE_ENV !== "production") {
      watchEffect(() => {
        devWarning(!props.multiple || !props.displayRender || !slots.displayRender, "Cascader", "`displayRender` not work on `multiple`. Please use `tagRender` instead.");
      });
    }
    const mergedShowSearch = computed(() => {
      if (!props.showSearch)
        return props.showSearch;
      let searchConfig = {
        render: defaultSearchRender
      };
      if (typeof props.showSearch === "object") {
        searchConfig = {
          ...searchConfig,
          ...props.showSearch
        };
      }
      return searchConfig;
    });
    const mergedDropdownClassName = computed(() => classNames(props.popupClassName || props.dropdownClassName, `${cascaderPrefixCls.value}-dropdown`, {
      [`${cascaderPrefixCls.value}-dropdown-rtl`]: isRtl.value
    }, hashId.value));
    const selectRef = ref();
    expose({
      focus() {
        var _a;
        (_a = selectRef.value) == null ? void 0 : _a.focus();
      },
      blur() {
        var _a;
        (_a = selectRef.value) == null ? void 0 : _a.blur();
      }
    });
    const handleChange = (...args) => {
      emit("update:value", args[0]);
      emit("change", ...args);
      formItemContext.onFieldChange();
    };
    const handleBlur = (...args) => {
      emit("blur", ...args);
      formItemContext.onFieldBlur();
    };
    const mergedShowArrow = computed(() => props.showArrow !== void 0 ? props.showArrow : props.loading || !props.multiple);
    const placement = computed(() => {
      if (props.placement !== void 0)
        return props.placement;
      return direction.value === "rtl" ? "bottomRight" : "bottomLeft";
    });
    return () => {
      var _a, _b;
      const {
        notFoundContent = (_a = slots.notFoundContent) == null ? void 0 : _a.call(slots),
        expandIcon = (_b = slots.expandIcon) == null ? void 0 : _b.call(slots),
        multiple,
        bordered,
        allowClear,
        choiceTransitionName,
        transitionName,
        id = formItemContext.id.value,
        ...restProps
      } = props;
      const mergedNotFoundContent = notFoundContent || renderEmpty("Cascader");
      let mergedExpandIcon = expandIcon;
      if (!expandIcon)
        mergedExpandIcon = isRtl.value ? createVNode(LeftOutlined, null, null) : createVNode(RightOutlined, null, null);
      const loadingIcon = createVNode("span", {
        "class": `${prefixCls.value}-menu-item-loading-icon`
      }, [createVNode(LoadingOutlined, {
        "spin": true
      }, null)]);
      const {
        suffixIcon,
        removeIcon,
        clearIcon
      } = getIcons({
        ...props,
        hasFeedback: formItemInputContext.hasFeedback,
        feedbackIcon: formItemInputContext.feedbackIcon,
        multiple,
        prefixCls: prefixCls.value,
        showArrow: mergedShowArrow.value
      }, slots);
      return wrapCascaderSSR(wrapSelectSSR(createVNode(VcCascader, {
        ...restProps,
        ...attrs,
        "id": id,
        "prefixCls": prefixCls.value,
        "class": [cascaderPrefixCls.value, {
          [`${prefixCls.value}-lg`]: mergedSize.value === "large",
          [`${prefixCls.value}-sm`]: mergedSize.value === "small",
          [`${prefixCls.value}-rtl`]: isRtl.value,
          [`${prefixCls.value}-borderless`]: !bordered,
          [`${prefixCls.value}-in-form-item`]: formItemInputContext.isFormItemInput
        }, getStatusClassNames(prefixCls.value, mergedStatus.value, formItemInputContext.hasFeedback), compactItemClassnames.value, attrs.class, hashId.value],
        "disabled": mergedDisabled.value,
        "direction": direction.value,
        "placement": placement.value,
        "notFoundContent": mergedNotFoundContent,
        "allowClear": allowClear,
        "showSearch": mergedShowSearch.value,
        "expandIcon": mergedExpandIcon,
        "inputIcon": suffixIcon,
        "removeIcon": removeIcon,
        "clearIcon": clearIcon,
        "loadingIcon": loadingIcon,
        "checkable": !!multiple,
        "dropdownClassName": mergedDropdownClassName.value,
        "dropdownPrefixCls": cascaderPrefixCls.value,
        "choiceTransitionName": getTransitionName(rootPrefixCls.value, "", choiceTransitionName),
        "transitionName": getTransitionName(rootPrefixCls.value, getTransitionDirection(placement.value), transitionName),
        "getPopupContainer": getPopupContainer == null ? void 0 : getPopupContainer.value,
        "customSlots": {
          ...slots,
          checkable: () => createVNode("span", {
            "class": `${cascaderPrefixCls.value}-checkbox-inner`
          }, null)
        },
        "tagRender": props.tagRender || slots.tagRender,
        "displayRender": props.displayRender || slots.displayRender,
        "maxTagPlaceholder": props.maxTagPlaceholder || slots.maxTagPlaceholder,
        "showArrow": formItemInputContext.hasFeedback || props.showArrow,
        "onChange": handleChange,
        "onBlur": handleBlur,
        "ref": selectRef
      }, slots)));
    };
  }
});

export { ACascader as default };
//# sourceMappingURL=Cascader.mjs.map
