// Markdown (.md) export. Optional title heading, then `**[m:ss]** text` lines.

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

/**
 * Render segments as Markdown. Each segment becomes a line like
 * `**[m:ss]** text` using {@link formatClock}. When `opts.title` is provided a
 * `# {title}` heading is emitted first, separated from the body by a blank line.
 */
export function toMarkdown(segments: Segment[], opts?: { title?: string }): string {
  const lines = segments.map((seg) => `**[${formatClock(seg.start)}]** ${seg.text.trim()}`);
  const body = lines.join('\n');
  const title = opts?.title?.trim();
  if (title) {
    return body.length > 0 ? `# ${title}\n\n${body}` : `# ${title}`;
  }
  return body;
}
