// src/utils/permission.js
export function extractButtonPermissions(routers) {
    const buttonPermissions = {};
    function traverse(nodes) {
      nodes.forEach(node => {
        if (node.ResType === "按钮") {
          const parentPath = findParentPath(routers, node.ParentId);
          if (parentPath) {
            if (!buttonPermissions[parentPath]) {
              buttonPermissions[parentPath] = [];
            }
            buttonPermissions[parentPath].push(node.AliasName);
          }
        }
        if (node.Children && node.Children.length > 0) {
          traverse(node.Children);
        }
      });
    }
  
    traverse(routers);
    return buttonPermissions;
  }
  
 
  function findParentPath(nodes, parentId) {
    for (const node of nodes) {
      if (node.Id === parentId) {
        return node.Path;
      }
      if (node.Children && node.Children.length > 0) {
        const path = findParentPath(node.Children, parentId);
        if (path) return path;
      }
    }
    return null;
  }
  
  export function hasPermission(buttonPermissions, pagePath, buttonAlias) {
    return buttonPermissions[pagePath]?.includes(buttonAlias) || false;
  }