import axios from 'axios';
import { getCrypto } from '../../nodejs/crypto.mjs';

var DEFAULT_RUM_INFO = {
  ENDPOINT: 'rum.tencentcloudapi.com',
  REGION: 'ap-guangzhou',
  VERSION: '2021-06-22',
  SERVICE: 'rum'
};
function sha256(message) {
  var secret = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  var encoding = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
  var crypto = getCrypto();
  var hmac = crypto.createHmac('sha256', secret);
  return hmac.update(message).digest(encoding);
}
function getHash(message) {
  var encoding = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'hex';
  var crypto = getCrypto();
  var hash = crypto.createHash('sha256');
  return hash.update(message).digest(encoding);
}
function getDate(timestamp) {
  var date = new Date(timestamp * 1000);
  var year = date.getUTCFullYear();
  var month = "0".concat(date.getUTCMonth() + 1).slice(-2);
  var day = "0".concat(date.getUTCDate()).slice(-2);
  return "".concat(year, "-").concat(month, "-").concat(day);
}
function getAuthorization(_ref) {
  var secretId = _ref.secretId,
    secretKey = _ref.secretKey,
    payload = _ref.payload,
    action = _ref.action,
    endpoint = _ref.endpoint,
    timestamp = _ref.timestamp,
    service = _ref.service;
  // 时间处理, 获取世界时间日期
  var date = getDate(timestamp);
  var hashedRequestPayload = getHash(payload);
  var httpRequestMethod = 'POST';
  var canonicalUri = '/';
  var canonicalQueryString = '';
  var canonicalHeaders = 'content-type:application/json; charset=utf-8\n' + "host:".concat(endpoint, "\n") + "x-tc-action:".concat(action.toLowerCase(), "\n");
  var signedHeaders = 'content-type;host;x-tc-action';
  var canonicalRequest = "".concat(httpRequestMethod, "\n").concat(canonicalUri, "\n").concat(canonicalQueryString, "\n").concat(canonicalHeaders, "\n").concat(signedHeaders, "\n").concat(hashedRequestPayload);
  // ************* 步骤 2：拼接待签名字符串 *************
  var algorithm = 'TC3-HMAC-SHA256';
  var hashedCanonicalRequest = getHash(canonicalRequest);
  var credentialScope = "".concat(date, "/").concat(service, "/") + 'tc3_request';
  var stringToSign = "".concat(algorithm, "\n").concat(timestamp, "\n").concat(credentialScope, "\n").concat(hashedCanonicalRequest);
  // ************* 步骤 3：计算签名 *************
  var kDate = sha256(date, "TC3".concat(secretKey));
  var kService = sha256(service, kDate);
  var kSigning = sha256('tc3_request', kService);
  var signature = sha256(stringToSign, kSigning, 'hex');
  // ************* 步骤 4：拼接 Authorization *************
  var authorization = "".concat(algorithm, " ") + "Credential=".concat(secretId, "/").concat(credentialScope, ", ") + "SignedHeaders=".concat(signedHeaders, ", ") + "Signature=".concat(signature);
  return authorization;
}
function fetchCloudData(_ref2) {
  var secretId = _ref2.secretId,
    secretKey = _ref2.secretKey,
    payload = _ref2.payload,
    action = _ref2.action,
    _ref2$endpoint = _ref2.endpoint,
    endpoint = _ref2$endpoint === void 0 ? DEFAULT_RUM_INFO.ENDPOINT : _ref2$endpoint,
    _ref2$region = _ref2.region,
    region = _ref2$region === void 0 ? DEFAULT_RUM_INFO.REGION : _ref2$region,
    _ref2$version = _ref2.version,
    version = _ref2$version === void 0 ? DEFAULT_RUM_INFO.VERSION : _ref2$version,
    _ref2$service = _ref2.service,
    service = _ref2$service === void 0 ? DEFAULT_RUM_INFO.SERVICE : _ref2$service;
  var timestamp = Math.round(Date.now() / 1000);
  var authorization = getAuthorization({
    secretId: secretId,
    secretKey: secretKey,
    endpoint: endpoint,
    action: action,
    payload: payload,
    timestamp: timestamp,
    service: service
  });
  return axios({
    url: "https://".concat(endpoint),
    method: 'POST',
    data: payload,
    headers: {
      Authorization: authorization,
      'Content-Type': 'application/json; charset=utf-8',
      Host: endpoint,
      'X-TC-Action': action,
      'X-TC-Timestamp': timestamp.toString(),
      'X-TC-Version': version,
      'X-TC-Region': region
    }
  });
}

export { fetchCloudData };
