// =============================================================================
// Shared Constants
// Centralized configuration for format mappings and codec settings
// =============================================================================

import { MagickFormat } from '@imagemagick/magick-wasm';
import type { AudioFormat, ImageFormat, SupportedFormat } from './types';
import { FORMAT_METADATA } from './types';

// -----------------------------------------------------------------------------
// MIME Type Mappings
// -----------------------------------------------------------------------------

/**
 * Convert format to MIME type (derived from FORMAT_METADATA for single source of truth)
 */
export const FORMAT_TO_MIME: Record<SupportedFormat, string> = Object.fromEntries(
  Object.entries(FORMAT_METADATA).map(([format, meta]) => [format, meta.mimeType])
) as Record<SupportedFormat, string>;

// -----------------------------------------------------------------------------
// Audio Codec Configuration (FFmpeg)
// -----------------------------------------------------------------------------

/**
 * Declarative FFmpeg codec arguments per audio format.
 * Each function returns the codec arguments for the given bitrate.
 */
export const AUDIO_CODEC_CONFIG: Record<AudioFormat, (bitrate: number) => string[]> = {
  mp3:  (br) => ['-c:a', 'libmp3lame', '-b:a', `${br}k`],
  aac:  (br) => ['-c:a', 'aac', '-b:a', `${br}k`],
  m4a:  (br) => ['-c:a', 'aac', '-b:a', `${br}k`],
  m4r:  (br) => ['-c:a', 'aac', '-b:a', `${br}k`],
  wma:  (br) => ['-c:a', 'wmav2', '-b:a', `${br}k`],
  opus: (br) => ['-c:a', 'libopus', '-b:a', `${br}k`],
  flac: ()   => ['-c:a', 'flac', '-compression_level', '5'],
  wav:  ()   => ['-c:a', 'pcm_s16le'],
  aiff: ()   => ['-c:a', 'pcm_s16be'],
  alac: ()   => ['-c:a', 'alac'],
  ogg:  ()   => ['-c:a', 'libvorbis', '-q:a', '4'],
  amr:  ()   => ['-c:a', 'libopencore_amrnb', '-ar', '8000', '-ac', '1'],
};

// -----------------------------------------------------------------------------
// Image Format Mapping (ImageMagick)
// -----------------------------------------------------------------------------

/**
 * Maps our ImageFormat to ImageMagick's MagickFormat enum
 */
export const IMAGE_FORMAT_MAP: Record<ImageFormat, MagickFormat> = {
  jpeg: MagickFormat.Jpeg,
  png:  MagickFormat.Png,
  webp: MagickFormat.WebP,
  gif:  MagickFormat.Gif,
  bmp:  MagickFormat.Bmp,
  tiff: MagickFormat.Tiff,
  heic: MagickFormat.Heic,
  ico:  MagickFormat.Ico,
  avif: MagickFormat.Avif,
  jxl:  MagickFormat.Jxl,
  svg:  MagickFormat.Svg,
  psd:  MagickFormat.Psd,
  raw:  MagickFormat.Raw,
  tga:  MagickFormat.Tga,
};

/**
 * Formats that support quality settings (lossy compression)
 */
export const LOSSY_IMAGE_FORMATS: ImageFormat[] = ['jpeg', 'webp', 'avif', 'jxl'];

/**
 * Get file extension for a format (handles special cases like ALAC)
 */
export function getFileExtension(format: AudioFormat): string {
  // ALAC uses m4a container
  return format === 'alac' ? 'm4a' : format;
}
