import { replaceAllPolyfill } from '../string/replace-all.mjs';
import { readFileSync, writeFileSync } from '../fs/fs.mjs';
import { getChildProcess } from '../nodejs/child_process.mjs';
import '../nodejs/crypto.mjs';
import '../nodejs/fs.mjs';
import '../nodejs/os.mjs';

function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var replaceList = [{
  from: '🐛 Bug Fixes | Bug 修复',
  to: 'Bug Fixes 🐞'
}, {
  from: '✨ Features | 新功能',
  to: 'Features 🎉'
}, {
  from: '✏️ Documentation | 文档',
  to: 'Documentation 📖'
}, {
  from: '♻️ Code Refactoring | 代码重构',
  to: 'Code Refactoring ♻️'
}, {
  from: '🚀 Chore | 构建/工程依赖/工具',
  to: 'Chore 🚀 '
}, {
  from: '📦‍ Build System | 打包构建',
  to: 'Build System 📦‍'
}, {
  from: '✅ Tests | 测试',
  to: 'Tests ✅'
}, {
  from: '💄 Styles | 风格',
  to: 'Styles 💄'
}];
function replaceChangeLogKeywords() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  replaceAllPolyfill();
  var newData = data;
  var _iterator = _createForOfIteratorHelper(replaceList),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var item = _step.value;
      var from = item.from,
        to = item.to;
      newData = newData.replaceAll(from, to);
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return newData;
}
/**
 * 同步最新版本的更新日志
 * @param {object} params 参数
 * @param {string} params.changelogPath 源 change-log路径
 * @param {string} params.docChangelogPath 文档 change-log 路径
 * @param {string} params.packageJsonPath package.json 路径
 *
 * @example
 * ```ts
 * const DOC_CHANGE_LOG_PATH = './docs/CHANGELOG.md';
 * const SOURCE_CHANGE_LOG_PATH = './CHANGELOG.md';
 *
 * insertDocChangeLog({
 *   changelogPath: SOURCE_CHANGE_LOG_PATH,
 *   docChangeLog: DOC_CHANGE_LOG_PATH,
 *   packageJsonPath: './package.json',
 * });
 * ```
 */
function insertDocChangeLog(_ref) {
  var changelogPath = _ref.changelogPath,
    docChangelogPath = _ref.docChangelogPath,
    packageJsonPath = _ref.packageJsonPath;
  var changeLog = readFileSync(changelogPath, false);
  var pkg = readFileSync(packageJsonPath, true);
  var version = pkg.version;
  // version = '1.0.31';
  console.log('[insertDocChangeLog] version: ', version);
  if (!version) return;
  var reg = new RegExp("\\n([#]+\\s*\\[".concat(version, "\\].*?\\n)(?=[#]+\\s*\\[\\d+\\.\\d+\\.\\d+)"), 's');
  var match = changeLog.match(reg);
  if (!(match === null || match === void 0 ? void 0 : match[1])) return;
  console.log('[insertDocChangeLog] match: ', match[1]);
  var docChangeLog = readFileSync(docChangelogPath);
  var newDocChangeLog = docChangeLog.replace(/(?=([#]{2,}))/, match[1]);
  newDocChangeLog = replaceChangeLogKeywords(newDocChangeLog);
  writeFileSync(docChangelogPath, newDocChangeLog);
  getChildProcess().execSync('git add .', {
    stdio: 'inherit'
  });
}

export { insertDocChangeLog };
