'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../tslib.es6-0d92ef81.js');
var fs_fs = require('../fs/fs.js');
var nodejs_fs = require('../nodejs/fs.js');
var nodejs_path = require('../nodejs/path.js');
var rainbow_rainbowUser = require('../rainbow/rainbow-user.js');
require('../nodejs/crypto.js');
require('../nodejs/os.js');
require('axios');
require('../rainbow/helper/helper.js');
require('../rainbow/sdk.js');
require('../json/json-parse.js');
require('../list/flat.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);

// 从七彩石获取CI配置
function getCIConfig(rainbowConfigKey, rainbowSecretInfo) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var res, str;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          res = {};
          _context.next = 1;
          return rainbow_rainbowUser.fetchRainbowConfig(rainbowConfigKey, rainbowSecretInfo);
        case 1:
          str = _context.sent;
          try {
            res = JSON.parse(str);
          } catch (err) {}
          return _context.abrupt("return", res);
        case 2:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
// 获取robot
function getRobot(_ref) {
  var config = _ref.config,
    branch = _ref.branch,
    env = _ref.env;
  var _a, _b;
  var configRobot = (_b = (_a = config === null || config === void 0 ? void 0 : config.robotMap) === null || _a === void 0 ? void 0 : _a[branch]) === null || _b === void 0 ? void 0 : _b[env];
  if (configRobot) {
    return configRobot;
  }
  // if (env === 'release') return 2;
  return 1;
}
// 写入环境变量
function writeEnv(robot) {
  var localEnv = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  nodejs_fs.getFs().writeFileSync('.env.local', "\n".concat(localEnv, "\nVUE_APP_AUTHOR=CI Robot ").concat(robot, "\n"), {
    flag: 'a',
    encoding: 'utf-8'
  });
}
// 写入密钥
function writePrivateKey(root, privateKey) {
  var nRoot = root || process.cwd();
  fs_fs.writeFileSync(nodejs_path.getPath().resolve(nRoot, 'private.key'), privateKey);
}
function writeEnvAndPrivateKey(_ref2) {
  var branch = _ref2.branch,
    env = _ref2.env,
    root = _ref2.root,
    rainbowConfigKey = _ref2.rainbowConfigKey,
    rainbowAppId = _ref2.rainbowAppId,
    rainbowEnvName = _ref2.rainbowEnvName,
    rainbowGroupName = _ref2.rainbowGroupName;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() {
    var rainbowSecretInfo, config, robot, rainbowPrivateKey, _config$localEnv, localEnv, _config$wxPrivateKey, wxPrivateKey, privateKey;
    return _regeneratorRuntime__default["default"].wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          rainbowSecretInfo = {
            appId: rainbowAppId,
            envName: rainbowEnvName,
            groupName: rainbowGroupName
          };
          _context2.next = 1;
          return getCIConfig(rainbowConfigKey, rainbowSecretInfo);
        case 1:
          config = _context2.sent;
          console.log('[CI] config: ', config);
          robot = getRobot({
            config: config,
            branch: branch,
            env: env
          });
          rainbowPrivateKey = config.rainbowPrivateKey, _config$localEnv = config.localEnv, localEnv = _config$localEnv === void 0 ? '' : _config$localEnv, _config$wxPrivateKey = config.wxPrivateKey, wxPrivateKey = _config$wxPrivateKey === void 0 ? '' : _config$wxPrivateKey;
          writeEnv(robot, localEnv);
          privateKey = wxPrivateKey;
          if (privateKey) {
            _context2.next = 3;
            break;
          }
          console.log('[CI] No Default PrivateKey');
          _context2.next = 2;
          return rainbow_rainbowUser.fetchRainbowConfig(rainbowPrivateKey, rainbowSecretInfo);
        case 2:
          privateKey = _context2.sent;
        case 3:
          console.log('[CI] privateKey: ', privateKey);
          writePrivateKey(root, privateKey);
        case 4:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
}
function writeEnvAndPrivateKeyByOptions(options) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3() {
    var branch, env, root, rainbowConfigKey, rainbowAppId, rainbowEnvName, rainbowGroupName;
    return _regeneratorRuntime__default["default"].wrap(function (_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          console.log('[options] ', options);
          branch = options.branch, env = options.env, root = options.root, rainbowConfigKey = options.configKey, rainbowAppId = options.appid, rainbowEnvName = options.envName, rainbowGroupName = options.groupName;
          if (!(!branch || !env || !root || !rainbowConfigKey || !rainbowAppId || !rainbowEnvName || !rainbowGroupName)) {
            _context3.next = 1;
            break;
          }
          console.error('缺少必要参数，请检查！');
          return _context3.abrupt("return");
        case 1:
          writeEnvAndPrivateKey({
            branch: branch,
            env: env,
            root: root,
            rainbowConfigKey: rainbowConfigKey,
            rainbowAppId: rainbowAppId,
            rainbowEnvName: rainbowEnvName,
            rainbowGroupName: rainbowGroupName
          });
        case 2:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
}

exports.getCIConfig = getCIConfig;
exports.getRobot = getRobot;
exports.writeEnvAndPrivateKey = writeEnvAndPrivateKey;
exports.writeEnvAndPrivateKeyByOptions = writeEnvAndPrivateKeyByOptions;
