{"size": 1497, "ext": "js", "max_stars_repo_path": "src/utils/authority.js", "max_stars_repo_name": "ilaipi/werobot-mng", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/utils/authority.js", "max_issues_repo_name": "ilaipi/werobot-mng", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/utils/authority.js", "max_forks_repo_name": "ilaipi/werobot-mng", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.8510638298, "max_line_length": 99, "alphanum_fraction": 0.6887107548}
import isEmpty from 'lodash/isEmpty';

// use localStorage to store the authority info, which might be sent from server in actual project.
export function getAuthority(str) {
  // return localStorage.getItem('antd-pro-authority') || ['admin', 'user'];
  const authorityString =
    typeof str === 'undefined' ? localStorage.getItem('antd-pro-authority') : str;
  // authorityString could be admin, "admin", ["admin"]
  let authority;
  try {
    authority = JSON.parse(authorityString);
  } catch (e) {
    authority = authorityString;
  }
  if (typeof authority === 'string') {
    return [authority];
  }
  return authority || ['admin'];
}

export function setAuthority(authority) {
  const proAuthority = typeof authority === 'string' ? [authority] : authority;
  return localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority));
}

const filterMenu = (menu, mapping) => {
  const currentPath = menu.path;
  if (!menu.routes) return mapping[currentPath] && menu;
  const filtered = { ...menu };
  filtered.routes = [];
  menu.routes.forEach(child => {
    const filteredChild = filterMenu(child, mapping);
    if (filteredChild) filtered.routes.push(filteredChild);
  });
  if (isEmpty(filtered.routes)) return '';
  return filtered;
};

export const filterRoutes = (routes, privileges) => {
  const filteredMenus = [];
  routes.forEach(menu => {
    const filtered = filterMenu(menu, privileges);
    if (filtered) filteredMenus.push(filtered);
  });
  return filteredMenus;
};
