/* Al-Huwais — application composition and route host */
const MODULES={
dashboard:window.RoleDashboard,aicos:window.AICOSConsole,customers:window.Customers,leads:window.Leads,quotations:window.Quotations,contracts:window.Contracts,receiving:window.Receiving,delivery:window.Delivery,invoices:window.Invoices,receivables:window.Receivables,payables:window.Payables,expenses:window.Expenses,vat:window.VAT,budgeting:window.Budgeting,planning:window.Planning,'my-jobs':window.MyJobs,execution:window.Execution,certificates:window.Certificates,standards:window.Standards,uncertainty:window.Uncertainty,environment:window.Environment,traceability:window.Traceability,ncr:window.NCR,complaints:window.Complaints,risk:window.Risk,audit:window.Audit,management:window.Management,documents:window.Documents,competency:window.Competency,proficiency:window.Proficiency,incidents:window.Incidents,employees:window.Employees,training:window.Training,leave:window.Leave,attendance:window.Attendance,nitaqat:window.Nitaqat,'goods-receipt':window.GoodsReceipt,inventory:window.Inventory,suppliers:window.Suppliers,portal:window.Portal};
const NAV_TITLES=Object.fromEntries((window.NAV_GROUPS||[]).flatMap(g=>g.items.map(x=>[x[0],x[1]])));
function ModuleHost({id,onNav}){const Component=MODULES[id];return Component?<Component onNav={onNav}/>:<div className="page"><EmptyState title="Module unavailable" desc={`No component is registered for ${id}.`}/></div>}
function defaultModule(){return window.ROLE==='Customer'?'portal':'dashboard'}
function App(){const [tick,setTick]=useState(0),[active,setActive]=useState(defaultModule),[notif,setNotif]=useState(false),[ai,setAI]=useState(false);window.__bump=()=>setTick(x=>x+1);useEffect(()=>{const allowed=window.allowedNav?window.allowedNav(window.ROLE).flatMap(g=>g.items.map(x=>x[0])):Object.keys(MODULES);if(!allowed.includes(active))setActive(defaultModule())},[tick,active]);const navigate=id=>{setActive(id);setNotif(false);setAI(false)};return <div className="app">{window.Sidebar&&<Sidebar active={active} onNav={navigate}/>}<main className="main">{window.Topbar&&<Topbar title={NAV_TITLES[active]||'Dashboard'} onOpenNotif={()=>setNotif(true)} onOpenAI={()=>setAI(true)}/>}<section className="content"><ModuleHost id={active} onNav={navigate}/></section></main>{window.NotifPanel&&<NotifPanel open={notif} onClose={()=>setNotif(false)} onNav={navigate}/>} {window.AICOSPanel&&<AICOSPanel open={ai} onClose={()=>setAI(false)}/>} {window.ToastHost&&<ToastHost/>}{window.FormHost&&<FormHost/>}</div>}
Object.assign(window,{App,ModuleHost,MODULES});
ReactDOM.createRoot(document.getElementById('root')).render(<App/>);
