import type { CTX, MsgProps } from './CTX';
import type { ChatResponse } from './HANDLERS';
import type { CustomOption, TopicInfo } from './UI';

export interface RequestCommonConfig {
  // 超时时间，各方法TAppOptionRequestReturn中的timeout权重高，都不设置时默认5000ms 
  timeout?: number;
  // 请求方法
  method?: 'POST' | 'GET'
  // 请求头
  headers?: { [k: string]: any }
}


// 交易信息配置
export interface TAppOptionRequestReturn extends RequestCommonConfig {
  /** 作为request请求的地址 */
  url?: string;
  /** 作为Response成功返回的数据解析 */
  ResponseFormat?: (response?: any) => any;
  CatchFormat?: (response?: any) => string;
  /** 除[url, ResponseFormat, CatchFormat,]外 整个TAppOptionRequestReturn参数将逐个全部放置在request请求中 */
  [i: string]: any;
}

// RequestConfig中 各项接口数据解析

/** TAppOptionRequestReturn_Fetch 流式问答交易数据自定义解析 */
export interface TAppOptionRequestReturn_Fetch extends TAppOptionRequestReturn {
  ResponseFormat?: (response?: any) => string
}

/** TAppOptionRequestReturn_Json 非流式问答交易数据自定义解析 */
export interface TAppOptionRequestReturn_Json extends TAppOptionRequestReturn {
  ResponseFormat?: (response?: any) => { [key: string]: any; data: ChatResponse }
}

/** TAppOptionRequestReturn_History 历史数据交易数据自定义解析 */
export interface PreDataHistory extends CustomOption {
  data: {
    topicInfo: TopicInfo | null;
  }
}
export interface TAppOptionRequestReturn_History extends TAppOptionRequestReturn {
  ResponseFormat?: (response?: any) => Array<MsgProps>
}

/** TAppOptionRequestReturn_Feedback 语音转文字交易数据自定义解析 */
export interface PreSpeechToText extends CustomOption {
  data: {
    voice: Blob[];
  }
}

export type TMediaType = 'mp3' | 'mp4' | 'mpeg' | 'mpga' | 'm4a' | 'wav' | 'webm'
export interface TAppOptionRequestReturn_SpeechToText extends TAppOptionRequestReturn {
  ResponseFormat?: (response?: any) => { status: 200 | number, message: string, code?: any, data?: { text: string } }
  /** mimeType 接口支持的媒体流转换格式
   * =TMediaType时默认为mp3,依据格式转换文件,合并body中的参数为FormData对象；
   * =false时表示用户已完成自定义参数，系统内不做任何处理直接发送请求（此时自定义body参数应是FormData对象）
   */
  mimeType: false | TMediaType
  body?: FormData | { [k: string]: any }
}

/** TAppOptionRequestReturn_Feedback 消息反馈交易数据自定义解析 */
export interface PreFeedBack extends CustomOption {
  data: {
    msg: MsgProps
    likes?: boolean
    dislikes?: boolean
  }
}
export interface TAppOptionRequestReturn_Feedback extends TAppOptionRequestReturn {
  ResponseFormat?: (response?: any) => boolean
}

/** TAppOptionRequestReturn_Voice 语音播报交易数据自定义解析 */
export interface PreVoice extends CustomOption {
  data: {
    text: string
  }
}
export interface TAppOptionRequestReturn_Voice extends TAppOptionRequestReturn {
  ResponseFormat?: (response?: any) => Blob
}

// 全局配置，会传递给Chat组件及内部
export interface RequestConfig extends RequestCommonConfig {
  /** 对话交易的参数配置 */
  QAChatFetch?: ({ data, ctx }: { data: { text: string, controller: any }, ctx: CTX }) => TAppOptionRequestReturn_Fetch;
  QAChatJson?: ({ data, ctx }: { data: string, ctx: CTX }) => TAppOptionRequestReturn_Json;

  /** 某一话题的历史记录交易配置 */
  history?: (options: PreDataHistory) => TAppOptionRequestReturn_History

  /** 语音转文字交易配置 */
  speechToText?: (options: PreSpeechToText) => TAppOptionRequestReturn_SpeechToText;

  /** 消息反馈交易配置 */
  feedback?: (options: PreFeedBack) => TAppOptionRequestReturn_Feedback;

  /** 语音播报配置 */
  voice?: (options: PreVoice) => TAppOptionRequestReturn_Voice;

  /** 会话管理 */
  // topic?: {
  //   /** 会话列表 */
  //   list?: (options: CustomOption) => TAppOptionRequestReturn;
  //   /** 会话删除 */
  //   delete?: (options: CustomOption) => TAppOptionRequestReturn;
  //   /** 会话创建 */
  //   create?: (options: CustomOption) => TAppOptionRequestReturn;
  //   /** 会话更新 ？页面中什么操作会自动更新会话 */
  //   // update?: (options: CustomOption) => TAppOptionRequestReturn;
  // }
}
