'use strict';

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

var git_submodule = require('../git/submodule.js');
var node_nodeCommand = require('../node/node-command.js');
var nodejs_fs = require('../nodejs/fs.js');
var nodejs_path = require('../nodejs/path.js');
require('../fs/fs.js');
require('../nodejs/crypto.js');
require('../nodejs/os.js');
require('@babel/runtime/helpers/typeof');
require('../nodejs/child_process.js');

function getSubmodulePathStr(file) {
  var submodules = git_submodule.getSubmodulePathList(file);
  var str = "\n".concat(submodules.map(function (item) {
    return "".concat(item, "/*");
  }).join('\n'));
  return str;
}
/**
 * 检查 ignore 文件是否已包含 submodule 忽略内容，
 * 只有不存在或未包含时才追加，避免重复写入
 */
function appendIgnoreIfNeeded(ignoreFile, contentToAppend, root) {
  var fs = nodejs_fs.getFs();
  var path = nodejs_path.getPath();
  var fullPath = path.resolve(root, ignoreFile);
  try {
    if (fs.existsSync(fullPath)) {
      var existing = fs.readFileSync(fullPath, 'utf-8');
      // 取 contentToAppend 中的路径行进行检查
      var lines = contentToAppend.split('\n').filter(function (l) {
        return l.trim();
      });
      var alreadyExists = lines.length > 0 && lines.every(function (line) {
        return existing.includes(line);
      });
      if (alreadyExists) {
        console.log("[ignoreSubmodule] ".concat(ignoreFile, " \u5DF2\u5305\u542B submodule \u5FFD\u7565\u5185\u5BB9\uFF0C\u8DF3\u8FC7"));
        return;
      }
    }
  } catch (_a) {
    // 文件不存在或读取失败，继续追加
  }
  node_nodeCommand.execCommand("echo \"".concat(contentToAppend, "\" >> ").concat(ignoreFile), root, 'inherit');
}
function ignoreSubmoduleInESLint() {
  var root = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : process.cwd();
  var str = getSubmodulePathStr(nodejs_path.getPath().resolve(root, '.gitmodules'));
  appendIgnoreIfNeeded('.eslintignore', str, root);
}
function ignoreSubmoduleInStyleLint() {
  var root = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : process.cwd();
  var str = getSubmodulePathStr(nodejs_path.getPath().resolve(root, '.gitmodules'));
  appendIgnoreIfNeeded('.stylelintignore', str, root);
}

exports.ignoreSubmoduleInESLint = ignoreSubmoduleInESLint;
exports.ignoreSubmoduleInStyleLint = ignoreSubmoduleInStyleLint;
