/**
 * Duration formatting utilities
 *
 * Jellyfin uses "ticks" for duration where 10,000,000 ticks = 1 second
 */

/**
 * Convert Jellyfin ticks to formatted duration string
 * @param ticks Duration in Jellyfin ticks (10M ticks = 1 second)
 * @param format Format type: "mm:ss" (default) or "hh:mm:ss"
 * @returns Formatted duration string or empty string if no ticks
 */
export function formatDuration(ticks?: number | null, format: "mm:ss" | "hh:mm:ss" = "mm:ss"): string {
  if (!ticks) return "";

  // Jellyfin uses 10,000,000 ticks per second
  const TICKS_PER_SECOND = 10000000;
  const totalSeconds = Math.floor(ticks / TICKS_PER_SECOND);

  if (format === "hh:mm:ss") {
    const hours = Math.floor(totalSeconds / 3600);
    const minutes = Math.floor((totalSeconds % 3600) / 60);
    const seconds = totalSeconds % 60;

    return `${hours}:${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
  }

  // Default "mm:ss" format
  const minutes = Math.floor(totalSeconds / 60);
  const seconds = totalSeconds % 60;
  return `${minutes}:${seconds.toString().padStart(2, "0")}`;
}

/**
 * Convert seconds to formatted duration string
 * @param seconds Duration in seconds
 * @param format Format type: "mm:ss" (default) or "hh:mm:ss"
 * @returns Formatted duration string
 */
export function formatSecondsDuration(seconds: number, format: "mm:ss" | "hh:mm:ss" = "mm:ss"): string {
  if (format === "hh:mm:ss") {
    const hours = Math.floor(seconds / 3600);
    const minutes = Math.floor((seconds % 3600) / 60);
    const secs = seconds % 60;

    return `${hours}:${minutes.toString().padStart(2, "0")}:${secs.toString().padStart(2, "0")}`;
  }

  // Default "mm:ss" format
  const minutes = Math.floor(seconds / 60);
  const secs = seconds % 60;
  return `${minutes}:${secs.toString().padStart(2, "0")}`;
}
