import * as OpenAiImpl from './platforms/openai';
import * as ElevenLabsImpl from './platforms/elevenlabs';

const SUPPORTED_PLATFORMS = {
  OpenAi: OpenAiImpl,
  ElevenLabs: ElevenLabsImpl,
} as const;

export * as OpenAi from './platforms/openai';
export * as ElevenLabs from './platforms/elevenlabs';

export type PlatformConfig = (
  OpenAiImpl.PlatformConfig
  | ElevenLabsImpl.PlatformConfig
)
export type PlatformEventEmitter = (
  OpenAiImpl.PlatformEventEmitter
  | ElevenLabsImpl.PlatformEventEmitter
);

export const createAiClient = (configParams: PlatformConfig): PlatformEventEmitter => {
  const {
    platform,
    platformConfiguration,
  } = configParams;

  const platformModules = Object.values(SUPPORTED_PLATFORMS);
  const platformModule = platformModules.find((p) => p.PLATFORM_ID === platform);

  if (!platformModule) {
    const supportedPlatforms = platformModules.map((p) => p.PLATFORM_ID).join(', ');
    throw new Error(`Unsupported platform: ${platform}. Supported platforms are: ${supportedPlatforms}`);
  }

  return new platformModule.PlatformEventEmitterImpl(platformConfiguration);
};
