function pluralize(text: string, count: number) {
  if (count === 1) {
    return text;
  }
  return text + "n";
}

export function humanizeDuration(minutes: number) {
  const hours = Math.floor(minutes / 60);
  const remainingMinutes = minutes % 60;

  if (hours === 0 && minutes < 16) {
    return pluralize(`${remainingMinutes} Minute`, remainingMinutes);
  }

  // Remaining minutes are rounded to 15 mins
  const roundToMinutes = 15;
  const roundedMinutes = Math.round((minutes % 60) / roundToMinutes) * roundToMinutes;

  const hoursString = hours > 0 ? pluralize(`${hours} Stunde`, hours) : "";

  const showMinutesUpToHours = 10;
  const minutesString =
    roundedMinutes > 0 && hours < showMinutesUpToHours
      ? pluralize(`${roundedMinutes} Minute`, roundedMinutes)
      : "";

  const delimiter = hoursString && minutesString ? " " : "";
  return `${hoursString}${delimiter}${minutesString}`;
}
