import { DageRouteItem } from "@/router";

export const getParentMenuItem = (
  menuItems: DageRouteItem[],
  path: string
): null | DageRouteItem => {
  for (const menuItem of menuItems) {
    if (path.startsWith(menuItem.path || "")) {
      return menuItem;
    }
    if (menuItem.children) {
      const parentMenuItem = getParentMenuItem(menuItem.children, path);
      if (parentMenuItem) {
        return parentMenuItem;
      }
    }
  }
  return null;
};

export const findRouteByPath = (
  routes: DageRouteItem[],
  path: string
): DageRouteItem | null => {
  for (const route of routes) {
    if (route.path === path) {
      return route;
    }
    if (route.children) {
      const subRoute = findRouteByPath(route.children, path);
      if (subRoute) {
        return subRoute;
      }
    }
  }
  return null;
};
