import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../tslib.es6-848120af.js';
import axios from 'axios';

var token = '';
function getDevopsAccessToken(_ref) {
  var secretInfo = _ref.secretInfo,
    _ref$host = _ref.host,
    host = _ref$host === void 0 ? '' : _ref$host;
  var _a, _b;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var appCode, appSecret, resp;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (!token) {
            _context.next = 1;
            break;
          }
          return _context.abrupt("return", token);
        case 1:
          host = host.replace(/^(http[s]?:\/\/)devops\./, '$1');
          appCode = secretInfo.appCode, appSecret = secretInfo.appSecret;
          _context.next = 2;
          return axios({
            url: "".concat(host, "/auth_api/token/"),
            method: 'post',
            data: {
              app_code: appCode,
              app_secret: appSecret,
              env_name: 'prod',
              grant_type: 'client_credentials'
            }
          })["catch"](function (err) {
            console.log('[getDevopsAccessToken] err: ', err);
          });
        case 2:
          resp = _context.sent;
          token = ((_b = (_a = resp.data) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.access_token) || '';
          console.log('[getDevopsAccessToken] token', token);
          return _context.abrupt("return", token);
        case 3:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}

export { getDevopsAccessToken };
