import { defineComponent, ref, computed, watch, nextTick, shallowRef, toRaw, createVNode } from 'vue';
import '../../../utils/index.mjs';
import '../../../hooks/index.mjs';
import VcTree from '../../vc-tree/src/Tree.mjs';
import '../../vc-select/index.mjs';
import { getAllKeys, isCheckDisabled } from './utils/valueUtil.mjs';
import useInjectLegacySelectContext from './LegacyContext.mjs';
import useInjectSelectContext from './TreeSelectContext.mjs';
import useBaseProps from '../../vc-select/src/hooks/useBaseProps.mjs';
import { useMemo } from '../../../hooks/use-memo/index.mjs';
import { KeyCode } from '../../../utils/KeyCode.mjs';

const HIDDEN_STYLE = {
  width: 0,
  height: 0,
  display: "flex",
  overflow: "hidden",
  opacity: 0,
  border: 0,
  padding: 0,
  margin: 0
};
var OptionList = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "OptionList",
  inheritAttrs: false,
  setup(_, {
    slots,
    expose
  }) {
    const baseProps = useBaseProps();
    const legacyContext = useInjectLegacySelectContext();
    const context = useInjectSelectContext();
    const treeRef = ref();
    const memoTreeData = useMemo(() => context.treeData, [() => baseProps.open, () => context.treeData], (next) => next[0]);
    const mergedCheckedKeys = computed(() => {
      const {
        checkable,
        halfCheckedKeys,
        checkedKeys
      } = legacyContext;
      if (!checkable)
        return null;
      return {
        checked: checkedKeys,
        halfChecked: halfCheckedKeys
      };
    });
    watch(() => baseProps.open, () => {
      nextTick(() => {
        var _a;
        if (baseProps.open && !baseProps.multiple && legacyContext.checkedKeys.length)
          (_a = treeRef.value) == null ? void 0 : _a.scrollTo({
            key: legacyContext.checkedKeys[0]
          });
      });
    }, {
      immediate: true,
      flush: "post"
    });
    const lowerSearchValue = computed(() => String(baseProps.searchValue).toLowerCase());
    const filterTreeNode = (treeNode) => {
      if (!lowerSearchValue.value)
        return false;
      return String(treeNode[legacyContext.treeNodeFilterProp]).toLowerCase().includes(lowerSearchValue.value);
    };
    const expandedKeys = shallowRef(legacyContext.treeDefaultExpandedKeys);
    const searchExpandedKeys = shallowRef(null);
    watch(() => baseProps.searchValue, () => {
      if (baseProps.searchValue)
        searchExpandedKeys.value = getAllKeys(toRaw(context.treeData), toRaw(context.fieldNames));
    }, {
      immediate: true
    });
    const mergedExpandedKeys = computed(() => {
      if (legacyContext.treeExpandedKeys)
        return legacyContext.treeExpandedKeys.slice();
      return baseProps.searchValue ? searchExpandedKeys.value : expandedKeys.value;
    });
    const onInternalExpand = (keys) => {
      var _a;
      expandedKeys.value = keys;
      searchExpandedKeys.value = keys;
      (_a = legacyContext.onTreeExpand) == null ? void 0 : _a.call(legacyContext, keys);
    };
    const onListMouseDown = (event) => {
      event.preventDefault();
    };
    const onInternalSelect = (_2, {
      node
    }) => {
      var _a, _b;
      const {
        checkable,
        checkedKeys
      } = legacyContext;
      if (checkable && isCheckDisabled(node))
        return;
      (_a = context.onSelect) == null ? void 0 : _a.call(context, node.key, {
        selected: !checkedKeys.includes(node.key)
      });
      if (!baseProps.multiple)
        (_b = baseProps.toggleOpen) == null ? void 0 : _b.call(baseProps, false);
    };
    const activeKey = ref(null);
    const activeEntity = computed(() => legacyContext.keyEntities[activeKey.value]);
    const setActiveKey = (key) => {
      activeKey.value = key;
    };
    expose({
      scrollTo: (...args) => {
        var _a, _b;
        return (_b = (_a = treeRef.value) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, ...args);
      },
      onKeydown: (event) => {
        var _a;
        const {
          which
        } = event;
        switch (which) {
          case KeyCode.UP:
          case KeyCode.DOWN:
          case KeyCode.LEFT:
          case KeyCode.RIGHT:
            (_a = treeRef.value) == null ? void 0 : _a.onKeydown(event);
            break;
          case KeyCode.ENTER: {
            if (activeEntity.value) {
              const {
                selectable,
                value
              } = activeEntity.value.node || {};
              if (selectable !== false) {
                onInternalSelect(null, {
                  node: {
                    key: activeKey.value
                  },
                  selected: !legacyContext.checkedKeys.includes(value)
                });
              }
            }
            break;
          }
          case KeyCode.ESC: {
            baseProps.toggleOpen(false);
          }
        }
      },
      onKeyup: () => {
      }
    });
    return () => {
      var _a;
      const {
        prefixCls,
        multiple,
        searchValue,
        open,
        notFoundContent = (_a = slots.notFoundContent) == null ? void 0 : _a.call(slots)
      } = baseProps;
      const {
        listHeight,
        listItemHeight,
        virtual,
        dropdownMatchSelectWidth,
        treeExpandAction
      } = context;
      const {
        checkable,
        treeDefaultExpandAll,
        treeIcon,
        showTreeIcon,
        switcherIcon,
        treeLine,
        loadData,
        treeLoadedKeys,
        treeMotion,
        onTreeLoad,
        checkedKeys
      } = legacyContext;
      if (memoTreeData.value.length === 0) {
        return createVNode("div", {
          "role": "listbox",
          "class": `${prefixCls}-empty`,
          "onMousedown": onListMouseDown
        }, [notFoundContent]);
      }
      const treeProps = {
        fieldNames: context.fieldNames
      };
      if (treeLoadedKeys)
        treeProps.loadedKeys = treeLoadedKeys;
      if (mergedExpandedKeys.value)
        treeProps.expandedKeys = mergedExpandedKeys.value;
      return createVNode("div", {
        "onMousedown": onListMouseDown
      }, [activeEntity.value && open && createVNode("span", {
        "style": HIDDEN_STYLE,
        "aria-live": "assertive"
      }, [activeEntity.value.node.value]), createVNode(VcTree, {
        "ref": treeRef,
        "focusable": false,
        "prefixCls": `${prefixCls}-tree`,
        "treeData": memoTreeData.value,
        "height": listHeight,
        "itemHeight": listItemHeight,
        "virtual": virtual !== false && dropdownMatchSelectWidth !== false,
        "multiple": multiple,
        "icon": treeIcon,
        "showIcon": showTreeIcon,
        "switcherIcon": switcherIcon,
        "showLine": treeLine,
        "loadData": searchValue ? null : loadData,
        "motion": treeMotion,
        "activeKey": activeKey.value,
        "checkable": checkable,
        "checkStrictly": true,
        "checkedKeys": mergedCheckedKeys.value,
        "selectedKeys": !checkable ? checkedKeys : [],
        "defaultExpandAll": treeDefaultExpandAll,
        ...treeProps,
        "onActiveChange": setActiveKey,
        "onSelect": onInternalSelect,
        "onCheck": onInternalSelect,
        "onExpand": onInternalExpand,
        "onLoad": onTreeLoad,
        "filterTreeNode": filterTreeNode,
        "expandAction": treeExpandAction
      }, {
        ...slots,
        checkable: legacyContext.customSlots.treeCheckable
      })]);
    };
  }
});

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