const MOBILE_USER_PROFILE_KEY = 'insuite_mobile_user_profile'; function normalizeText(value) { return String(value ?? '').trim(); } function normalizeLowerText(value) { return normalizeText(value).toLowerCase(); } function normalizeMobileTechnicianType(value) { const technicianType = normalizeLowerText(value); if (technicianType === 'etude' || technicianType === 'etude_raccordement') { return 'raccordement'; } return technicianType; } export function normalizeMobileUserProfile(profile) { if (!profile || typeof profile !== 'object') { return null; } return { id: Number(profile.id || 0), name: normalizeText(profile.name), email: normalizeText(profile.email), role_key: normalizeLowerText(profile.role_key), technician_type: normalizeMobileTechnicianType(profile.technician_type), assigned_city: normalizeText(profile.assigned_city), }; } export function getStoredUserProfile() { try { return normalizeMobileUserProfile(JSON.parse(localStorage.getItem(MOBILE_USER_PROFILE_KEY) || 'null')); } catch { return null; } } export function setStoredUserProfile(profile) { const normalized = normalizeMobileUserProfile(profile); if (!normalized) { localStorage.removeItem(MOBILE_USER_PROFILE_KEY); return null; } localStorage.setItem(MOBILE_USER_PROFILE_KEY, JSON.stringify(normalized)); return normalized; } export function clearStoredUserProfile() { localStorage.removeItem(MOBILE_USER_PROFILE_KEY); } export function deriveMobileExperience(profile) { const normalizedProfile = normalizeMobileUserProfile(profile); const technicianType = normalizedProfile?.technician_type || ''; if (technicianType === 'raccordement') { return { profile: normalizedProfile, technicianType, moduleKey: 'raccordement', moduleLabel: 'Raccordement', moduleListTitle: 'Raccordements', moduleHeroSubtitle: 'Portefeuille terrain des raccordements clients assignés.', moduleMetricLabel: 'Raccordements', moduleInProgressLabel: 'Raccordements en cours', moduleActionLabel: 'Ouvrir raccordement', moduleOpenActionLabel: 'Voir raccordements', moduleNotificationLabel: 'mission(s) raccordement', defaultRoute: '/dashboard', navItems: [ { path: '/dashboard', label: 'Dashboard', icon: 'fa-chart-line', activePaths: ['/dashboard'] }, { path: '/ftth', label: 'Raccordement', icon: 'fa-network-wired', activePaths: ['/ftth', '/ftth-report'] }, ], canAccessPath(path) { return ['/dashboard', '/ftth', '/ftth-report'].includes(path); }, }; } if (technicianType === 'maintenance_ftth') { return { profile: normalizedProfile, technicianType, moduleKey: 'maintenance_ftth', moduleLabel: 'Maintenance Ftth', moduleListTitle: 'Maintenance Ftth', moduleHeroSubtitle: 'Portefeuille terrain des interventions FTTH assignées.', moduleMetricLabel: 'Maintenance Ftth', moduleInProgressLabel: 'FTTH en cours', moduleActionLabel: 'Ouvrir maintenance', moduleOpenActionLabel: 'Voir fiches', moduleNotificationLabel: 'mission(s) FTTH', defaultRoute: '/dashboard', navItems: [ { path: '/dashboard', label: 'Dashboard', icon: 'fa-chart-line', activePaths: ['/dashboard'] }, { path: '/ftth', label: 'Maintenance Ftth', icon: 'fa-network-wired', activePaths: ['/ftth', '/ftth-report'] }, { path: '/nearby', label: 'Proximité', icon: 'fa-location-crosshairs', activePaths: ['/nearby'] }, ], canAccessPath(path) { return ['/dashboard', '/ftth', '/ftth-report', '/nearby'].includes(path); }, }; } if (technicianType === 'backbones') { return { profile: normalizedProfile, technicianType, moduleKey: 'backbones', moduleLabel: 'Incidents', moduleListTitle: 'Incidents', moduleHeroSubtitle: 'Traitement terrain et suivi des tickets backbone assignés.', moduleMetricLabel: 'Incidents', moduleInProgressLabel: 'Incidents urgents', moduleActionLabel: 'Ouvrir incidents', moduleOpenActionLabel: 'Traiter urgences', moduleNotificationLabel: 'incident(s)', defaultRoute: '/dashboard', navItems: [ { path: '/dashboard', label: 'Dashboard', icon: 'fa-chart-line', activePaths: ['/dashboard'] }, { path: '/assigned', label: 'Incidents', icon: 'fa-triangle-exclamation', activePaths: ['/assigned', '/treatment'] }, ], canAccessPath(path) { return ['/dashboard', '/assigned', '/treatment'].includes(path); }, }; } return { profile: normalizedProfile, technicianType, moduleKey: 'mixed', moduleLabel: 'FTTH', moduleListTitle: 'FTTH', moduleHeroSubtitle: 'Portefeuille terrain FTTH et raccordements assignés.', moduleMetricLabel: 'Missions FTTH', moduleInProgressLabel: 'FTTH en cours', moduleActionLabel: 'Ouvrir FTTH', moduleOpenActionLabel: 'Voir fiches', moduleNotificationLabel: 'mission(s) FTTH', defaultRoute: '/dashboard', navItems: [ { path: '/dashboard', label: 'Dashboard', icon: 'fa-chart-line', activePaths: ['/dashboard'] }, { path: '/assigned', label: 'Incidents', icon: 'fa-triangle-exclamation', activePaths: ['/assigned', '/treatment'] }, { path: '/nearby', label: 'Proximité', icon: 'fa-location-crosshairs', activePaths: ['/nearby'] }, { path: '/ftth', label: 'FTTH', icon: 'fa-network-wired', activePaths: ['/ftth', '/ftth-report'] }, ], canAccessPath() { return true; }, }; }