import { createVNode, defineComponent, ref, watch, onUpdated, nextTick, computed } from 'vue';
import { debounce } from 'lodash-es';
import { FileOutlined, FolderOpenOutlined, FolderOutlined } from '@ant-design/icons-vue';
import '../../../utils/index.mjs';
import { convertTreeToData, fillFieldNames, convertDataToEntities } from '../../../vue-components/vc-tree/src/utils/treeUtil.mjs';
import { conductExpandParent } from '../../../vue-components/vc-tree/src/util.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import ATree from './Tree.mjs';
import { convertDirectoryKeysToNodes, calcRangeKeys } from './utils/dictUtil.mjs';
import { directoryTreeProps } from './props.mjs';
import { initDefaultProps } from '../../../utils/vue/initDefaultProps.mjs';
import { filterEmpty } from '../../../utils/vue/props.mjs';
import { classNames } from '../../../utils/dom/class.mjs';

function getIcon(props) {
  const {
    isLeaf,
    expanded
  } = props;
  if (isLeaf)
    return createVNode(FileOutlined, null, null);
  return expanded ? createVNode(FolderOpenOutlined, null, null) : createVNode(FolderOutlined, null, null);
}
var ADirectoryTree = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ADirectoryTree",
  inheritAttrs: false,
  props: initDefaultProps(directoryTreeProps(), {
    showIcon: true,
    expandAction: "click"
  }),
  slots: Object,
  // emits: [
  //   'update:selectedKeys',
  //   'update:checkedKeys',
  //   'update:expandedKeys',
  //   'expand',
  //   'select',
  //   'check',
  //   'doubleclick',
  //   'dblclick',
  //   'click',
  // ],
  setup(props, {
    attrs,
    slots,
    emit,
    expose
  }) {
    var _a;
    const treeData = ref(props.treeData || convertTreeToData(filterEmpty((_a = slots.default) == null ? void 0 : _a.call(slots))));
    watch(() => props.treeData, () => {
      treeData.value = props.treeData;
    });
    onUpdated(() => {
      nextTick(() => {
        var _a2;
        if (props.treeData === void 0 && slots.default)
          treeData.value = convertTreeToData(filterEmpty((_a2 = slots.default) == null ? void 0 : _a2.call(slots)));
      });
    });
    const lastSelectedKey = ref();
    const cachedSelectedKeys = ref();
    const fieldNames = computed(() => fillFieldNames(props.fieldNames));
    const treeRef = ref();
    const scrollTo = (scroll) => {
      var _a2;
      (_a2 = treeRef.value) == null ? void 0 : _a2.scrollTo(scroll);
    };
    expose({
      scrollTo,
      selectedKeys: computed(() => {
        var _a2;
        return (_a2 = treeRef.value) == null ? void 0 : _a2.selectedKeys;
      }),
      checkedKeys: computed(() => {
        var _a2;
        return (_a2 = treeRef.value) == null ? void 0 : _a2.checkedKeys;
      }),
      halfCheckedKeys: computed(() => {
        var _a2;
        return (_a2 = treeRef.value) == null ? void 0 : _a2.halfCheckedKeys;
      }),
      loadedKeys: computed(() => {
        var _a2;
        return (_a2 = treeRef.value) == null ? void 0 : _a2.loadedKeys;
      }),
      loadingKeys: computed(() => {
        var _a2;
        return (_a2 = treeRef.value) == null ? void 0 : _a2.loadingKeys;
      }),
      expandedKeys: computed(() => {
        var _a2;
        return (_a2 = treeRef.value) == null ? void 0 : _a2.expandedKeys;
      })
    });
    const getInitExpandedKeys = () => {
      const {
        keyEntities
      } = convertDataToEntities(treeData.value, {
        fieldNames: fieldNames.value
      });
      let initExpandedKeys;
      if (props.defaultExpandAll) {
        initExpandedKeys = Object.keys(keyEntities);
      } else if (props.defaultExpandParent) {
        initExpandedKeys = conductExpandParent(props.expandedKeys || props.defaultExpandedKeys || [], keyEntities);
      } else {
        initExpandedKeys = props.expandedKeys || props.defaultExpandedKeys;
      }
      return initExpandedKeys;
    };
    const selectedKeys = ref(props.selectedKeys || props.defaultSelectedKeys || []);
    const expandedKeys = ref(getInitExpandedKeys());
    watch(() => props.selectedKeys, () => {
      if (props.selectedKeys !== void 0)
        selectedKeys.value = props.selectedKeys;
    }, {
      immediate: true
    });
    watch(() => props.expandedKeys, () => {
      if (props.expandedKeys !== void 0)
        expandedKeys.value = props.expandedKeys;
    }, {
      immediate: true
    });
    const expandFolderNode = (event, node) => {
      const {
        isLeaf
      } = node;
      if (isLeaf || event.shiftKey || event.metaKey || event.ctrlKey)
        return;
      treeRef.value.onNodeExpand(event, node);
    };
    const onDebounceExpand = debounce(expandFolderNode, 200, {
      leading: true
    });
    const onExpand = (keys, info) => {
      if (props.expandedKeys === void 0)
        expandedKeys.value = keys;
      emit("update:expandedKeys", keys);
      emit("expand", keys, info);
    };
    const onClick = (event, node) => {
      const {
        expandAction
      } = props;
      if (expandAction === "click")
        onDebounceExpand(event, node);
      emit("click", event, node);
    };
    const onDoubleClick = (event, node) => {
      const {
        expandAction
      } = props;
      if (expandAction === "dblclick" || expandAction === "doubleclick")
        onDebounceExpand(event, node);
      emit("doubleclick", event, node);
      emit("dblclick", event, node);
    };
    const onSelect = (keys, event) => {
      const {
        multiple
      } = props;
      const {
        node,
        nativeEvent
      } = event;
      const key = node[fieldNames.value.key];
      const newEvent = {
        ...event,
        selected: true
        // Directory selected always true
      };
      const ctrlPick = (nativeEvent == null ? void 0 : nativeEvent.ctrlKey) || (nativeEvent == null ? void 0 : nativeEvent.metaKey);
      const shiftPick = nativeEvent == null ? void 0 : nativeEvent.shiftKey;
      let newSelectedKeys;
      if (multiple && ctrlPick) {
        newSelectedKeys = keys;
        lastSelectedKey.value = key;
        cachedSelectedKeys.value = newSelectedKeys;
        newEvent.selectedNodes = convertDirectoryKeysToNodes(treeData.value, newSelectedKeys, fieldNames.value);
      } else if (multiple && shiftPick) {
        newSelectedKeys = Array.from(/* @__PURE__ */ new Set([...cachedSelectedKeys.value || [], ...calcRangeKeys({
          treeData: treeData.value,
          expandedKeys: expandedKeys.value,
          startKey: key,
          endKey: lastSelectedKey.value,
          fieldNames: fieldNames.value
        })]));
        newEvent.selectedNodes = convertDirectoryKeysToNodes(treeData.value, newSelectedKeys, fieldNames.value);
      } else {
        newSelectedKeys = [key];
        lastSelectedKey.value = key;
        cachedSelectedKeys.value = newSelectedKeys;
        newEvent.selectedNodes = convertDirectoryKeysToNodes(treeData.value, newSelectedKeys, fieldNames.value);
      }
      emit("update:selectedKeys", newSelectedKeys);
      emit("select", newSelectedKeys, newEvent);
      if (props.selectedKeys === void 0)
        selectedKeys.value = newSelectedKeys;
    };
    const onCheck = (checkedObjOrKeys, eventObj) => {
      emit("update:checkedKeys", checkedObjOrKeys);
      emit("check", checkedObjOrKeys, eventObj);
    };
    const {
      prefixCls,
      direction
    } = useConfigInject("tree", props);
    return () => {
      const connectClassName = classNames(`${prefixCls.value}-directory`, {
        [`${prefixCls.value}-directory-rtl`]: direction.value === "rtl"
      }, attrs.class);
      const {
        icon = slots.icon,
        blockNode = true,
        ...otherProps
      } = props;
      return createVNode(ATree, {
        ...attrs,
        "icon": icon || getIcon,
        "ref": treeRef,
        "blockNode": blockNode,
        ...otherProps,
        "prefixCls": prefixCls.value,
        "class": connectClassName,
        "expandedKeys": expandedKeys.value,
        "selectedKeys": selectedKeys.value,
        "onSelect": onSelect,
        "onClick": onClick,
        "onDblclick": onDoubleClick,
        "onExpand": onExpand,
        "onCheck": onCheck
      }, slots);
    };
  }
});

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