'use strict';

require('fs');
require('path');
var child_process = require('child_process');
require('util');

// src/platform/core/helpers.ts
function runGitCommand(rootDir, command) {
  return child_process.execSync(command, {
    cwd: rootDir,
    encoding: "utf8",
    maxBuffer: 1024 * 1024 * 20,
    stdio: ["ignore", "pipe", "pipe"]
  }).trim();
}
var COMMIT_FETCH_LIMIT = 200;

// src/features/review/domain/presentation.ts
function truncateCommitSubject(subject) {
  if (subject.length <= 72) {
    return subject;
  }
  return `${subject.slice(0, 69)}...`;
}
function formatReviewTargetFileCount(fileCount) {
  return fileCount === 0 ? "\uBCC0\uACBD \uC5C6\uC74C" : `${fileCount}\uAC1C \uD30C\uC77C`;
}
function buildReviewTargetSectionTitle(target) {
  return target.kind === "commit" ? `${target.hash} ${target.subject}` : `${target.hash} | ${target.subject}`;
}

// src/features/review/domain/targets.ts
function createWorkingTreeReviewOption(kind, fileCount) {
  const subject = kind === "unstaged" ? "\uC544\uC9C1 git add \uD558\uC9C0 \uC54A\uC740 \uBCC0\uACBD\uC0AC\uD56D" : "git add \uB41C \uBCC0\uACBD\uC0AC\uD56D";
  return {
    author: "",
    hash: kind,
    kind,
    label: kind,
    relativeDate: "",
    subject: `${subject} | ${formatReviewTargetFileCount(fileCount)}`
  };
}
function parseRecentCommitOptions(output) {
  if (!output) {
    return [];
  }
  return output.split("\n").map((line) => {
    const [hash = "", author = "", relativeDate = "", ...subjectParts] = line.split("	");
    const subject = subjectParts.join("	").trim();
    return {
      author,
      hash,
      kind: "commit",
      label: `${hash} | ${truncateCommitSubject(subject)}`,
      relativeDate,
      subject
    };
  });
}

// src/features/review/infrastructure/git.ts
function getWorkingTreeFileList(rootDir, kind) {
  const command = kind === "staged" ? "git diff --cached --name-only" : "git diff --name-only";
  const output = runGitCommand(rootDir, command);
  return output.split("\n").map((line) => line.trim()).filter(Boolean);
}
function getRecentCommitOptions(rootDir) {
  const output = runGitCommand(rootDir, `git log --date=relative --pretty=format:%h%x09%an%x09%ar%x09%s -${COMMIT_FETCH_LIMIT}`);
  return parseRecentCommitOptions(output);
}
function getReviewTargetOptions(rootDir) {
  return [
    createWorkingTreeReviewOption("unstaged", getWorkingTreeFileList(rootDir, "unstaged").length),
    createWorkingTreeReviewOption("staged", getWorkingTreeFileList(rootDir, "staged").length),
    ...getRecentCommitOptions(rootDir)
  ];
}
function buildSelectedCommitDiff(rootDir, commits) {
  return commits.map((commit) => {
    const diff = commit.kind === "commit" ? runGitCommand(rootDir, `git show --stat --patch ${commit.hash}`) : runGitCommand(rootDir, commit.kind === "staged" ? "git diff --cached --stat --patch" : "git diff --stat --patch");
    if (!diff) {
      return "";
    }
    return [`## ${buildReviewTargetSectionTitle(commit)}`, diff].join("\n\n");
  }).filter(Boolean).join("\n\n");
}
function getReviewTargetFilePaths(rootDir, target) {
  if (target.kind === "commit") {
    const output = runGitCommand(rootDir, `git show --name-only --format= ${target.hash}`);
    return output.split("\n").map((line) => line.trim()).filter(Boolean);
  }
  return getWorkingTreeFileList(rootDir, target.kind);
}
function buildSelectedCommitFilePaths(rootDir, targets) {
  return Array.from(new Set(targets.flatMap((target) => getReviewTargetFilePaths(rootDir, target))));
}

exports.buildSelectedCommitDiff = buildSelectedCommitDiff;
exports.buildSelectedCommitFilePaths = buildSelectedCommitFilePaths;
exports.getRecentCommitOptions = getRecentCommitOptions;
exports.getReviewTargetFilePaths = getReviewTargetFilePaths;
exports.getReviewTargetOptions = getReviewTargetOptions;
exports.getWorkingTreeFileList = getWorkingTreeFileList;
