import { Client } from '@gradio/client';

type CosyVoiceTTSOptions = {
  url: string;
};
type AudioOptions = {
  tts_text: string;
  mode_checkbox_group: string;
  sft_dropdown: string;
  prompt_text: string;
  prompt_wav_upload?: any;
  prompt_wav_record: any | null;
  instruct_text: string;
  seed: number;
  stream: boolean;
  speed: number;
};

export class CosyVoiceTTS {
  private client: Client;
  private url: string;
  isInit = false;
  constructor(opts?: CosyVoiceTTSOptions) {
    this.url = opts?.url || 'http://localhost:50000/';
  }
  async init() {
    const url = this.url;
    const client = await Client.connect(url);
    this.client = client;
    this.isInit = true;
    return true;
  }
  generateAudio = async (opts?: Partial<AudioOptions>) => {
    if (!this.isInit) {
      await this.init();
    }
    const data: AudioOptions = {
      tts_text: '梦里的一天结束了一个新的轮m,回梦里的一天结束了一个新的轮回梦里的一,,天结束了一个新的轮回,梦里的一天结束了一个新的轮回',
      mode_checkbox_group: '3s极速复刻',
      sft_dropdown: '',
      prompt_text: '在一无所知中，梦里的一天结束了一个新的轮回，便会开始。',
      // prompt_wav_upload: exampleAudio,
      prompt_wav_record: null,
      instruct_text: '',
      seed: 3,
      stream: false,
      speed: 1,
      ...opts,
    };
    const result = await this.client.predict('/generate_audio', data);
    return result;
  };
}
