import * as routes from '../constants';

type MappedRouteType = {
  path: string;
  permissions: string[] | null;
};

let mappedRoutes: MappedRouteType[];

function getAppRoutes() {
  if (!mappedRoutes) {
    mappedRoutes = Object.values(routes).map((route) => ({
      ...route,
      path: route.path.split('/')[1],
    }));
  }

  return mappedRoutes;
}

export default function getRoutesAllowed(permissions: string[]) {
  return getAppRoutes()
    .filter((route) => route?.permissions?.some((permission: string) => permissions?.includes(permission)))
    .map((route) => route.path);
}
