import axios from "axios";
import base from "./config";

export default class TextRender {
  constructor(param = {}) {
    this.url = base.url;
    this.resultList = [];
    this.config = param.config;
    this.token = param.token;
  }

  async ttsRender(list, role, config) {
    this.resultList = [];
    var model = "merge";
    if (config?.task) {
      model = config.task.model;
    }
    if (model == "single") {
      for (var i = 0; i < list.length; i++) {
        try {
          var singleList = [];
          singleList.push(list[i]);
          var param = {
            list: singleList,
            roleId: role,
            config: config || this.config,
            token: this.token,
          };
          var options = {
            url: this.url + "/tts/short/text",
            method: "POST",
            data: param,
          };
          var result = await axios(options);
          if (result.status == 200) {
            this.resultList=[...this.resultList,...result.data.data.list];
          } else {
            console.log("tts服务错误：", result);
          }
        } catch (e) {
            console.log(e);
            continue;
        }
      }
	  console.log(">>>>>>>>>>>",this.resultList)
      return this.resultList;
    } else {
      try {
        var param = {
          list: list,
          roleId: role,
          config: config || this.config,
          token: this.token,
        };
        var options = {
          url: this.url + "/tts/short/text",
          method: "POST",
          data: param,
        };
        var result = await axios(options);
        if (result.status == 200) {
          this.resultList = result.data.data.list;
          return this.resultList;
        } else {
          console.log("tts服务错误：", result);
        }
      } catch (e) {
        console.log(e);
      }
    }

  }
}
