import routes from '../config/routes'

export default (initialState) => {
  console.log(routes);
  console.log(initialState.permissions);
  console.log(initialState);
  const filterRoutes = (routes, actions): RouteConfig[] => {
    return routes.filter(route => {
      if (route.access && !actions.includes(route.access)) {
        return false;
      }

      if (route.routes) {
        route.routes = filterRoutes(route.routes, actions);
      }

      return true;
    });
  };

  const convertRoutesToAccessObject = (routes: RouteConfig[]): Record<string, boolean> => {
    const accessObject: Record<string, boolean> = {};

    const convertRoute = (route: RouteConfig) => {
      if (route.access) {
        accessObject[route.access] = true;
      }

      if (route.routes) {
        route.routes.forEach(convertRoute);
      }
    };

    routes.forEach(convertRoute);

    return accessObject;
  };

  let accessObject = convertRoutesToAccessObject(filterRoutes(routes, initialState.permissions));

  return {
    home: true,
    ...accessObject
  };
};
