/**
 * 平台系统配置
 */
interface SystemConfig {
  /**
   * 在平台申请的密钥信息
   */
  readonly API_SECRET: string
  /**
   * 在平台申请的APPID信息
   */
  readonly APPID: string
  /**
   *  在平台申请的API_KEY信息
   */
  readonly API_KEY: string
}

/**
 * 公共参数接口
 */
interface CommonParams {
  /**
   * 在平台申请的APPID信息
   */
  app_id: string
}

/**
 * 业务参数接口
 */
interface BusinessParams {
  /**
   * 音频编码
   * - 'raw': 未压缩的pcm
   * - 'lame': mp3 (当aue=lame时需传参sfl=1)
   * - 'speex-org-wb;7': 标准开源speex（for speex_wideband，即16k）数字代表指定压缩等级（默认等级为8）
   * - 'speex-org-nb;7': 标准开源speex（for speex_narrowband，即8k）数字代表指定压缩等级（默认等级为8）
   * - 'speex;7': 压缩格式，压缩等级1~10，默认为7（8k讯飞定制speex）
   * - 'speex-wb;7': 压缩格式，压缩等级1~10，默认为7（16k讯飞定制speex）
   */
  aue: 'raw' | 'lame' | 'speex-org-wb;7' | 'speex-org-nb;7' | 'speex;7' | 'speex-wb;7'

  /**
   * 需要配合aue=lame使用，开启流式返回
   * 取值：1 开启
   */
  sfl?: 1

  /**
   * 音频采样率
   * - 'audio/L16;rate=8000': 合成8K 的音频
   * - 'audio/L16;rate=16000': 合成16K 的音频
   * - 不传值：合成16K 的音频
   */
  auf?: 'audio/L16;rate=8000' | 'audio/L16;rate=16000'

  /**
   * 发音人，可选值：请到控制台添加试用或购买发音人，添加后即显示发音人参数值
   */
  vcn: string

  /**
   * 语速，可选值：[0-100]，默认为50
   */
  speed?: number

  /**
   * 音量，可选值：[0-100]，默认为50
   */
  volume?: number

  /**
   * 音高，可选值：[0-100]，默认为50
   */
  pitch?: number

  /**
   * 合成音频的背景音
   * - 0: 无背景音（默认值）
   * - 1: 有背景音
   */
  bgs?: 0 | 1

  /**
   * 文本编码格式
   * - 'GB2312': GB2312
   * - 'GBK': GBK
   * - 'BIG5': BIG5
   * - 'UNICODE': UNICODE(小语种必须使用UNICODE编码，合成的文本需使用utf16小端的编码方式)
   * - 'GB18030': GB18030
   * - 'UTF8': UTF8（小语种）
   */
  tte?: 'GB2312' | 'GBK' | 'BIG5' | 'UNICODE' | 'GB18030' | 'UTF8'

  /**
   * 设置英文发音方式：
   * - 0：自动判断处理，如果不确定将按照英文词语拼写处理（缺省）
   * - 1：所有英文按字母发音
   * - 2：自动判断处理，如果不确定将按照字母朗读
   */
  reg?: '0' | '1' | '2'

  /**
   * 合成音频数字发音方式
   * - 0：自动判断（默认值）
   * - 1：完全数值
   * - 2：完全字符串
   * - 3：字符串优先
   */
  rdn?: '0' | '1' | '2' | '3'
}

/**
 * 业务数据流参数接口
 */
interface DataParams {
  /**
   * 文本内容，需进行base64编码；
   * base64编码前最大长度需小于8000字节，约2000汉字
   */
  text: string

  /**
   * 数据状态，固定为2
   * 注：由于流式合成的文本只能一次性传输，不支持多次分段传输，此处status必须为2。
   */
  status: 2
}

/**
 * 请求参数
 */
interface RequestParams {
  common: CommonParams
  business: BusinessParams
  data: DataParams
}

/**
 * 内部使用的事件名称
 */
type PrivateCustomEventName =
  /**
   * 文本切割
   */
  | '_textSplitFinish'

  /**
   * tts请求执行
   */
  | '_ttsRequestFinish'
  /**
   * 响应转码执行
   */
  | '_responseTranscodeFinish'
  /**
   * 音频播放执行
   */
  | '_audioActuatorFinish'
  | '_audioActuatorBeforeFirstExecute'

  /**
   * 当前整个应用
   */
  | '_appError'
  | '_appFinish'

/**
 * 对外暴露的事件名称
 */
type PublicCustomEventName =
  | 'appError'
  | 'appFinish'
  | 'audioFirstStart'

/**
 * 发送消息给worker
 */
interface SendWorkerMessage<T = any> {
  readonly type: 'send'
  readonly data?: T
}

/**
 * 接收来自worker的消息
 */
interface ReplyWorkerMessage<T = any> {
  readonly type: 'success'
  readonly data: T
}

enum SystemStatus {
  /**
   * 空闲状态
   */
  OFFLINE = 'offline',
  /**
   * 执行中
   */
  EXECUTE = 'execute',
}

export type {
  BusinessParams,
  CommonParams,
  DataParams,
  PrivateCustomEventName,
  PublicCustomEventName,
  ReplyWorkerMessage,
  RequestParams,
  SendWorkerMessage,
  SystemConfig,
}

export { SystemStatus }
