'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
require('../../../hooks/index.cjs');
var Tree = require('../../vc-tree/src/Tree.cjs');
require('../../vc-select/index.cjs');
var valueUtil = require('./utils/valueUtil.cjs');
var LegacyContext = require('./LegacyContext.cjs');
var TreeSelectContext = require('./TreeSelectContext.cjs');
var useBaseProps = require('../../vc-select/src/hooks/useBaseProps.cjs');
var index = require('../../../hooks/use-memo/index.cjs');
var KeyCode = require('../../../utils/KeyCode.cjs');

const HIDDEN_STYLE = {
  width: 0,
  height: 0,
  display: "flex",
  overflow: "hidden",
  opacity: 0,
  border: 0,
  padding: 0,
  margin: 0
};
var OptionList = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "OptionList",
  inheritAttrs: false,
  setup(_, {
    slots,
    expose
  }) {
    const baseProps = useBaseProps.default();
    const legacyContext = LegacyContext.default();
    const context = TreeSelectContext.default();
    const treeRef = vue.ref();
    const memoTreeData = index.useMemo(() => context.treeData, [() => baseProps.open, () => context.treeData], (next) => next[0]);
    const mergedCheckedKeys = vue.computed(() => {
      const {
        checkable,
        halfCheckedKeys,
        checkedKeys
      } = legacyContext;
      if (!checkable)
        return null;
      return {
        checked: checkedKeys,
        halfChecked: halfCheckedKeys
      };
    });
    vue.watch(() => baseProps.open, () => {
      vue.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 = vue.computed(() => String(baseProps.searchValue).toLowerCase());
    const filterTreeNode = (treeNode) => {
      if (!lowerSearchValue.value)
        return false;
      return String(treeNode[legacyContext.treeNodeFilterProp]).toLowerCase().includes(lowerSearchValue.value);
    };
    const expandedKeys = vue.shallowRef(legacyContext.treeDefaultExpandedKeys);
    const searchExpandedKeys = vue.shallowRef(null);
    vue.watch(() => baseProps.searchValue, () => {
      if (baseProps.searchValue)
        searchExpandedKeys.value = valueUtil.getAllKeys(vue.toRaw(context.treeData), vue.toRaw(context.fieldNames));
    }, {
      immediate: true
    });
    const mergedExpandedKeys = vue.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 && valueUtil.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 = vue.ref(null);
    const activeEntity = vue.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.KeyCode.UP:
          case KeyCode.KeyCode.DOWN:
          case KeyCode.KeyCode.LEFT:
          case KeyCode.KeyCode.RIGHT:
            (_a = treeRef.value) == null ? void 0 : _a.onKeydown(event);
            break;
          case KeyCode.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.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 vue.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 vue.createVNode("div", {
        "onMousedown": onListMouseDown
      }, [activeEntity.value && open && vue.createVNode("span", {
        "style": HIDDEN_STYLE,
        "aria-live": "assertive"
      }, [activeEntity.value.node.value]), vue.createVNode(Tree.default, {
        "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
      })]);
    };
  }
});

exports.default = OptionList;
//# sourceMappingURL=OptionList.cjs.map
