import type { BuiltInExportPreset } from './preset.types';

function audioPreset(
  preset: Omit<BuiltInExportPreset, 'builtIn' | 'kind'>
): BuiltInExportPreset {
  return Object.freeze({
    ...preset,
    kind: 'audio',
    builtIn: true,
    requirements: Object.freeze({
      ...preset.requirements,
      ...(preset.requirements.muxers
        ? { muxers: Object.freeze([...preset.requirements.muxers]) }
        : {}),
      ...(preset.requirements.encoders
        ? { encoders: Object.freeze([...preset.requirements.encoders]) }
        : {}),
    }),
    ...(preset.audio ? { audio: Object.freeze({ ...preset.audio }) } : {}),
    settingsSummary: Object.freeze([...preset.settingsSummary]),
  });
}

const COMMON_POLICY = {
  metadataPolicy: 'copy',
  chapterPolicy: 'remove',
  subtitlePolicy: 'none',
} as const;

export const AUDIO_PRESETS: readonly BuiltInExportPreset[] = Object.freeze([
  audioPreset({
    id: 'audio-mp3',
    name: 'MP3',
    description: 'Widely compatible compressed audio.',
    container: 'mp3',
    fileExtension: 'mp3',
    audio: {
      codec: 'libmp3lame',
      bitrateKbps: 192,
      sampleRate: 44_100,
      channels: 2,
    },
    requirements: { muxers: ['mp3'], encoders: ['libmp3lame'] },
    settingsSummary: ['libmp3lame 192 kbit/s stereo at 44.1 kHz'],
    ...COMMON_POLICY,
  }),
  audioPreset({
    id: 'audio-m4a-aac',
    name: 'M4A · AAC',
    description: 'Browser-friendly compressed audio.',
    container: 'ipod',
    fileExtension: 'm4a',
    audio: { codec: 'aac', bitrateKbps: 160, sampleRate: 48_000, channels: 2 },
    requirements: { muxers: ['ipod'], encoders: ['aac'] },
    settingsSummary: ['AAC 160 kbit/s stereo at 48 kHz'],
    fastStart: true,
    ...COMMON_POLICY,
  }),
  audioPreset({
    id: 'audio-ogg-vorbis',
    name: 'Ogg Vorbis',
    description: 'Open compressed audio.',
    container: 'ogg',
    fileExtension: 'ogg',
    audio: {
      codec: 'libvorbis',
      bitrateKbps: 160,
      sampleRate: 48_000,
      channels: 2,
    },
    requirements: { muxers: ['ogg'], encoders: ['libvorbis'] },
    settingsSummary: ['libvorbis 160 kbit/s stereo at 48 kHz'],
    ...COMMON_POLICY,
  }),
  audioPreset({
    id: 'audio-opus',
    name: 'Opus',
    description: 'Efficient speech and music audio.',
    container: 'opus',
    fileExtension: 'opus',
    audio: {
      codec: 'libopus',
      bitrateKbps: 128,
      sampleRate: 48_000,
      channels: 2,
    },
    requirements: { muxers: ['opus'], encoders: ['libopus'] },
    settingsSummary: ['libopus 128 kbit/s stereo at 48 kHz'],
    ...COMMON_POLICY,
  }),
  audioPreset({
    id: 'audio-wav-pcm',
    name: 'WAV · PCM',
    description: 'Uncompressed 16-bit PCM audio.',
    container: 'wav',
    fileExtension: 'wav',
    audio: { codec: 'pcm_s16le', sampleRate: 48_000, channels: 2 },
    requirements: { muxers: ['wav'], encoders: ['pcm_s16le'] },
    settingsSummary: ['16-bit little-endian PCM stereo at 48 kHz'],
    ...COMMON_POLICY,
  }),
  audioPreset({
    id: 'audio-flac',
    name: 'FLAC',
    description: 'Lossless compressed audio.',
    container: 'flac',
    fileExtension: 'flac',
    audio: { codec: 'flac', sampleRate: 48_000, channels: 2 },
    requirements: { muxers: ['flac'], encoders: ['flac'] },
    settingsSummary: ['FLAC stereo at 48 kHz'],
    ...COMMON_POLICY,
  }),
]);
