import RProgress from 'ray-progress';
import { loginURI } from './../consts/urlConsts';
import calcRoutes from './calcRoutes';
import addCustomRoutes, { customRoutes } from './customRoutes';
import pageCompontent, { RootView,RootBizView,businessRouts } from './view';

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: RootView,
      childRoutes: [...mainChilds]
    },
    {
      path: 'biz',
      component: RootBizView,
      childRoutes: [...bizChilds, ...customRoutes, ...businessRouts]
    }
  ];
  return routes;
};

const progressStart = () => {
  RProgress.start();
};

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

export default rootRoutes;
