import config from "../../config";

const tencentcloud = require("tencentcloud-sdk-nodejs");

const AsrClient = tencentcloud.asr.v20190614.Client;




class asyncTencentASR {
  constructor(options) {
    //配置项
    this.secretid= options.token?.secretId||config.tencent.secretId;
        this.secretKey= options.token?.secretKey||config.tencent.secretKey;
        this.appId= options.token?.appId||config.tencent.appId;
        this.config=options.config.params;
  }

  //创建任务
  async createRecTask(url) {
    const clientConfig = {
      credential: {
        secretId: this.secretid,
        secretKey: this.secretKey,
      },
      region: "",
      profile: {
        httpProfile: {
          endpoint: "asr.tencentcloudapi.com",
        },
      },
    };

    // 实例化要请求产品的client对象,clientProfile是可选的
    const client = new AsrClient(clientConfig);
    var  params = {
      "EngineModelType":  this.config.EngineModelType||"8k_zh",
      "ChannelNum":  this.config.ChannelNum||1,
      "ResTextFormat":  this.config.ResTextFormat||3,
      "SourceType":  this.config.SourceType||0,
      "Url": url
    };
    delete this.config.token;
    delete this.config.EngineModelType;
    delete this.config.ChannelNum;
    delete this.config.ResTextFormat;
    delete this.config.SourceType;

    Object.assign(params,this.config);
    var result = {};
    try {
      result = await client.CreateRecTask(params);
      //  console.log(result);
      result.status = 200;
    } catch (e) {
      console.error("【TENCENT ASR】初始化接口内部错误", e);
      result.statusText = e;
      result.status = 500;
    }
    return result;
  }
  //查询结果
  async describeTaskStatus(taskId) {
    const clientConfig = {
      credential: {
        secretId: this.secretid,
        secretKey: this.secretKey,
      },
      region: "",
      profile: {
        httpProfile: {
          endpoint: "asr.tencentcloudapi.com",
        },
      },
    };

    // 实例化要请求产品的client对象,clientProfile是可选的
    const client = new AsrClient(clientConfig);
    var params = {
      "TaskId": taskId
    };
    var result = {};
    try {
      result = await client.DescribeTaskStatus(params);
      //  console.log(result);
      result.status = 200;
    } catch (e) {
      console.error("【TENCENT ASR】初始化接口内部错误", e);
      result.statusText = e;
      result.status = 500;
    }
    return result;
  }
}

export default asyncTencentASR;