'use strict';

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

var mpCi_manageConfig_helper = require('./helper.js');
var object_sort = require('../../object/sort.js');
require('@babel/runtime/helpers/toConsumableArray');

function changeRobot(_ref) {
  var info = _ref.info,
    robot = _ref.robot,
    env = _ref.env,
    testRobot = _ref.testRobot,
    releaseRobot = _ref.releaseRobot;
  if (robot && env) {
    info[env] = robot;
  } else {
    info.test = testRobot;
    info.release = releaseRobot;
  }
}
function updateMpCIRainbowConfig(_ref2) {
  var rainbowConfig = _ref2.rainbowConfig,
    branch = _ref2.branch,
    originBranch = _ref2.originBranch,
    testRobot = _ref2.testRobot,
    releaseRobot = _ref2.releaseRobot,
    robot = _ref2.robot,
    env = _ref2.env,
    useMpQQ = _ref2.useMpQQ;
  var _a, _b;
  function setWxRobot() {
    if (!rainbowConfig.robotMap[branch]) {
      rainbowConfig.robotMap[branch] = {};
    }
    changeRobot({
      info: rainbowConfig.robotMap[branch],
      testRobot: testRobot,
      releaseRobot: releaseRobot,
      robot: robot,
      env: env
    });
  }
  function setQQRobot() {
    var _a;
    if (!rainbowConfig.qqRobotMap) {
      rainbowConfig.qqRobotMap = {};
    }
    if (useMpQQ) {
      if (!rainbowConfig.qqRobotMap[branch]) {
        rainbowConfig.qqRobotMap[branch] = {};
      }
      changeRobot({
        info: rainbowConfig.qqRobotMap[branch],
        testRobot: testRobot,
        releaseRobot: releaseRobot,
        robot: robot,
        env: env
      });
    } else {
      if ((_a = rainbowConfig.qqRobotMap) === null || _a === void 0 ? void 0 : _a[branch]) {
        delete rainbowConfig.qqRobotMap[branch].test;
        delete rainbowConfig.qqRobotMap[branch].release;
      }
    }
  }
  // const originUseMpQQ = !!rainbowConfig.qqRobotMap?.[branch];
  if (originBranch) {
    if ((_a = rainbowConfig.robotMap) === null || _a === void 0 ? void 0 : _a[originBranch]) {
      delete rainbowConfig.robotMap[originBranch].test;
      delete rainbowConfig.robotMap[originBranch].release;
    }
    if ((_b = rainbowConfig.qqRobotMap) === null || _b === void 0 ? void 0 : _b[originBranch]) {
      delete rainbowConfig.qqRobotMap[originBranch].test;
      delete rainbowConfig.qqRobotMap[originBranch].release;
      delete rainbowConfig.qqRobotMap[originBranch].tencentDocsLine;
    }
  }
  setWxRobot();
  setQQRobot();
  // 新增
  // if (!originBranch) {
  // // 该分支已经有配置了
  //   if (rainbowConfig.robotMap[branch]) {
  //     return {
  //       error: errorMap.BRANCH_EXIST,
  //     };
  //   }
  //   setWxRobot();
  //   setQQRobot();
  // } else {
  //   // 修改
  //   // 分支相同，说明是仅修改qq配置
  //   if (originBranch === branch) {
  //     if (originUseMpQQ === useMpQQ) {
  //       return {
  //         error: errorMap.SAME_CONFIG,
  //       };
  //     }
  //     setQQRobot();
  //   } else {
  //     if (rainbowConfig.robotMap[branch]) {
  //       return {
  //         error: errorMap.BRANCH_EXIST,
  //       };
  //     }
  //     setWxRobot();
  //     setQQRobot();
  //     delete rainbowConfig.robotMap[originBranch];
  //     delete rainbowConfig.qqRobotMap[originBranch];
  //   }
  // }
  rainbowConfig.robotMap = mpCi_manageConfig_helper.sortRobotMapByRobot(rainbowConfig.robotMap);
  var rainbowKey = rainbowConfig.rainbowKey;
  delete rainbowConfig.rainbowKey;
  var newRainbowConfig = object_sort.sortObjectByKey(rainbowConfig);
  mpCi_manageConfig_helper.updateQQTencentSheetLine(newRainbowConfig);
  return {
    newRainbowConfig: newRainbowConfig,
    rainbowKey: rainbowKey
  };
}

exports.updateMpCIRainbowConfig = updateMpCIRainbowConfig;
