import type { CampusInfo } from '@/shared/types/app';
import type { CampusCatalogItem } from '@/shared/types/campuses';

export const DEFAULT_CAMPUS_LABEL = 'Current Campus';

export function getCampusDisplayName(name: string): string {
  return name.replace(/\s+Campus$/u, '');
}

export function toCampusInfo(campus: CampusCatalogItem): CampusInfo | null {
  const campusId = campus.code.trim().toLowerCase();

  if (!campusId) {
    return null;
  }

  return {
    id: campusId,
    mascot: campus.mascot || getCampusDisplayName(campus.name),
    fullName: campus.name,
    color: campus.color || '',
    bgGradient: campus.bgGradient || '',
    borderColor: campus.borderColor || '',
    textColor: campus.textColor || '',
    bgLight: campus.bgLight || '',
    description: campus.description || '',
    isOnline: campus.isOnline,
  };
}

export function findCampusByNameOrCode(
  campuses: readonly CampusInfo[],
  value: string,
): CampusInfo | undefined {
  const normalizedValue = value.trim().toLowerCase();

  return campuses.find((campus) => (
    campus.id === normalizedValue
    || campus.mascot.toLowerCase() === normalizedValue
    || campus.fullName.toLowerCase() === normalizedValue
  ));
}
