/**
 * ldesign v0.4.6
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var VueCompositionAPI = require('@vue/composition-api');
var tree_treeItem = require('../tree-item.js');
var _common_js_tree_treeNode = require('../../_common/js/tree/tree-node.js');
var 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('../../_common/js/global-config/default-config.js');
require('../../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../../_chunks/dep-7c884501.js');
require('dayjs');
require('../../utils/ripple.js');
require('../../_common/js/utils/set-style.js');
require('../../config.js');
require('./useTreeItem.js');
require('@babel/runtime/helpers/defineProperty');
require('../../hooks/useConfig.js');
require('../../config-provider/useConfig.js');
require('../../hooks/useLazyLoad.js');
require('../../_common/js/utils/observe.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/directive.js');
require('lodash/isObject');
require('lodash/mapKeys');
require('lodash/isEqual');
require('../../loading/plugin.js');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('../../loading/loading.js');
require('../../utils/render-tnode.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../utils/transfer-dom.js');
require('../../utils/mixins.js');
require('../../loading/props.js');
require('../../loading/icon/gradient.js');
require('../../_common/js/loading/circle-adapter.js');
require('../../_common/js/utils/helper.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isNumber');
require('lodash/isArray');
require('../util.js');
require('./useRenderLabel.js');
require('../../_chunks/dep-c3e1ffac.js');
require('lodash/isBoolean');
require('../../checkbox/index.js');
require('../../utils/map-props.js');
require('../../checkbox/group.js');
require('lodash/intersection');
require('../../utils/event.js');
require('../../utils/helper.js');
require('../../checkbox/checkbox.js');
require('../../checkbox/props.js');
require('../../checkbox/checkbox-group-props.js');
require('./useRenderLine.js');
require('./useRenderOperations.js');
require('./useDraggable.js');
require('lodash/throttle');
require('@babel/runtime/helpers/asyncToGenerator');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('@babel/runtime/regenerator');
require('lodash/isFunction');
require('lodash/uniqueId');
require('lodash/isNil');
require('lodash/get');
require('../../_common/js/log/log.js');
require('../../_common/js/tree/tree-node-model.js');
require('lodash/pick');
require('lodash/omit');
require('./useTreeAction.js');

function useTreeNodes(props, context, state) {
  var treeState = state;
  var store = treeState.store,
    scope = treeState.scope,
    nodes = treeState.nodes,
    virtualConfig = treeState.virtualConfig;
  var _useTreeEvents = tree_hooks_useTreeEvents["default"](props, context, state),
    handleClick = _useTreeEvents.handleClick,
    handleChange = _useTreeEvents.handleChange;
  var renderItem = function renderItem(h, node, index) {
    var expandOnClickNode = props.expandOnClickNode;
    var rowIndex = node.__VIRTUAL_SCROLL_INDEX || index;
    var treeItem = h(tree_treeItem["default"], {
      "key": node[_common_js_tree_treeNode.privateKey],
      "attrs": {
        "rowIndex": rowIndex,
        "node": node,
        "treeScope": scope,
        "expandOnClickNode": expandOnClickNode
      },
      "on": {
        "click": handleClick,
        "change": handleChange
      }
    });
    return treeItem;
  };
  var cacheMap = /* @__PURE__ */new Map();
  var nodesEmpty = VueCompositionAPI.ref(false);
  var refresh = function refresh() {
    nodes.value = store.getNodes();
  };
  var renderTreeNodes = function renderTreeNodes(h) {
    var treeNodeViews = [];
    var isEmpty = true;
    var list = nodes.value;
    var isVirtual = virtualConfig === null || virtualConfig === void 0 ? void 0 : virtualConfig.isVirtualScroll.value;
    if (isVirtual) {
      list = virtualConfig.visibleData.value;
      nodesEmpty.value = list.length <= 0;
      treeNodeViews = list.map(function (node, index) {
        return renderItem(h, node, index);
      });
    } else {
      treeNodeViews = list.map(function (node, index) {
        var nodePrivateKey = node[_common_js_tree_treeNode.privateKey];
        var nodeView = cacheMap.get(nodePrivateKey);
        if (node.visible) {
          isEmpty = false;
          if (!nodeView) {
            nodeView = renderItem(h, node, index);
            cacheMap.set(nodePrivateKey, nodeView);
          }
        }
        return nodeView;
      });
      nodesEmpty.value = isEmpty;
      VueCompositionAPI.nextTick(function () {
        cacheMap.forEach(function (view, nodePrivateKey) {
          var node = store.privateMap.get(nodePrivateKey);
          if (!node) cacheMap["delete"](nodePrivateKey);
        });
      });
    }
    return treeNodeViews;
  };
  refresh();
  store.emitter.on("update", refresh);
  return {
    refresh: refresh,
    nodesEmpty: nodesEmpty,
    renderTreeNodes: renderTreeNodes
  };
}

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