//import { orgList } from "@/views/home/components/mockData";

// 返回可动态添加的路由
export const getAuthRouterFn = (list) => {
  let anthBtnList = [];
  const filterTreeRecursive = (nodes) => {
    return nodes.reduce((filteredNodes, node) => {
      const newNode = { ...node };
      filteredNodes.push({
        id: node.permissionId,
        name: node.permissionName,
        path: node.permissionCode,
        meta: {
          hidden: node.permissionShow == 1 ? false : true,
        },
        iconName: node.permissionIconUrl ? node.permissionIconUrl : "",
        component: () =>
          import(/*webpackChunkName:'system'*/ `@/views${node.permissionUrl}`),
        children:
          newNode.children && newNode.children.length > 0
            ? filterTreeRecursive(newNode.children)
            : undefined,
      });
      if (node.permissionType === 3) {
        anthBtnList.push(node.permissionCode);
      }
      return filteredNodes;
    }, []);
  };
  return { authRouterList: filterTreeRecursive(list), anthBtnList };
  // 将list 转成一维数组，按钮级别权限拿到 返回为[]格式
  //   const { result, anthBtnList } = flattenTree(list);
  //   const arr = filterTreeByPermissions(result, orgList);
  //   return { authRouterList: arr, anthBtnList };
  // };
  // const flattenTree = (tree) => {
  //   let result = [];
  //   let anthBtnList = [];
  //   const flattenRecursive = (nodes) => {
  //     nodes.forEach((node) => {
  //       result.push(node);
  //       if (node.children && node.children.length > 0) {
  //         flattenRecursive(node.children);
  //       }
  //       if (node.permissionDepth === 2) {
  //         anthBtnList.push(node);
  //       }
  //     });
  //   };
  //   flattenRecursive(tree);
  //   return { result, anthBtnList };
  // };
  // const filterTreeByPermissions = (permissions, tree) => {
  //   const permissionSet = new Set(permissions.map((item) => item.permissionName));
  //   const filterTreeRecursive = (nodes) => {
  //     return nodes.reduce((filteredNodes, node) => {
  //       const newNode = { ...node };
  //       if (newNode.children && newNode.children.length > 0) {
  //         newNode.children = filterTreeRecursive(newNode.children);
  //       }
  //       if (
  //         permissionSet.has(newNode.name) ||
  //         (newNode.children && newNode.children.length > 0)
  //       ) {
  //         // 找到对应的iconName
  //         const permission = permissions.find(
  //           (perm) => perm.permissionName === newNode.name
  //         );
  //         if (permission && permission.permissionShow) {
  //           newNode.meta.hidden = permission.permissionShow == 1 ? false : true;
  //         }
  //         if (permission && permission.permissionIconUrl) {
  //           newNode.iconName = permission.permissionIconUrl;
  //         } else {
  //           newNode.iconName = "";
  //         }
  //         filteredNodes.push(newNode);
  //       }
  //       return filteredNodes;
  //     }, []);
  //   };
  //   return filterTreeRecursive(tree);
};
