// SubRip (.srt) export. Numbered cues with `HH:MM:SS,mmm` timecodes.

import type { Segment } from '../types';
import { formatTimecode } from '../format';

/**
 * Render segments as SubRip subtitles.
 *
 * Each cue is:
 *   <index>
 *   HH:MM:SS,mmm --> HH:MM:SS,mmm
 *   <text>
 * with a blank line between cues. Empty input yields an empty string.
 */
export function toSrt(segments: Segment[]): string {
  if (segments.length === 0) return '';
  const cues = segments.map((seg, i) => {
    const index = i + 1;
    const start = formatTimecode(seg.start, ',');
    const end = formatTimecode(seg.end, ',');
    const text = seg.text.trim();
    return `${index}\n${start} --> ${end}\n${text}`;
  });
  return cues.join('\n\n');
}
