import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../tslib.es6-848120af.js';
import { reportToRdPlatform } from '../rd-platform-report/rd-platform-report.mjs';
import { getInnerBundleBuildDesc } from './helper.mjs';
import { MpCI } from './mp-ci.mjs';
import { getRobot, getCIConfig } from './write-env-and-private-key.mjs';
import 'axios';
import '../bite/format-bite.mjs';
import '../git/git.mjs';
import '../node/node-command.mjs';
import '@babel/runtime/helpers/typeof';
import '../nodejs/child_process.mjs';
import '../validate/validate.mjs';
import '../nodejs/path.mjs';
import '../wecom-robot/message.mjs';
import './config.mjs';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '../canvas/img-text.mjs';
import '../third-party/image-size.mjs';
import '../canvas/get-canvas.mjs';
import '../third-party/canvas.mjs';
import '../node-img/img.mjs';
import '../nodejs/crypto.mjs';
import '../nodejs/fs.mjs';
import '../nodejs/request.mjs';
import '../tencent-cloud/cos/cos.mjs';
import '../tencent-cloud/cos/helper.mjs';
import '../third-party/cos-nodejs-sdk-v5.mjs';
import '../tencent-cloud/cos/link.mjs';
import '../third-party/miniprogram-ci.mjs';
import '../time/time.mjs';
import '../wecom-robot/base.mjs';
import '../wecom-robot/helper.mjs';
import '../fs/fs.mjs';
import '../nodejs/os.mjs';
import '../rainbow/rainbow-user.mjs';
import '../list/flat.mjs';
import '../rainbow/helper/helper.mjs';
import '../rainbow/sdk.mjs';
import '../json/json-parse.mjs';

