import { getAuthRoleLabel } from '@/business/auth/selectors';
import type { CampusInfo, Module, ModuleId, UserRole } from '@/shared/types/app';

export function getAccessibleModules(
  modules: readonly Module[],
  userRole: UserRole,
): readonly Module[] {
  return modules.filter((module) => module.roles.includes(userRole));
}

export function canAccessModule(
  modules: readonly Module[],
  moduleId: ModuleId,
  userRole: UserRole,
): boolean {
  return Boolean(modules.find((module) => module.id === moduleId)?.roles.includes(userRole));
}

export function getAccessibleModuleId(
  modules: readonly Module[],
  moduleId: ModuleId,
  userRole: UserRole,
): ModuleId {
  return canAccessModule(modules, moduleId, userRole) ? moduleId : 'dashboard';
}

export function getSidebarRoleLabel(role: UserRole): string {
  return getAuthRoleLabel(role);
}

export function getSidebarCampusInitial(campusInfo?: CampusInfo): string | null {
  const mascot = campusInfo?.mascot.trim();

  if (!mascot) {
    return null;
  }

  return mascot.charAt(0).toUpperCase();
}

export function shouldShowMobileSidebarOverlay(isMobile: boolean, mobileSidebarOpen: boolean): boolean {
  return isMobile && mobileSidebarOpen;
}
