import {YRouteProps, YLocation, Route} from '../router.interface'; import * as CONTANTS from '../contants'; // import { slicePath } from '@ywfe/utils'; export const reducePath = ({ grandPath = '', parentPath = '', path, title = CONTANTS.DEFAULT_TITLE, moduleId = '', moduleName = '', menu = '', search = '', isDefault = false }: YRouteProps, routes : Route): Route => { let newParentPath = ''; if (moduleName && moduleId) { newParentPath = `/${moduleName}/${moduleId}`; } const currentLocation: YLocation = { parentPath, grandPath, path, url: `${newParentPath}${menu ? '/' : ''}${menu}${search}`, title, moduleId, moduleName, menu, children: {}, subTotal: 0 } if(!moduleId || !moduleName) { return { [path]: currentLocation } } else { currentLocation.isDefault = isDefault; // if(isDefault) { // currentLocation.url = slicePath(currentLocation.url); // } if(parentPath){ let parentRoute = routes[parentPath]; if(!parentRoute && grandPath) { parentRoute = routes[grandPath]; parentPath = grandPath; } parentRoute.children = parentRoute.children || {}; parentRoute.children = { ...parentRoute.children, [path]: currentLocation }; return { [parentPath]: { ...parentRoute, moduleId, moduleName, default: `${isDefault ? path : parentRoute.default}`, subTotal: parentRoute.children ? Object.keys(parentRoute.children).length : 0, } }; } else { return { [path]: currentLocation } } } }