import{r,a as I,b as G,u as w,d as A,j as e,N as T,B as E,T as R,M as L,L as B}from"./index-D4ZMoJFe.js";import{A as D}from"./arrow-right-CbCEXg_0.js";const F=[{module:"SCF",icon:E,titleKey:"moduleSelector.cards.scf.title",subtitleKey:"moduleSelector.cards.scf.subtitle",tagsKey:"moduleSelector.cards.scf.tags",accent:"scf",target:"/dashboard"},{module:"MGX",icon:R,titleKey:"moduleSelector.cards.mgx.title",subtitleKey:"moduleSelector.cards.mgx.subtitle",tagsKey:"moduleSelector.cards.mgx.tags",accent:"mgx",target:"/mgx/dashboard"},{module:"TRACKING",icon:L,titleKey:"moduleSelector.cards.tracking.title",subtitleKey:"moduleSelector.cards.tracking.subtitle",tagsKey:"moduleSelector.cards.tracking.tags",accent:"tracking",target:"/tracking/map"}],U=c=>String(c||"").trim().split(/\s+/)[0]||"Utilisateur",_=c=>{const t=String(c).trim().split(/\s+/).filter(Boolean);return t.length?t.length===1?t[0][0].toUpperCase():(t[0][0]+t[t.length-1][0]).toUpperCase():"?"};function X(){const[c,t]=r.useState(!1),[h,g]=r.useState(""),[p,x]=r.useState(""),[o,j]=r.useState(null),{user:a,setActiveModule:f,selectedCompanyId:S,selectedCompany:n}=I(),{t:l}=G(),k=w(),i=(a==null?void 0:a.allowedModules)||(a==null?void 0:a.allowed_modules)||[],d=S||(n==null?void 0:n.id)||(a==null?void 0:a.companyId)||"";if(r.useEffect(()=>{!d||!i.includes("TRACKING")||A.get("/tracking/devices/quick-count",{params:{company_id:d}}).then(s=>{j(s.data.item||null)}).catch(()=>{j(null)})},[i,d]),!i.some(s=>["SCF","MGX","TRACKING"].includes(s)))return e.jsx(T,{to:"/login",replace:!0});const y=async(s,m)=>{x(""),g(s);try{await f(s,{setDefault:c}),k(m,{replace:!0})}catch(u){x(u.message||"Impossible de sélectionner ce module.")}finally{g("")}},v=F.filter(s=>i.includes(s.module)),K=new Date().getFullYear(),N=(a==null?void 0:a.fullName)||(a==null?void 0:a.email)||"Utilisateur";return e.jsxs("div",{className:"module-selector",children:[e.jsx("div",{className:"ms-bg","aria-hidden":"true"}),e.jsx("div",{className:"ms-grid-overlay","aria-hidden":"true"}),e.jsxs("div",{className:"ms-shell",children:[e.jsxs("header",{className:"ms-topbar",children:[e.jsxs(B,{to:"/",className:"ms-brand",children:[e.jsx("span",{className:"ms-brand-mark",children:e.jsx("img",{src:"/landing/mekahli-logo.jpg",alt:"Logo Groupe Mekahli"})}),e.jsxs("span",{children:[e.jsx("span",{className:"ms-brand-name",children:"Groupe Mekahli"}),e.jsx("span",{className:"ms-brand-sub",children:"Espace collaborateurs"})]})]}),e.jsxs("div",{className:"ms-user",children:[e.jsx("span",{className:"ms-user-avatar",children:_(N)}),e.jsxs("span",{children:[e.jsx("span",{className:"ms-user-name",children:N}),e.jsx("span",{className:"ms-user-role",children:(n==null?void 0:n.name)||(a==null?void 0:a.role)||"Connecté"})]})]})]}),e.jsxs("section",{className:"ms-header",children:[e.jsx("span",{className:"ms-eyebrow",children:l("moduleSelector.kicker")}),e.jsxs("h1",{className:"ms-title",children:[l("moduleSelector.title").replace("{name}",""),e.jsxs("em",{children:[U(a==null?void 0:a.fullName),"."]})]}),e.jsx("p",{className:"ms-desc",children:l("moduleSelector.description")})]}),p?e.jsx("div",{className:"ms-error",children:p}):null,e.jsx("div",{className:"ms-grid",children:v.map((s,m)=>{const u=s.icon,C=h===s.module,M=l(s.tagsKey)||[];return e.jsxs("button",{type:"button",className:`ms-card accent-${s.accent}`,onClick:()=>y(s.module,s.target),disabled:!!h,children:[e.jsxs("div",{className:"ms-card-head",children:[e.jsx("span",{className:"ms-card-icon",children:e.jsx(u,{size:26,strokeWidth:1.6})}),e.jsxs("span",{className:"ms-card-num",children:[String(m+1).padStart(2,"0")," / ",s.module]})]}),e.jsx("h2",{className:"ms-card-title",children:l(s.titleKey)}),e.jsx("p",{className:"ms-card-sub",children:l(s.subtitleKey)}),e.jsxs("div",{className:"ms-card-tags",children:[M.map(b=>e.jsx("span",{className:"ms-card-tag",children:b},b)),s.module==="TRACKING"&&o?e.jsxs("span",{className:"ms-card-tag live",children:[o.total_devices," téléphones · ",o.online_devices," en ligne"]}):null]}),e.jsxs("span",{className:"ms-card-cta",children:[e.jsxs("span",{style:{display:"inline-flex",alignItems:"center",gap:8},children:[C&&e.jsx("span",{className:"ms-spin","aria-hidden":"true"}),l("moduleSelector.access")]}),e.jsx("span",{className:"ms-card-cta-arrow","aria-hidden":"true",children:e.jsx(D,{size:14})})]})]},s.module)})}),e.jsxs("footer",{className:"ms-footer",children:[e.jsxs("label",{className:"ms-default",children:[e.jsx("input",{type:"checkbox",checked:c,onChange:s=>t(s.target.checked)}),l("moduleSelector.setDefault")]}),e.jsxs("span",{className:"ms-meta",children:["© ",K," Groupe Mekahli"]})]})]})]})}export{X as default};
