
import RProgress from 'ray-progress';
import { loginURI } from './../consts/urlConsts';
import calcRoutes from './calcRoutes';
import addCustomRoutes, { customRoutes } from './customRoutes';
import pageCompontent, { AsyncRootView, AsyncLogin, AsyncRootBizView,businessRouts } from './asyncView';


const groups = [
  'main', 'biz'
];

const injectRoutes = menus => {
  const { main = [], biz = [] } = calcRoutes(menus, pageCompontent, groups) || {};
  const mainChilds = addCustomRoutes(main);
  const bizChilds = addCustomRoutes(biz);

  const routes = [
    {
      path: 'main',
      component: AsyncRootView,
      childRoutes: [...mainChilds]
    },
    {
      path: 'biz',
      component: AsyncRootBizView,
      childRoutes: [...bizChilds, ...customRoutes, ...businessRouts]
    }
  ];
  return routes;
};

const progressStart = () => {
  RProgress.start();
};
const rootRoutes = menus => {
  return {
    path: '/',
    onChange: progressStart,
    indexRedirect: AsyncLogin,
    indexRoute: {
      component: AsyncLogin,
      onEnter(nextState, replace) {
        window.location.href = loginURI;
      }
    },
    childRoutes: injectRoutes(menus)
  };
};

export default rootRoutes;
