'use strict';

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

var vue = require('vue');
var lodashEs = require('lodash-es');
var iconsVue = require('@ant-design/icons-vue');
require('../../../utils/index.cjs');
var treeUtil = require('../../../vue-components/vc-tree/src/utils/treeUtil.cjs');
var util = require('../../../vue-components/vc-tree/src/util.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
var Tree = require('./Tree.cjs');
var dictUtil = require('./utils/dictUtil.cjs');
var props = require('./props.cjs');
var initDefaultProps = require('../../../utils/vue/initDefaultProps.cjs');
var props$1 = require('../../../utils/vue/props.cjs');
var _class = require('../../../utils/dom/class.cjs');

function getIcon(props) {
  const {
    isLeaf,
    expanded
  } = props;
  if (isLeaf)
    return vue.createVNode(iconsVue.FileOutlined, null, null);
  return expanded ? vue.createVNode(iconsVue.FolderOpenOutlined, null, null) : vue.createVNode(iconsVue.FolderOutlined, null, null);
}
var ADirectoryTree = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ADirectoryTree",
  inheritAttrs: false,
  props: initDefaultProps.initDefaultProps(props.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 = vue.ref(props.treeData || treeUtil.convertTreeToData(props$1.filterEmpty((_a = slots.default) == null ? void 0 : _a.call(slots))));
    vue.watch(() => props.treeData, () => {
      treeData.value = props.treeData;
    });
    vue.onUpdated(() => {
      vue.nextTick(() => {
        var _a2;
        if (props.treeData === void 0 && slots.default)
          treeData.value = treeUtil.convertTreeToData(props$1.filterEmpty((_a2 = slots.default) == null ? void 0 : _a2.call(slots)));
      });
    });
    const lastSelectedKey = vue.ref();
    const cachedSelectedKeys = vue.ref();
    const fieldNames = vue.computed(() => treeUtil.fillFieldNames(props.fieldNames));
    const treeRef = vue.ref();
    const scrollTo = (scroll) => {
      var _a2;
      (_a2 = treeRef.value) == null ? void 0 : _a2.scrollTo(scroll);
    };
    expose({
      scrollTo,
      selectedKeys: vue.computed(() => {
        var _a2;
        return (_a2 = treeRef.value) == null ? void 0 : _a2.selectedKeys;
      }),
      checkedKeys: vue.computed(() => {
        var _a2;
        return (_a2 = treeRef.value) == null ? void 0 : _a2.checkedKeys;
      }),
      halfCheckedKeys: vue.computed(() => {
        var _a2;
        return (_a2 = treeRef.value) == null ? void 0 : _a2.halfCheckedKeys;
      }),
      loadedKeys: vue.computed(() => {
        var _a2;
        return (_a2 = treeRef.value) == null ? void 0 : _a2.loadedKeys;
      }),
      loadingKeys: vue.computed(() => {
        var _a2;
        return (_a2 = treeRef.value) == null ? void 0 : _a2.loadingKeys;
      }),
      expandedKeys: vue.computed(() => {
        var _a2;
        return (_a2 = treeRef.value) == null ? void 0 : _a2.expandedKeys;
      })
    });
    const getInitExpandedKeys = () => {
      const {
        keyEntities
      } = treeUtil.convertDataToEntities(treeData.value, {
        fieldNames: fieldNames.value
      });
      let initExpandedKeys;
      if (props.defaultExpandAll) {
        initExpandedKeys = Object.keys(keyEntities);
      } else if (props.defaultExpandParent) {
        initExpandedKeys = util.conductExpandParent(props.expandedKeys || props.defaultExpandedKeys || [], keyEntities);
      } else {
        initExpandedKeys = props.expandedKeys || props.defaultExpandedKeys;
      }
      return initExpandedKeys;
    };
    const selectedKeys = vue.ref(props.selectedKeys || props.defaultSelectedKeys || []);
    const expandedKeys = vue.ref(getInitExpandedKeys());
    vue.watch(() => props.selectedKeys, () => {
      if (props.selectedKeys !== void 0)
        selectedKeys.value = props.selectedKeys;
    }, {
      immediate: true
    });
    vue.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 = lodashEs.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 = dictUtil.convertDirectoryKeysToNodes(treeData.value, newSelectedKeys, fieldNames.value);
      } else if (multiple && shiftPick) {
        newSelectedKeys = Array.from(/* @__PURE__ */ new Set([...cachedSelectedKeys.value || [], ...dictUtil.calcRangeKeys({
          treeData: treeData.value,
          expandedKeys: expandedKeys.value,
          startKey: key,
          endKey: lastSelectedKey.value,
          fieldNames: fieldNames.value
        })]));
        newEvent.selectedNodes = dictUtil.convertDirectoryKeysToNodes(treeData.value, newSelectedKeys, fieldNames.value);
      } else {
        newSelectedKeys = [key];
        lastSelectedKey.value = key;
        cachedSelectedKeys.value = newSelectedKeys;
        newEvent.selectedNodes = dictUtil.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.default("tree", props);
    return () => {
      const connectClassName = _class.classNames(`${prefixCls.value}-directory`, {
        [`${prefixCls.value}-directory-rtl`]: direction.value === "rtl"
      }, attrs.class);
      const {
        icon = slots.icon,
        blockNode = true,
        ...otherProps
      } = props;
      return vue.createVNode(Tree.default, {
        ...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);
    };
  }
});

exports.default = ADirectoryTree;
//# sourceMappingURL=DirectoryTree.cjs.map
