import { IKnowledge } from '../types';

/**
 * @description 先序遍历节点
 * @param {IKnowledge} knowledge
 */
const expandAll = (knowledge: IKnowledge) => {
  if (knowledge.children && knowledge.children.length > 0) {
    // 先序遍历生成节点
    knowledge.children = knowledge.children.map((cKnowledge) => expandAll(cKnowledge));
  }
  knowledge.expanded = true;
  return knowledge;
};

const throttleFn = (fn, time) => {
  let startTime = new Date();
  return (...args) => {
    const currentTime = new Date();
    if (currentTime.valueOf() - startTime.valueOf() < time) {
      return;
    }
    startTime = currentTime;
    fn(...args);
  };
};

export { expandAll, throttleFn };
