/*
 * @Author: your name
 * @Date: 2024-05-21 14:21:20
 * @LastEditTime: 2024-05-21 17:36:17
 * @LastEditors: bogon
 * @Description: In User Settings Edit
 * @FilePath: /performance-test/src/utils/getMenu.js
 */
export const checkListFn = (data) => {
  let list = [];
  recursiveFind(data);
  function recursiveFind(data) {
    data.forEach((item) => {
      //   if (item.permissionType === 1 && item.checked === 1) {
      if (item.checked === 1) {
        list.push(item.permissionId);
      }
      if (item.children && item.children.length > 0) {
        recursiveFind(item.children); // 递归遍历子节点
      }
    });
  }
  return list;
};

// data 是一个树形数组结构数据，checkIdList是一个一维数组
// 需求：递归循环data，data.permissionId在checkIdList这个数组中，父级节点和祖先节点的permissionId也需要push到新的一维数组，这个方法如何改？
export const getcheckListFn = (checkIdList, data) => {
  let list = [];
  console.log(checkIdList, "checkIdList");
  function recursiveFind(node, ancestors = []) {
    if (checkIdList.includes(node.permissionId)) {
      // 将当前节点的 permissionId 加入到列表中
      list.push(node.permissionId);

      // 将当前节点的祖先节点的 permissionId 加入到列表中
      ancestors.forEach((ancestor) => {
        list.push(ancestor.permissionId);
      });
    }
    // 递归遍历子节点
    if (node.children && node.children.length > 0) {
      node.children.forEach((child) => {
        recursiveFind(child, [...ancestors, node]);
      });
    }
  }
  // 从根节点开始递归遍历
  data.forEach((root) => {
    recursiveFind(root);
  });

  return list;
};