function flattenSubPackages(result) {
  var _result$subPackageInf = result.subPackageInfo,
    subPackageInfo = _result$subPackageInf === void 0 ? [] : _result$subPackageInf;
  return subPackageInfo.reduce(function (acc, item) {
    acc[item.name] = item;
    return acc;
  }, {});
}
function reportToRd(_ref) {
  var bundleInfo = _ref.bundleInfo,
    rdProjectId = _ref.rdProjectId,
    subProject = _ref.subProject,
    robot = _ref.robot,
    lastCommit = _ref.lastCommit,
    repo = _ref.repo,
    env = _ref.env,
    branch = _ref.branch,
    rdHost = _ref.rdHost,
    bkStartUserName = _ref.bkStartUserName,
    bkStartType = _ref.bkStartType,
    bkBuildUrl = _ref.bkBuildUrl,
    bkPipelineId = _ref.bkPipelineId;
  var _a, _b;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var mainBundleSize, totalBundleSize, reportToRdRes;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (bundleInfo === null || bundleInfo === void 0 ? void 0 : bundleInfo.__APP__) {
            _context.next = 1;
            break;
          }
          return _context.abrupt("return");
        case 1:
          mainBundleSize = parseInt("".concat((((_a = bundleInfo.__APP__) === null || _a === void 0 ? void 0 : _a.size) || 0) / 1024), 10);
          totalBundleSize = parseInt("".concat((((_b = bundleInfo.__FULL__) === null || _b === void 0 ? void 0 : _b.size) || 0) / 1024), 10);
          _context.next = 2;
          return reportToRdPlatform({
            data: {
              projectId: rdProjectId,
              project: repo || '',
              subProject: subProject,
              bkStartUserName: bkStartUserName || '',
              bkStartType: bkStartType || '',
              bkBuildUrl: bkBuildUrl || '',
              bkPipelineId: bkPipelineId || '',
              env: env || '',
              branch: branch || '',
              mainBundleSize: mainBundleSize,
              totalBundleSize: totalBundleSize,
              createTime: Date.now()
            },
            host: rdHost || '',
            type: 2,
            platform: 2
          });
        case 2:
          reportToRdRes = _context.sent;
          _context.next = 3;
          return reportToRdPlatform({
            data: {
              projectId: rdProjectId,
              projectName: repo || '',
              subProjectName: subProject,
              env: env || '',
              branch: branch || '',
              robot: robot,
              lastCommit: lastCommit,
              mainBundleSize: mainBundleSize,
              totalBundleSize: totalBundleSize,
              bkBuildUrl: bkBuildUrl || '',
              time: Date.now(),
              isWx: true
            },
            host: rdHost || '',
            type: 4
          })["catch"](function (err) {
            console.log('[reportToRdRecordRes] err', err);
          });
        case 3:
          console.log('[reportToRdRes]', reportToRdRes);
        case 4:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
function mpUploadAndReport(_ref2) {
  var branch = _ref2.branch,
    env = _ref2.env,
    root = _ref2.root,
    rainbowConfigKey = _ref2.rainbowConfigKey,
    rainbowAppId = _ref2.rainbowAppId,
    rainbowEnvName = _ref2.rainbowEnvName,
    rainbowGroupName = _ref2.rainbowGroupName,
    rdHost = _ref2.rdHost,
    bkStartType = _ref2.bkStartType,
    bkBuildUrl = _ref2.bkBuildUrl,
    bkStartUserName = _ref2.bkStartUserName,
    bkPipelineId = _ref2.bkPipelineId,
    commitInfo = _ref2.commitInfo,
    version = _ref2.version,
    buildDesc = _ref2.buildDesc;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    var rainbowSecretInfo, config, robot, appName, appId, webhookUrl, chatId, cosInfo, rdProjectId, subProject, ciConfig, mpCISetting, ci, _ci$commitInfo, ciCommitInfo, bundleInfo, _t;
    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
          });
          appName = config.appName, appId = config.appId, webhookUrl = config.webhookUrl, chatId = config.chatId, cosInfo = config.cosInfo, rdProjectId = config.rdProjectId, subProject = config.subProject, ciConfig = config.ci, mpCISetting = config.mpCISetting;
          ci = new MpCI({
            appName: appName,
            appId: appId,
            root: root,
            env: env,
            robotNumber: robot,
            webhookUrl: webhookUrl,
            chatId: chatId,
            cosInfo: cosInfo,
            errorLink: bkBuildUrl,
            commitInfo: commitInfo,
            version: version,
            buildDesc: buildDesc,
            buildSetting: mpCISetting || {}
          });
          _context2.next = 2;
          return ci.uploadAndPreview();
        case 2:
          _ci$commitInfo = ci.commitInfo, ciCommitInfo = _ci$commitInfo === void 0 ? {} : _ci$commitInfo;
          bundleInfo = flattenSubPackages(ci.previewResult || {});
          _context2.prev = 3;
          _context2.next = 4;
          return reportToRd({
            bundleInfo: bundleInfo,
            rdProjectId: rdProjectId,
            subProject: subProject,
            robot: robot,
            lastCommit: "".concat(ciCommitInfo.author, " - ").concat(ciCommitInfo.message, " - ").concat(ciCommitInfo.hash),
            repo: ciConfig.repo || '',
            env: env,
            branch: branch,
            rdHost: rdHost,
            bkStartUserName: bkStartUserName,
            bkStartType: bkStartType,
            bkBuildUrl: bkBuildUrl,
            bkPipelineId: bkPipelineId
          });
        case 4:
          _context2.next = 6;
          break;
        case 5:
          _context2.prev = 5;
          _t = _context2["catch"](3);
          console.log('[reportToRd] err: ', _t);
        case 6:
          if (!(bkStartType !== 'TIME_TRIGGER')) {
            _context2.next = 7;
            break;
          }
          _context2.next = 7;
          return ci.sendRobotMsg();
        case 7:
        case "end":
          return _context2.stop();
      }
    }, _callee2, null, [[3, 5]]);
  }));
}
function mpUploadAndReportByOptions(options) {
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
    var branch, env, root, rainbowConfigKey, rainbowAppId, rainbowEnvName, rainbowGroupName, rdHost, bkStartType, bkBuildUrl, bkStartUserName, bkPipelineId, commitAuthor, commitMessage, commitHash, mpVersion;
    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, rdHost = options.rdHost, bkStartType = options.bkStartType, bkBuildUrl = options.bkBuildUrl, bkStartUserName = options.bkStartUserName, bkPipelineId = options.bkPipelineId, commitAuthor = options.commitAuthor, commitMessage = options.commitMessage, commitHash = options.commitHash, mpVersion = options.mpVersion;
          if (!(!branch || !env || !root || !rainbowConfigKey || !rainbowAppId || !rainbowEnvName || !rainbowGroupName || !rdHost || !bkStartType || !bkBuildUrl || !bkStartUserName || !bkPipelineId || !commitAuthor || !commitMessage || !commitHash || !mpVersion)) {
            _context3.next = 1;
            break;
          }
          console.error('缺少必要参数，请检查！');
          return _context3.abrupt("return");
        case 1:
          mpUploadAndReport({
            branch: branch,
            env: env,
            root: root,
            rainbowConfigKey: rainbowConfigKey,
            rainbowAppId: rainbowAppId,
            rainbowEnvName: rainbowEnvName,
            rainbowGroupName: rainbowGroupName,
            rdHost: rdHost,
            bkStartType: bkStartType,
            bkBuildUrl: bkBuildUrl,
            bkStartUserName: bkStartUserName,
            bkPipelineId: bkPipelineId,
            commitInfo: {
              author: commitAuthor,
              message: commitMessage,
              hash: commitHash,
              branch: branch
            },
            buildDesc: getInnerBundleBuildDesc({
              env: env,
              branch: branch,
              author: commitAuthor,
              message: commitMessage
            }),
            version: mpVersion
          });
        case 2:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
}

export { mpUploadAndReport, mpUploadAndReportByOptions };
