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

'use strict';

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

var VueCompositionAPI = require('@vue/composition-api');
var components_tree_treeItem = require('../tree-item.js');
var components_tree_hooks_useTreeEvents = require('./useTreeEvents.js');
require('../../config-provider/config-receiver.js');
require('vue');
require('lodash/mergeWith');
require('../../config-provider/context.js');
require('lodash/merge');
require('../../../chunks/zh_CN.js');
require('../../../utils/ripple.js');
require('../../../chunks/set-style.js');
require('../../../config.js');
require('./useTreeItem.js');
require('../../../hooks/useConfig.js');
require('../../config-provider/useConfig.js');
require('./useItemEvents.js');
require('./useRenderIcon.js');
require('@ldesign/icons-vue');
require('../../../hooks/useGlobalIcon.js');
require('../../loading/index.js');
require('../../../utils/withInstall.js');
require('lodash/capitalize');
require('../../loading/loading.js');
require('../../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../../chunks/log.js');
require('../../../chunks/_commonjsHelpers-fe796db6.js');
require('../../../utils/easing.js');
require('../../../utils/render-node.js');
require('lodash');
require('../../../utils/transfer-dom.js');
require('../../../utils/mixins.js');
require('../../loading/props.js');
require('../../loading/icon/gradient.js');
require('../../../chunks/defineProperty-19da3d4c.js');
require('../../../chunks/helper2.js');
require('../../loading/plugin.js');
require('../utils.js');
require('lodash/camelCase');
require('./useRenderLabel.js');
require('../../checkbox/index.js');
require('../../../utils/map-props.js');
require('../../checkbox/group.js');
require('../../../chunks/helper.js');
require('lodash/intersection');
require('../../../utils/event.js');
require('../../../utils/helper.js');
require('../../checkbox/checkbox.js');
require('../../checkbox/props.js');
require('./useRenderLine.js');
require('./useRenderOperations.js');
require('./useDraggable.js');
require('lodash/throttle');
require('./useTreeAction.js');

function useTreeNodes(props, context, state) {
  const {
    store
  } = state;
  const {
    handleClick,
    handleChange
  } = components_tree_hooks_useTreeEvents["default"](props, context, state);
  const renderItem = (h, node) => {
    const {
      cache
    } = state;
    const {
      expandOnClickNode
    } = props;
    const {
      scope
    } = cache;
    const treeItem = h(components_tree_treeItem["default"], {
      "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 = VueCompositionAPI.ref([]);
  const nodesFilterEmpty = VueCompositionAPI.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;
    VueCompositionAPI.nextTick(() => {
      cacheMap.forEach((view, value) => {
        if (!store.getNode(value)) cacheMap.delete(value);
      });
    });
    return treeNodeViews;
  };
  refresh();
  store.emitter.on("update", refresh);
  return {
    nodesFilterEmpty,
    clearCacheNodes,
    renderTreeNodes
  };
}

exports["default"] = useTreeNodes;
//# sourceMappingURL=useTreeNodes.js.map
