import { defineComponent, ref, computed, shallowRef, watchEffect, onMounted, watch, createVNode } from 'vue';
import useBaseProps from '../../../vc-select/src/hooks/useBaseProps.mjs';
import { isLeaf, toPathKey, toPathValueStr, toPathKeys, scrollIntoParentView } from '../utils/commonUtil.mjs';
import { toPathOptions } from '../utils/treeUtil.mjs';
import { useInjectCascader } from '../context.mjs';
import useKeyboard from './useKeyboard.mjs';
import useActive from './useActive.mjs';
import Column, { FIX_LABEL } from './Column.mjs';

var OptionList = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "OptionList",
  inheritAttrs: false,
  setup(_props, context) {
    const {
      attrs,
      slots
    } = context;
    const baseProps = useBaseProps();
    const containerRef = ref();
    const rtl = computed(() => baseProps.direction === "rtl");
    const {
      options,
      values,
      halfValues,
      fieldNames,
      changeOnSelect,
      onSelect,
      searchOptions,
      dropdownPrefixCls,
      loadData,
      expandTrigger,
      customSlots
    } = useInjectCascader();
    const mergedPrefixCls = computed(() => dropdownPrefixCls.value || baseProps.prefixCls);
    const loadingKeys = shallowRef([]);
    const internalLoadData = (valueCells) => {
      if (!loadData.value || baseProps.searchValue)
        return;
      const optionList = toPathOptions(valueCells, options.value, fieldNames.value);
      const rawOptions = optionList.map(({
        option
      }) => option);
      const lastOption = rawOptions[rawOptions.length - 1];
      if (lastOption && !isLeaf(lastOption, fieldNames.value)) {
        const pathKey = toPathKey(valueCells);
        loadingKeys.value = [...loadingKeys.value, pathKey];
        loadData.value(rawOptions);
      }
    };
    watchEffect(() => {
      if (loadingKeys.value.length) {
        loadingKeys.value.forEach((loadingKey) => {
          const valueStrCells = toPathValueStr(loadingKey);
          const optionList = toPathOptions(valueStrCells, options.value, fieldNames.value, true).map(({
            option
          }) => option);
          const lastOption = optionList[optionList.length - 1];
          if (!lastOption || lastOption[fieldNames.value.children] || isLeaf(lastOption, fieldNames.value))
            loadingKeys.value = loadingKeys.value.filter((key) => key !== loadingKey);
        });
      }
    });
    const checkedSet = computed(() => new Set(toPathKeys(values.value)));
    const halfCheckedSet = computed(() => new Set(toPathKeys(halfValues.value)));
    const [activeValueCells, setActiveValueCells] = useActive();
    const onPathOpen = (nextValueCells) => {
      setActiveValueCells(nextValueCells);
      internalLoadData(nextValueCells);
    };
    const isSelectable = (option) => {
      const {
        disabled
      } = option;
      const isMergedLeaf = isLeaf(option, fieldNames.value);
      return !disabled && (isMergedLeaf || changeOnSelect.value || baseProps.multiple);
    };
    const onPathSelect = (valuePath, leaf, fromKeyboard = false) => {
      onSelect(valuePath);
      if (!baseProps.multiple && (leaf || changeOnSelect.value && (expandTrigger.value === "hover" || fromKeyboard)))
        baseProps.toggleOpen(false);
    };
    const mergedOptions = computed(() => {
      if (baseProps.searchValue)
        return searchOptions.value;
      return options.value;
    });
    const optionColumns = computed(() => {
      const optionList = [{
        options: mergedOptions.value
      }];
      let currentList = mergedOptions.value;
      for (let i = 0; i < activeValueCells.value.length; i += 1) {
        const activeValueCell = activeValueCells.value[i];
        const currentOption = currentList.find((option) => option[fieldNames.value.value] === activeValueCell);
        const subOptions = currentOption == null ? void 0 : currentOption[fieldNames.value.children];
        if (!(subOptions == null ? void 0 : subOptions.length))
          break;
        currentList = subOptions;
        optionList.push({
          options: subOptions
        });
      }
      return optionList;
    });
    const onKeyboardSelect = (selectValueCells, option) => {
      if (isSelectable(option))
        onPathSelect(selectValueCells, isLeaf(option, fieldNames.value), true);
    };
    useKeyboard(context, mergedOptions, fieldNames, activeValueCells, onPathOpen, onKeyboardSelect);
    const onListMouseDown = (event) => {
      event.preventDefault();
    };
    onMounted(() => {
      watch(activeValueCells, (cells) => {
        var _a;
        for (let i = 0; i < cells.length; i += 1) {
          const cellPath = cells.slice(0, i + 1);
          const cellKeyPath = toPathKey(cellPath);
          const ele = (_a = containerRef.value) == null ? void 0 : _a.querySelector(
            `li[data-path-key="${cellKeyPath.replace(/\\{0,2}"/g, '\\"')}"]`
          );
          if (ele)
            scrollIntoParentView(ele);
        }
      }, {
        flush: "post",
        immediate: true
      });
    });
    return () => {
      var _a, _b, _c, _d, _e;
      const {
        notFoundContent = ((_a = slots.notFoundContent) == null ? void 0 : _a.call(slots)) || ((_c = (_b = customSlots.value).notFoundContent) == null ? void 0 : _c.call(_b)),
        multiple,
        toggleOpen
      } = baseProps;
      const isEmpty = !((_e = (_d = optionColumns.value[0]) == null ? void 0 : _d.options) == null ? void 0 : _e.length);
      const emptyList = [{
        [fieldNames.value.value]: "__EMPTY__",
        [FIX_LABEL]: notFoundContent,
        disabled: true
      }];
      const columnProps = {
        ...attrs,
        multiple: !isEmpty && multiple,
        onSelect: onPathSelect,
        onActive: onPathOpen,
        onToggleOpen: toggleOpen,
        checkedSet: checkedSet.value,
        halfCheckedSet: halfCheckedSet.value,
        loadingKeys: loadingKeys.value,
        isSelectable
      };
      const mergedOptionColumns = isEmpty ? [{
        options: emptyList
      }] : optionColumns.value;
      const columnNodes = mergedOptionColumns.map((col, index) => {
        const prevValuePath = activeValueCells.value.slice(0, index);
        const activeValue = activeValueCells.value[index];
        return createVNode(Column, {
          "key": index,
          ...columnProps,
          "prefixCls": mergedPrefixCls.value,
          "options": col.options,
          "prevValuePath": prevValuePath,
          "activeValue": activeValue
        }, null);
      });
      return createVNode("div", {
        "class": [`${mergedPrefixCls.value}-menus`, {
          [`${mergedPrefixCls.value}-menu-empty`]: isEmpty,
          [`${mergedPrefixCls.value}-rtl`]: rtl.value
        }],
        "onMousedown": onListMouseDown,
        "ref": containerRef
      }, [columnNodes]);
    };
  }
});

export { OptionList as default };
//# sourceMappingURL=index.mjs.map
