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 speechRecognition(audioContent) {
        const clientConfig = {
            credential: {
              secretId: this.secretid,
              secretKey: this.secretKey,
            },
            region: "",
            profile: {
              httpProfile: {
                endpoint: "asr.tencentcloudapi.com",
              },
            },
          };
          
          // 实例化要请求产品的client对象,clientProfile是可选的
          const client = new AsrClient(clientConfig);
          const params = {
            "ProjectId": 0,
            "UsrAudioKey":"1",
            "SubServiceType": 2,
            "EngSerViceType": this.config.EngSerViceType||"16k_zh",
            "SourceType": 1,
            "VoiceFormat": this.config.VoiceFormat||"pcm",
            "Data": audioContent,
            "DataLen":audioContent.length
          };
          delete this.config.token;
          delete this.config.EngSerViceType;
          delete this.config.VoiceFormat;
          Object.assign(params,this.config);
          var result={};
          try{
            result= await client.SentenceRecognition(params);
            delete params.Data;
            console.log("【Tencent ASR】params>>>>>>:",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;