/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

import { ref, nextTick } from '@vue/composition-api';
import TreeItem from '../tree-item.js';
import useTreeEvents from './useTreeEvents.js';
import '../../config-provider/config-receiver.js';
import 'vue';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import 'lodash/merge';
import '../../../_chunks/dep-b4fd1955.js';
import '../../../utils/ripple.js';
import '../../../_chunks/dep-c68dc4b6.js';
import '../../../config.js';
import './useTreeItem.js';
import '../../../hooks/useConfig.js';
import '../../config-provider/useConfig.js';
import './useItemEvents.js';
import './useRenderIcon.js';
import '@ldesign/icons-vue';
import '../../../hooks/useGlobalIcon.js';
import '../../loading/index.js';
import '../../../utils/withInstall.js';
import 'lodash/capitalize';
import '../../loading/loading.js';
import '../../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../../_chunks/dep-eaa95999.js';
import '../../../_chunks/dep-102361be.js';
import '../../../utils/easing.js';
import '../../../utils/render-node.js';
import 'lodash';
import '../../../utils/transfer-dom.js';
import '../../../utils/mixins.js';
import '../../loading/props.js';
import '../../loading/icon/gradient.js';
import '../../../_chunks/dep-aacc31a3.js';
import '../../../_chunks/dep-1a6bca30.js';
import '../../loading/plugin.js';
import '../utils.js';
import 'lodash/camelCase';
import './useRenderLabel.js';
import '../../checkbox/index.js';
import '../../../utils/map-props.js';
import '../../checkbox/group.js';
import '../../../_chunks/dep-b0f1ed22.js';
import 'lodash/intersection';
import '../../../utils/event.js';
import '../../../utils/helper.js';
import '../../checkbox/checkbox.js';
import '../../checkbox/props.js';
import './useRenderLine.js';
import './useRenderOperations.js';
import './useDraggable.js';
import 'lodash/throttle';
import './useTreeAction.js';

function useTreeNodes(props, context, state) {
  const {
    store
  } = state;
  const {
    handleClick,
    handleChange
  } = useTreeEvents(props, context, state);
  const renderItem = (h, node) => {
    const {
      cache
    } = state;
    const {
      expandOnClickNode
    } = props;
    const {
      scope
    } = cache;
    const treeItem = h(TreeItem, {
      "key": node.value,
      "attrs": {
        "node": node,
        "treeScope": scope,
        "expandOnClickNode": expandOnClickNode
      },
      "on": {
        "click": handleClick,
        "change": handleChange
      }
    });
    return treeItem;
  };
  const cacheMap = /* @__PURE__ */new Map();
  const clearCacheNodes = () => {
    cacheMap.clear();
  };
  const nodes = ref([]);
  const nodesFilterEmpty = ref(false);
  const refresh = () => {
    nodes.value = store.getNodes();
  };
  const renderTreeNodes = h => {
    let isFilterEmpty = true;
    const treeNodeViews = nodes.value.map(node => {
      let nodeView = cacheMap.get(node.value);
      if (node.visible) {
        isFilterEmpty = false;
        if (!nodeView) {
          nodeView = renderItem(h, node);
          cacheMap.set(node.value, nodeView);
        }
      }
      return nodeView;
    });
    nodesFilterEmpty.value = isFilterEmpty;
    nextTick(() => {
      cacheMap.forEach((view, value) => {
        if (!store.getNode(value)) cacheMap.delete(value);
      });
    });
    return treeNodeViews;
  };
  refresh();
  store.emitter.on("update", refresh);
  return {
    nodesFilterEmpty,
    clearCacheNodes,
    renderTreeNodes
  };
}

export { useTreeNodes as default };
//# sourceMappingURL=useTreeNodes.js.map
