// WebVTT (.vtt) export. Header + cues with `HH:MM:SS.mmm` timecodes.

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

const HEADER = 'WEBVTT\n\n';

/**
 * Render segments as WebVTT. Always starts with the `WEBVTT` header followed by
 * a blank line. Cues use `.` as the millisecond separator. Empty input yields
 * just the header.
 */
export function toVtt(segments: Segment[]): string {
  if (segments.length === 0) return HEADER;
  const cues = segments.map((seg) => {
    const start = formatTimecode(seg.start, '.');
    const end = formatTimecode(seg.end, '.');
    const text = seg.text.trim();
    return `${start} --> ${end}\n${text}`;
  });
  return HEADER + cues.join('\n\n') + '\n';
}
