import a from './index5.mjs';
import { i as inquirer } from './index6.mjs';
import { exec as q } from './main.mjs';
import { s as showImpactReminder } from '../shared/ccjk.C1hANZTu.mjs';
import '../shared/ccjk.BAGoDD49.mjs';
import 'node:readline';
import 'stream';
import 'node:tty';
import 'node:process';
import 'node:async_hooks';
import '../shared/ccjk.Cjgrln_h.mjs';
import 'node:util';
import 'tty';
import 'fs';
import 'child_process';
import 'node:path';
import 'node:os';
import 'node:crypto';
import 'buffer';
import 'string_decoder';
import 'module';
import 'node:child_process';
import 'node:stream';

async function checkGitRepo() {
  try {
    await q("git", ["rev-parse", "--git-dir"]);
    return true;
  } catch {
    return false;
  }
}
async function getGitStatus() {
  const isRepo = await checkGitRepo();
  if (!isRepo)
    return { isRepo: false, hasChanges: false, staged: [], unstaged: [], untracked: [] };
  const { stdout } = await q("git", ["status", "--porcelain"]);
  const lines = stdout.trim().split("\n").filter(Boolean);
  const staged = [];
  const unstaged = [];
  const untracked = [];
  for (const line of lines) {
    const [s1, s2] = line.substring(0, 2);
    const file = line.substring(3);
    if (s1 !== " " && s1 !== "?")
      staged.push(file);
    if (s2 !== " ")
      unstaged.push(file);
    if (line.startsWith("??"))
      untracked.push(file);
  }
  return {
    isRepo: true,
    hasChanges: staged.length + unstaged.length + untracked.length > 0,
    staged,
    unstaged,
    untracked
  };
}
async function generateCommitMessage(files) {
  if (files.length === 0)
    return "chore: update files";
  const { stdout: diff } = await q("git", ["diff", "--cached", "--stat"]);
  const hasTest = files.some((f) => f.includes("test") || f.includes("spec"));
  const hasDocs = files.some((f) => f.endsWith(".md"));
  const hasConfig = files.some((f) => f.endsWith(".json") || f.endsWith(".yaml"));
  const hasTs = files.some((f) => f.endsWith(".ts"));
  const type = hasTest ? "test" : hasDocs ? "docs" : hasConfig ? "config" : hasTs ? "feat" : "chore";
  const scope = files.length === 1 ? files[0].split("/")[0] : void 0;
  const fileList = files.length <= 3 ? files.join(", ") : `${files.length} files`;
  const message = scope ? `${type}(${scope}): update ${fileList}` : `${type}: update ${fileList}`;
  return `${message}

${diff.trim()}`;
}
async function stageAllChanges() {
  await q("git", ["add", "-A"]);
}
async function commitChanges(message) {
  await q("git", ["commit", "-m", message]);
}

async function commit(options = {}) {
  if (!await checkGitRepo()) {
    console.log(a.red("\u2717 Not a git repository"));
    return;
  }
  const status = await getGitStatus();
  if (!status.hasChanges) {
    console.log(a.yellow("No changes to commit"));
    return;
  }
  console.log(a.green("\n\u{1F4DD} Changes detected:"));
  if (status.staged.length > 0) {
    console.log(a.green(`  Staged: ${status.staged.length} files`));
    status.staged.forEach((f) => console.log(a.gray(`    ${f}`)));
  }
  if (status.unstaged.length > 0)
    console.log(a.yellow(`  Unstaged: ${status.unstaged.length} files`));
  if (status.untracked.length > 0)
    console.log(a.yellow(`  Untracked: ${status.untracked.length} files`));
  if (status.unstaged.length > 0 || status.untracked.length > 0) {
    await stageAllChanges();
    console.log(a.green("\n\u2713 All changes staged"));
  }
  const allFiles = [...status.staged, ...status.unstaged, ...status.untracked];
  let message;
  if (options.message) {
    message = options.message;
  } else if (options.auto) {
    message = await generateCommitMessage(allFiles);
    console.log(a.green("\n\u{1F4AC} Generated commit message:"));
    console.log(a.white(message));
  } else {
    const suggested = await generateCommitMessage(allFiles);
    const { commitMessage } = await inquirer.prompt([{
      type: "input",
      name: "commitMessage",
      message: "Commit message:",
      default: suggested.split("\n")[0]
    }]);
    message = commitMessage;
  }
  if (options.dryRun) {
    console.log(a.yellow("\n\u{1F50D} Dry run - no commit created"));
    console.log(a.gray("Would commit with:"));
    console.log(a.white(message));
    return;
  }
  try {
    await commitChanges(message);
    console.log(a.green("\n\u2713 Changes committed"));
    showImpactReminder("commit");
  } catch (error) {
    console.log(a.red(`
\u2717 Commit failed: ${error}`));
  }
}

export { commit };
