'use strict';

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

var bite_formatBite = require('../bite/format-bite.js');
var git_git = require('../git/git.js');
var nodejs_path = require('../nodejs/path.js');
var wecomRobot_message = require('../wecom-robot/message.js');
var mpCi_config = require('./config.js');
require('../node/node-command.js');
require('@babel/runtime/helpers/typeof');
require('../nodejs/child_process.js');
require('../validate/validate.js');

/* eslint-disable @typescript-eslint/no-require-imports */
function getInnerBundleBuildDesc(_ref) {
  var _ref$env = _ref.env,
    env = _ref$env === void 0 ? '' : _ref$env,
    _ref$branch = _ref.branch,
    branch = _ref$branch === void 0 ? '' : _ref$branch,
    _ref$author = _ref.author,
    author = _ref$author === void 0 ? '' : _ref$author,
    _ref$message = _ref.message,
    message = _ref$message === void 0 ? '' : _ref$message;
  var buildDesc = wecomRobot_message.genRobotMessage([[{
    label: '环境',
    content: env || ''
  }, {
    label: '分支',
    content: branch || ''
  }, {
    label: '最后提交',
    content: "".concat(author, " - ").concat(message)
  }]]);
  return buildDesc;
}
function getBundleBuildDesc(_ref2) {
  var root = _ref2.root,
    _ref2$env = _ref2.env,
    env = _ref2$env === void 0 ? 'test' : _ref2$env,
    branch = _ref2.branch,
    author = _ref2.author,
    message = _ref2.message;
  var commitInfo = git_git.getGitCommitInfo(root, false, false, false);
  return getInnerBundleBuildDesc({
    env: env,
    branch: branch || commitInfo.branch,
    author: author || commitInfo.author,
    message: message || commitInfo.message
  });
}
function getBundleVersion(root) {
  try {
    var pkgFile = nodejs_path.getPath().resolve(root, './package.json');
    var pkg = require(pkgFile) || {};
    return pkg.version;
  } catch (err) {
    console.log('[getBundleVersion] error: ', err);
  }
  return '';
}
/**
 * 解析上传结果
 * @example
 * ```ts
 * parseUploadResult({
 *   subPackageInfo: [
 *     { name: '__APP__', size: 102400 },
 *     { name: 'pages/foo', size: 51200 },
 *   ],
 * });
 * // [
 * //   'PACKAGE SIZE INFO: ',
 * //   '- 主包: 100KB',
 * //   '- pages/foo: 50KB',
 * // ]
 * ```
 * @ignore
 */
function parseUploadResult(result) {
  var subPackageInfo = result.subPackageInfo;
  subPackageInfo.reverse();
  var list = subPackageInfo.sort(function (a, b) {
    var keys = Object.keys(mpCi_config.BUNDLE_NAME_MAP);
    if (keys.indexOf(a.name) > -1 || keys.indexOf(b.name) > -1) {
      return keys.indexOf(b.name) - keys.indexOf(a.name);
    }
    return b.size - a.size;
  }).map(function (pkg) {
    return "- ".concat(mpCi_config.BUNDLE_NAME_MAP[pkg.name] || pkg.name, ": ").concat(bite_formatBite.formatBite(pkg.size));
  });
  list.unshift('PACKAGE SIZE INFO: ');
  return list;
}
function flattenSubPackages(result) {
  var _ref3 = result || {},
    _ref3$subPackageInfo = _ref3.subPackageInfo,
    subPackageInfo = _ref3$subPackageInfo === void 0 ? [] : _ref3$subPackageInfo;
  return subPackageInfo.reduce(function (acc, item) {
    acc[item.name] = item;
    return acc;
  }, {});
}

exports.flattenSubPackages = flattenSubPackages;
exports.getBundleBuildDesc = getBundleBuildDesc;
exports.getBundleVersion = getBundleVersion;
exports.getInnerBundleBuildDesc = getInnerBundleBuildDesc;
exports.parseUploadResult = parseUploadResult;
