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

import { ref, nextTick } from '@vue/composition-api';
import TreeItem from '../tree-item.js';
import { privateKey } from '../../_common/js/tree/tree-node.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 '../../_common/js/global-config/default-config.js';
import '../../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../../_chunks/dep-be599ec8.js';
import 'dayjs';
import '../../_chunks/dep-67ff4010.js';
import '../../config-provider/type.js';
import '../../utils/ripple.js';
import '../../_common/js/utils/set-style.js';
import '../../config.js';
import './useTreeItem.js';
import '@babel/runtime/helpers/defineProperty';
import '../../hooks/useConfig.js';
import '../../config-provider/useConfig.js';
import '../../hooks/useLazyLoad.js';
import '../../_common/js/utils/observe.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/directive.js';
import 'lodash/isObject';
import 'lodash/mapKeys';
import 'lodash/isEqual';
import '../../loading/plugin.js';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import '../../loading/loading.js';
import '../../utils/render-tnode.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../utils/transfer-dom.js';
import '../../utils/mixins.js';
import '../../loading/props.js';
import '../../loading/icon/gradient.js';
import '../../_common/js/loading/circle-adapter.js';
import '../../_common/js/utils/helper.js';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/isNull';
import 'lodash/isUndefined';
import 'lodash/isNumber';
import 'lodash/isArray';
import '../../loading/style';
import '../../loading/type.js';
import '../util.js';
import './useRenderLabel.js';
import '../../_chunks/dep-c89aafbb.js';
import 'lodash/isBoolean';
import '../../checkbox/index.js';
import '../../utils/map-props.js';
import '../../checkbox/group.js';
import 'lodash/intersection';
import '../../utils/event.js';
import '../../utils/helper.js';
import '../../checkbox/checkbox.js';
import '../../checkbox/props.js';
import '../../checkbox/checkbox-group-props.js';
import '../../checkbox/style';
import '../../checkbox/type.js';
import './useRenderLine.js';
import './useRenderOperations.js';
import './useDraggable.js';
import 'lodash/throttle';
import '@babel/runtime/helpers/asyncToGenerator';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '@babel/runtime/regenerator';
import 'lodash/isFunction';
import 'lodash/uniqueId';
import 'lodash/isNil';
import 'lodash/get';
import '../../_common/js/log/index.js';
import '../../_common/js/log/log.js';
import '../../_common/js/tree/tree-node-model.js';
import 'lodash/pick';
import 'lodash/omit';
import './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 = useTreeEvents(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(TreeItem, {
      "key": node[privateKey],
      "attrs": {
        "rowIndex": rowIndex,
        "node": node,
        "treeScope": scope,
        "expandOnClickNode": expandOnClickNode
      },
      "on": {
        "click": handleClick,
        "change": handleChange
      }
    });
    return treeItem;
  };
  var cacheMap = /* @__PURE__ */new Map();
  var nodesEmpty = 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[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;
      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
  };
}

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