import axios from 'axios';
import { BASE_URL } from './helper.mjs';
import { genRainbowHeaderSignature } from './rainbow-signature.mjs';
import '../../nodejs/crypto.mjs';

/**
 * 基本请求
 * @private
 * @param {object} config - 配置信息
 * @returns {Promise} 请求Promise
 * @example
 * ```ts
 * baseRequestRainbow({
 *   url: '/api/some-path',
 *   data: { foo: 1 },
 *   secretInfo: {
 *     appId: 'xxx',
 *     userId: 'yyy',
 *     secretKey: 'zzz',
 *     envName: 'Default',
 *     groupName: 'group',
 *   },
 * }).then(res => console.log(res));
 * ```
 */
function baseRequestRainbow(_ref) {
  var url = _ref.url,
    reqData = _ref.data,
    secretInfo = _ref.secretInfo;
  return new Promise(function (resolve, reject) {
    var baseUrl = BASE_URL;
    var appID = secretInfo.appID,
      appId = secretInfo.appId,
      userID = secretInfo.userID,
      userId = secretInfo.userId,
      secretKey = secretInfo.secretKey,
      envName = secretInfo.envName,
      groupName = secretInfo.groupName;
    var realSig = genRainbowHeaderSignature({
      appID: appID,
      appId: appId,
      userID: userID,
      userId: userId,
      secretKey: secretKey
    });
    axios({
      url: "".concat(baseUrl).concat(url),
      data: Object.assign({
        app_id: appId || appID,
        env_name: envName,
        group_name: groupName
      }, reqData),
      method: 'POST',
      headers: Object.assign({
        'Content-Type': 'application/json'
      }, realSig)
    }).then(function (res) {
      if (!res.data.ret_code) {
        resolve(res.data);
      } else {
        reject(res.data);
      }
    })["catch"](function (e) {
      reject(e);
    });
  });
}

export { baseRequestRainbow };
