// Export-format registry and dispatcher. Re-exports each formatter and maps a
// format id to its serializer plus file metadata (extension / MIME / label).

import type { Segment } from '../types';
import { toSrt } from './srt';
import { toVtt } from './vtt';
import { toTxt } from './txt';
import { toMarkdown } from './md';
import { toJson } from './json';

export { toSrt } from './srt';
export { toVtt } from './vtt';
export { toTxt } from './txt';
export { toMarkdown } from './md';
export { toJson } from './json';
export type { TranscriptExport } from './json';

/** Supported export formats. */
export type ExportFormat = 'srt' | 'vtt' | 'txt' | 'md' | 'json';

/** File metadata for each format, for download / share UIs. */
export const EXPORT_META: Record<ExportFormat, { ext: string; mime: string; label: string }> = {
  srt: { ext: 'srt', mime: 'application/x-subrip', label: 'SubRip (.srt)' },
  vtt: { ext: 'vtt', mime: 'text/vtt', label: 'WebVTT (.vtt)' },
  txt: { ext: 'txt', mime: 'text/plain', label: 'Plain text (.txt)' },
  md: { ext: 'md', mime: 'text/markdown', label: 'Markdown (.md)' },
  json: { ext: 'json', mime: 'application/json', label: 'JSON (.json)' },
};

/**
 * Serialize segments using the requested format. `opts.title` is honored by the
 * Markdown and JSON formatters and ignored by the others.
 */
export function formatTranscript(
  segments: Segment[],
  format: ExportFormat,
  opts?: { title?: string },
): string {
  switch (format) {
    case 'srt':
      return toSrt(segments);
    case 'vtt':
      return toVtt(segments);
    case 'txt':
      return toTxt(segments);
    case 'md':
      return toMarkdown(segments, opts);
    case 'json':
      return toJson(segments, opts);
    default: {
      // Exhaustiveness guard: if `ExportFormat` grows, this fails to compile.
      const never: never = format;
      throw new Error(`Unknown export format: ${String(never)}`);
    }
  }
}
