import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../tslib.es6-848120af.js';
import { writeFileSync } from '../fs/fs.mjs';
import { getFs } from '../nodejs/fs.mjs';
import { getPath } from '../nodejs/path.mjs';
import { fetchRainbowConfig } from '../rainbow/rainbow-user.mjs';
import '../nodejs/crypto.mjs';
import '../nodejs/os.mjs';
import 'axios';
import '../list/flat.mjs';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/typeof';
import '../rainbow/helper/helper.mjs';
import '../rainbow/sdk.mjs';
import '../json/json-parse.mjs';

// 从七彩石获取CI配置
function getCIConfig(rainbowConfigKey, rainbowSecretInfo) {
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var res, str;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          res = {};
          _context.next = 1;
          return 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] : '';
  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();
  writeFileSync(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 __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    var rainbowSecretInfo, config, robot, rainbowPrivateKey, _config$localEnv, localEnv, _config$wxPrivateKey, wxPrivateKey, privateKey;
    return _regeneratorRuntime.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 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 __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
    var branch, env, root, rainbowConfigKey, rainbowAppId, rainbowEnvName, rainbowGroupName;
    return _regeneratorRuntime.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);
  }));
}

export { getCIConfig, getRobot, writeEnvAndPrivateKey, writeEnvAndPrivateKeyByOptions };
