"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.initCloudAPI=initCloudAPI,exports.get3rdCloudCodeSecret=get3rdCloudCodeSecret,exports.boundTransactRequest=boundTransactRequest;const tslib_1=require("tslib"),crypto_1=tslib_1.__importDefault(require("crypto")),request_1=tslib_1.__importDefault(require("request")),cloudAPI=tslib_1.__importStar(require("../../common/cloud-api/index")),request_2=require("../../utils/request"),sign_1=require("../../utils/sign"),jsonParse_1=require("../../utils/jsonParse"),urlConfig=tslib_1.__importStar(require("../../config/url.config"));function initCloudAPI(e){cloudAPI.setDefaultAppID(e),cloudAPI.setTransactType(cloudAPI.TransactType.IDE),cloudAPI.setRequest(transactRequest)}const SERVICE_CLOUD_URL={scf:"https://scf.tencentcloudapi.com",flexdb:"https://flexdb.tencentcloudapi.com",tcb:"https://tcb.tencentcloudapi.com"},DIRECT_CLOUD_API_SET=new Set(["UpdateFunctionCode","CreateFunction"]);async function transactRequest(e){var t;const r=e.project,o=await(0,sign_1.getSignature)(r.privateKey,r.appid),s=await r.getExtAppid();if(DIRECT_CLOUD_API_SET.has(e.identity.action||"")&&Boolean(SERVICE_CLOUD_URL[e.identity.service])){const{body:i}=await(0,request_2.request)({url:urlConfig.GET_CLOUD_API_SIGNATURE,method:"post",body:JSON.stringify({appid:r.appid,extAppid:s||void 0,signature:o,signReq:{action:e.identity.action,path:e.identity.path,service:e.identity.service,version:e.identity.version,region:e.identity.region,hashed_postdata:crypto_1.default.createHash("sha256").update(e.postdata).digest("hex")}}),headers:{"content-type":"application/json"}}),n=(0,jsonParse_1.jsonRespParse)(i,"cloudapi.getCloudAPISignedHeader");if(n.errCode)throw new Error(`getCloudAPISignedHeader failed, errCode: ${i.errCode}, errMsg: ${i.errMsg}`);const a=n.header;if(!a)throw new Error("empty header, recv cgi resp: "+i);const{resp:d}=await new Promise((t,r)=>{(0,request_1.default)({url:SERVICE_CLOUD_URL[e.identity.service],method:"post",body:e.postdata,headers:a.split("\n").map(e=>e.trim()).reduce((e,t)=>{const r=t.indexOf(":");return e[t.slice(0,r)]=t.slice(r+1),e},{})},(e,o)=>{e?r(e):t({resp:o})})});if(413===d.statusCode)throw new Error("Body too large");const c=d.body;if(!c)throw new Error("Empty body "+d);const p="string"==typeof c?JSON.parse(c):c;if(!p.Response||(null===(t=p.Response)||void 0===t?void 0:t.Error))throw new Error("TencentCloud API error: "+c);return c}const{resp:i,body:n}=await(0,request_2.request)({url:urlConfig.cloudAPIAgentURL,method:"post",body:JSON.stringify({appid:r.appid,extAppid:s||void 0,signature:o,agentReq:Object.assign({postdata:e.postdata,test:!1},e.identity)}),headers:Object.assign(Object.assign({},e.headers),{"content-type":"application/json"})});if(413===i.statusCode)throw new Error("Body too large");if(!n)throw new Error("Empty body "+i);const a="string"==typeof n?JSON.parse(n):n;if(a.errCode)throw new Error(`${a.errCode} ${a.errMsg}`);const d=a.data;if(!d||!d.base_resp||"0"!=d.base_resp.ret){if(80210===d.base_resp.ret)throw new Error("NO_CLOUD_MANAGE_PERMISSION_AUTHORIZED_TO_3RD_PLATFORM");throw new Error("Base resp abnormal, "+JSON.stringify(null==d?void 0:d.base_resp))}return d.content}async function get3rdCloudCodeSecret(e){const t=await e.attr();let r,o="",s={};try{r=await e.getFile(e.miniprogramRoot,"ext.json"),s=JSON.parse(r.toString("utf-8"))}catch(e){}if(s&&s.extEnable&&(o=s.extAppid||""),(null==t?void 0:t.platform)&&o)try{const t=await(0,sign_1.getSignature)(e.privateKey,e.appid),{body:r}=await(0,request_2.request)({url:urlConfig.get3rdCloudCodeSecret,method:"post",headers:{"content-type":"application/json"},body:JSON.stringify({appid:e.appid,extAppid:o,signature:t})}),s=(0,jsonParse_1.jsonRespParse)(r);if(0===s.errCode)return s.codeSecret;throw new Error("get 3rd cloud codesecret invalid resp "+JSON.stringify(r))}catch(e){throw new Error("get 3rd cloud codesecret error "+e)}}function boundTransactRequest(e){return t=>transactRequest(Object.assign(Object.assign({},t),{project:e}))}