interface TimeParts {
  hours: number;
  minutes: number;
  milliseconds: number;
}

interface TimeResult {
  hours: number;
  minutes: number;
}

function parseTime(time: string | null | undefined): TimeParts {
    const safeTime = time || '0:0:0'; // Provide a default string if time is null/undefined/empty
    const [hours, minutes, milliseconds] = safeTime.split(':').map(Number);
    return { hours, minutes, milliseconds };
}
  
function timeToMilliseconds({ hours, minutes, milliseconds }: TimeParts): number {
    return (hours * 60 * 60 * 1000) + (minutes * 60 * 1000) + milliseconds;
}
  
function millisecondsToTime(ms: number): TimeResult {
    const totalMinutes = Math.floor(ms / (60 * 1000));
    const hours = Math.floor(totalMinutes / 60);
    const minutes = totalMinutes % 60;
    return { hours, minutes };
}
  
export function sumTimes(times: string[]): TimeResult {
    const totalMilliseconds = times
      .map(parseTime)
      .map(timeToMilliseconds)
      .reduce((acc: number, ms: number) => acc + ms, 0);
    return millisecondsToTime(totalMilliseconds);
}
