import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../../tslib.es6-848120af.js';
import { compareTwoObj } from '../../object/compare.mjs';
import { getJsonFromLog, saveJsonToLog } from '../../fs/fs-util.mjs';
import { getAllDevopsTemplateInstances } from '../template.mjs';
import { DEVOPS_MP_CI_CONFIG } from './config.mjs';
import '../../object/equal.mjs';
import '@babel/runtime/helpers/toConsumableArray';
import '../../fs/fs.mjs';
import '../../nodejs/crypto.mjs';
import '../../nodejs/fs.mjs';
import '../../nodejs/os.mjs';
import '../../nodejs/path.mjs';
import '../../time/time.mjs';
import 'axios';
import '../token.mjs';

var CI_SEPARATOR = DEVOPS_MP_CI_CONFIG.CI_SEPARATOR,
  VALID_ENVS = DEVOPS_MP_CI_CONFIG.VALID_ENVS;
var remoteInstancesMap = {};
function genOneFileFromRainbowGroup(list) {
  var obj = list.reduce(function (acc, item) {
    acc[item.key] = {
      value: item.value,
      value_type: item.value_type
    };
    return acc;
  }, {});
  return obj;
}
function parseRobotMap() {
  var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var branches = Object.keys(obj);
  var res = {};
  for (var _i = 0, _branches = branches; _i < _branches.length; _i++) {
    var branch = _branches[_i];
    var envs = Object.keys(obj[branch]);
    for (var _i2 = 0, _envs = envs; _i2 < _envs.length; _i2++) {
      var env = _envs[_i2];
      if (!VALID_ENVS.includes(env)) {
        continue;
      }
      var robotNumber = obj[branch][env];
      res[robotNumber] = {
        branch: branch,
        env: env
      };
    }
  }
  console.log('[parseRobotMap] res: ', res);
  return res;
}
/**
 * 获取流水线名称
 * @param {object} config 配置信息
 * @ignore
 *
 * @example
 * getPipelineName({
 *   robot: '26',
 *   env: 'release',
 *   branch: 'feature/team-pvp-kpl',
 *   ciKey: 'convert_wz',
 *   prefix: 'wxci',
 * })
 * // wxci__convert_wz__26__release__feature/team-pvp-kpl
 */
