// =============================================================================
// Converter Factory / Registry
// =============================================================================

import type { IFileConverter, SupportedFormat } from './types';

const adapters: IFileConverter[] = [];

/**
 * Register a converter adapter
 */
export function registerAdapter(adapter: IFileConverter): void {
  // Avoid duplicates
  if (!adapters.some(a => a.name === adapter.name)) {
    adapters.push(adapter);
  }
}

/**
 * Get a converter that can handle the input→output conversion
 */
export function getAdapter(
  inputFormat: SupportedFormat,
  outputFormat: SupportedFormat
): IFileConverter {
  const adapter = adapters.find(a => a.supports(inputFormat, outputFormat));
  if (!adapter) {
    throw new Error(
      `No converter found for ${inputFormat} → ${outputFormat}. ` +
      `Registered adapters: ${adapters.map(a => a.name).join(', ') || 'none'}`
    );
  }
  return adapter;
}

/**
 * Check if a conversion is supported
 */
export function isConversionSupported(
  inputFormat: SupportedFormat,
  outputFormat: SupportedFormat
): boolean {
  return adapters.some(a => a.supports(inputFormat, outputFormat));
}

/**
 * Get all registered adapters (for debugging)
 */
export function getRegisteredAdapters(): readonly IFileConverter[] {
  return adapters;
}