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

const SUPPORTED_PLATFORMS = [
  OpenAi,
  ElevenLabs,
] as const;

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

export type PlatformConfig = (
  OpenAi.PlatformConfig
  | ElevenLabs.PlatformConfig
);

export type PlatformEventEmitter = (
  OpenAi.PlatformEventEmitter
  | ElevenLabs.PlatformEventEmitter
);

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

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

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

  return new platformModule.PlatformEventEmitterImpl(platformConfiguration) as T;
};