function getPipelineName(_ref) {
  var robot = _ref.robot,
    env = _ref.env,
    branch = _ref.branch,
    ciKey = _ref.ciKey,
    prefix = _ref.prefix;
  return [prefix, ciKey, robot, env, branch].join(CI_SEPARATOR);
}
function parseInstanceName(name) {
  var list = name.split(CI_SEPARATOR);
  var prefix = list[0],
    projectShortName = list[1],
    robotNumber = list[2],
    env = list[3],
    branch = list[4];
  return {
    prefix: prefix,
    projectShortName: projectShortName,
    robotNumber: robotNumber,
    env: env,
    branch: branch
  };
}
function getPipelineParam(_ref2) {
  var branch = _ref2.branch,
    env = _ref2.env,
    rainbowConfigKey = _ref2.rainbowConfigKey,
    repo = _ref2.repo,
    isWxCI = _ref2.isWxCI,
    devopsParams = _ref2.devopsParams;
  var _a;
  var res = [{
    id: 'rainbowConfigKey',
    required: true,
    type: 'STRING',
    defaultValue: rainbowConfigKey,
    desc: '七彩石配置的key',
    readOnly: false
  }, {
    id: 'repo',
    required: true,
    type: 'STRING',
    defaultValue: repo,
    desc: '仓库',
    readOnly: false
  }, {
    id: 'branch',
    required: true,
    type: 'STRING',
    defaultValue: branch,
    desc: '分支',
    readOnly: false
  }, {
    id: 'env',
    required: true,
    type: 'STRING',
    defaultValue: env,
    desc: '环境',
    readOnly: false
  }, {
    id: 'forceNpm',
    required: true,
    type: 'STRING',
    // 从 devopsParams 中获取
    defaultValue: (_a = devopsParams === null || devopsParams === void 0 ? void 0 : devopsParams.forceNpm) !== null && _a !== void 0 ? _a : '1',
    desc: '是否强制使用 NPM，默认是',
    readOnly: false
  }];
  if (isWxCI) {
    res.push.apply(res, [{
      id: 'needSourceMap',
      required: true,
      type: 'STRING',
      defaultValue: '0',
      desc: '是否需要sourceMap',
      readOnly: false
    }, {
      id: 'tamProjectId',
      required: true,
      type: 'STRING',
      defaultValue: '',
      desc: 'Tam上报ID',
      readOnly: false
    }, {
      id: 'useDevopsWXCIPlugin',
      required: true,
      type: 'STRING',
      defaultValue: (devopsParams === null || devopsParams === void 0 ? void 0 : devopsParams.useDevopsWXCIPlugin) || '0',
      desc: '是否使用蓝盾平台小程序CI插件',
      readOnly: false
    }]);
  } else {
    res.push.apply(res, [{
      id: 'useNode16',
      required: true,
      type: 'STRING',
      defaultValue: (devopsParams === null || devopsParams === void 0 ? void 0 : devopsParams.useDevopsWXCIPlugin) == '1' ? '1' : '0',
      desc: '是否使用 node 16 版本',
      readOnly: false
    }]);
  }
  return res;
}
function compareFromLogFile(key, obj) {
  var originFile = getJsonFromLog(key);
  var compareRes = compareTwoObj(originFile, obj);
  return compareRes;
}
var getTemplateInstanceFileName = function getTemplateInstanceFileName(isWxCI) {
  return "devops-template-instances-".concat(isWxCI ? 'wx' : 'qq', ".json");
};
var getRainbowMpCIFileName = function getRainbowMpCIFileName(isWxCI) {
  return "devops-mp-ci-rainbow-".concat(isWxCI ? 'wx' : 'qq', ".json");
};
function isPipelineUpdated(_ref3) {
  var pipelineName = _ref3.pipelineName,
    isWxCI = _ref3.isWxCI,
    forceUpdate = _ref3.forceUpdate,
    devopsConfig = _ref3.devopsConfig,
    templateIdMap = _ref3.templateIdMap;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var _parseInstanceName, prefix, projectShortName, robotNumber, name, templateId, remoteInstances, sameRobotPipeline;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _parseInstanceName = parseInstanceName(pipelineName), prefix = _parseInstanceName.prefix, projectShortName = _parseInstanceName.projectShortName, robotNumber = _parseInstanceName.robotNumber;
          name = "".concat([prefix, projectShortName, robotNumber].join(CI_SEPARATOR)).concat(CI_SEPARATOR);
          templateId = isWxCI ? templateIdMap.WX_MP_CI : templateIdMap.QQ_MP_CI;
          remoteInstances = remoteInstancesMap[templateId] || [];
          if (remoteInstances.length) {
            _context.next = 2;
            break;
          }
          _context.next = 1;
          return getAllDevopsTemplateInstances(Object.assign(Object.assign({}, devopsConfig), {
            templateId: templateId
          }));
        case 1:
          remoteInstancesMap[templateId] = _context.sent;
          remoteInstances = remoteInstancesMap[templateId];
          saveJsonToLog(remoteInstances, getTemplateInstanceFileName(isWxCI));
          _context.next = 3;
          break;
        case 2:
          console.log('cache');
        case 3:
          sameRobotPipeline = remoteInstances.find(function (item) {
            return item.pipelineName.startsWith(name);
          });
          if (!sameRobotPipeline) {
            _context.next = 5;
            break;
          }
          if (!(!forceUpdate && sameRobotPipeline.pipelineName === pipelineName)) {
            _context.next = 4;
            break;
          }
          return _context.abrupt("return", {
            type: 'NONE'
          });
        case 4:
          return _context.abrupt("return", Object.assign({
            type: 'UPDATE'
          }, sameRobotPipeline));
        case 5:
          return _context.abrupt("return", {
            type: 'CREATE'
          });
        case 6:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
function getUnusedPipelineList(_ref4) {
  var usefulList = _ref4.usefulList,
    isWxCI = _ref4.isWxCI,
    templateIdMap = _ref4.templateIdMap;
  var templateId = isWxCI ? templateIdMap.WX_MP_CI : templateIdMap.QQ_MP_CI;
  var remoteInstances = remoteInstancesMap[templateId] || [];
  return remoteInstances.filter(function (item) {
    return usefulList.indexOf(item.pipelineId) === -1;
  });
}

export { compareFromLogFile, genOneFileFromRainbowGroup, getPipelineName, getPipelineParam, getRainbowMpCIFileName, getTemplateInstanceFileName, getUnusedPipelineList, isPipelineUpdated, parseInstanceName, parseRobotMap };
