import { Feature } from '@common-stack/client-react'; const routeConfigKeyReplace = (keyReplacedWith: any, features: any,appLayout=null as any,intialRouteName='MainStack.Layout' as any) => { let appLayoutFeatures = { ...features }; appLayoutFeatures.routeConfig = appLayoutFeatures?.routeConfig?.map((obj: any) => { let rObj: any = { ...obj }; if (rObj.hasOwnProperty('/')) rObj['/'].props.initialRouteName = intialRouteName; let objectEntries = Object.entries(rObj).map(([key, val]: any) => { let objVal: any = Object.assign({}, val); let initialParams = objVal?.props?.initialParams ?? {}; if (objVal?.props?.options) { initialParams.menu_position = objVal?.menu_position ?? 'bottom'; initialParams.priority = objVal?.props?.options?.priority ?? 0; initialParams.options = objVal.props.options ? JSON.stringify(objVal?.props?.options) : null; objVal.props.initialParams = initialParams; } let objKey: any; if(keyReplacedWith=='//host_tab/'){ objKey = key.startsWith('//:orgName/h/') ?key.replace('//:orgName/h/', '//:orgName'+keyReplacedWith?.replace('//','/')) :key.startsWith('//h/') ?key.replace('//h/', keyReplacedWith) :key.startsWith('//:orgName/l/')?null :key.startsWith('//l/') ? null :key; } else{ objKey = key.startsWith('//:orgName/l/') ? key.replace('//:orgName/l/', '//:orgName'+keyReplacedWith?.replace('//','/')) : key.startsWith('//l/') ? key.replace('//l/', keyReplacedWith) : key.startsWith('//:orgName/h/') ? null : key.startsWith('//h/') ? null : key; } return [objKey, objVal]; }); return Object.fromEntries(objectEntries); }); let appLayoutConfig:any=null; if(appLayout) { let layoutFeatures = [...features?.routeConfig] //const intialRoutename = layoutFeatures?.map((obj:any)=>obj.hasOwnProperty('/') && obj['/'].props.initialRouteName)?.filter((obj:any)=>obj)[0]??'MainStack.MainDrawer'; const isOrgName = layoutFeatures?.map((obj: any)=>{ return Object.entries(obj).map(([key, val]: any) => { if(keyReplacedWith=='//host_tab/'){ if(key.startsWith('//:orgName/h/')) return true; else return null; } else{ if(key.startsWith('//:orgName/l/')) return true; else null; } }) })?.flat(1)?.filter((obj:any)=>obj)??[]; appLayoutConfig = {...appLayout}; appLayoutConfig.routeConfig = appLayoutConfig?.routeConfig?.map((obj:any)=>{ let rObj: any = { ...obj }; //if (rObj.hasOwnProperty('/')) rObj['/'].props.initialRouteName = intialRoutename; let objectEntries = Object.entries(rObj).map(([key, val]: any) => { let objVal: any = Object.assign({}, val); let objKey: any; objKey = isOrgName?.length > 0 ? key.startsWith('//')? key.replace('//', '//:orgName/') : key : key; return [objKey, objVal]; }); return Object.fromEntries(objectEntries); }) } const appFeatures = new Feature(appLayoutConfig, appLayoutFeatures); return {appFeatures,appLayoutConfig,appLayoutFeatures}; }; export default routeConfigKeyReplace;