'use strict';

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

var node_nodeCommand = require('../node/node-command.js');
var nodejs_fs = require('../nodejs/fs.js');
var nodejs_path = require('../nodejs/path.js');
var time_time = require('../time/time.js');
var versionTip_config = require('./config.js');
var git_git = require('../git/git.js');
require('@babel/runtime/helpers/typeof');
require('../nodejs/child_process.js');
require('../validate/validate.js');

function getTimeStampFromDate(date) {
  return new Date(date).getTime();
}
function doRelease(_ref) {
  var isFirstRelease = _ref.isFirstRelease,
    root = _ref.root;
  console.log('[GEN VERSION] Doing standard-version ...');
  if (isFirstRelease) {
    node_nodeCommand.execCommand('npx standard-version --first-release', root);
  } else {
    node_nodeCommand.execCommand('npx standard-version --release-as patch', root);
  }
  console.log('[GEN VERSION] Done standard-version.');
}
/**
 * 是否应该执行 standard-version
 * 返回0，不执行
 * 返回1，执行--first-release
 * 返回2，执行--release-as patch
 * @private
 * @param {string} [] 命令执行目录
 * @returns {number} 是否应该执行 standard-version
 * @example
 * ```ts
 * const code = shouldGenVersion(process.cwd());
 * // 0 = 不执行；1 = --first-release；2 = --release-as patch
 *
 * // 强制生成
 * shouldGenVersion(process.cwd(), true);
 * ```
 */
function shouldGenVersion(root, forceGenVersion) {
  var fs = nodejs_fs.getFs();
  if (!root) {
    console.log('[GEN VERSION] ERROR: Please input root. Such as process.cwd()');
    return versionTip_config.TAG_MAP.NO_TAG;
  }
  var INTERVAL_TIME = 24 * 60 * 60 * 1000;
  var gitPath = nodejs_path.getPath().resolve(root, '.git');
  console.log('[GEN VERSION] GitPath: ', gitPath);
  if (!fs.existsSync(gitPath)) {
    console.log("[GEN VERSION] ERROR: NOT FOUND .git of ".concat(gitPath));
    return versionTip_config.TAG_MAP.NO_TAG;
  }
  var tag = git_git.getGitLastTag(root);
  console.log("[GEN VERSION] Tag: ".concat(tag));
  if (!tag) {
    return versionTip_config.TAG_MAP.FIRST_TAG;
  }
  if (forceGenVersion) {
    return versionTip_config.TAG_MAP.MORE_TAG;
  }
  var tagDate = git_git.getGitTagTime(tag, root);
  console.log("[GEN VERSION] Tag Date: ".concat(tagDate));
  var commits = git_git.getGitCommitsBeforeTag(tag, root);
  console.log("[GEN VERSION] Commit number: ".concat(commits));
  if (Number(commits) < 1) {
    console.log('[GEN VERSION] ERROR: commit number less than 1');
    return versionTip_config.TAG_MAP.NO_TAG;
  }
  var tagTimeStamp = getTimeStampFromDate(tagDate);
  var dateStr = time_time.timeStampFormat(tagTimeStamp, 'yyyy-MM-dd hh:mm:ss');
  console.log("[GEN VERSION] Tag TimeStamp: ".concat(dateStr));
  if (Date.now() - tagTimeStamp < INTERVAL_TIME) {
    console.log("[GEN VERSION] ERROR: \u95F4\u9694\u5C0F\u4E8E".concat(INTERVAL_TIME));
    return versionTip_config.TAG_MAP.NO_TAG;
  }
  return versionTip_config.TAG_MAP.MORE_TAG;
}
/**
 * 自动生成version，核心是利用 standard-version 命令
 * @param {object} config 配置信息
 * @param {string} config.root 项目根路径
 * @returns {boolean} 是否执行了 standard-version
 * @example
 *
 * genVersion({
 *   root: process.cwd()
 * })
 *
 */
function genVersion(_ref2) {
  var root = _ref2.root,
    _ref2$forceGenVersion = _ref2.forceGenVersion,
    forceGenVersion = _ref2$forceGenVersion === void 0 ? false : _ref2$forceGenVersion;
  var genType = shouldGenVersion(root, forceGenVersion);
  if (!genType) return false;
  if (genType === versionTip_config.TAG_MAP.FIRST_TAG) {
    doRelease({
      isFirstRelease: true,
      root: root
    });
    return true;
  }
  if (genType === versionTip_config.TAG_MAP.MORE_TAG) {
    doRelease({
      isFirstRelease: false,
      root: root
    });
    return true;
  }
  return false;
}

exports.genVersion = genVersion;
exports.shouldGenVersion = shouldGenVersion;
