"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var git_exports = {};
__export(git_exports, {
  gitFilter: () => gitFilter
});
module.exports = __toCommonJS(git_exports);
const gitFilter = {
  name: "git",
  matches(command) {
    return /^\s*(git\s|rtk\s+git\s)/.test(command);
  },
  filter(command, rawOutput, level) {
    const subcommand = extractGitSubcommand(command);
    switch (subcommand) {
      case "status":
        return filterGitStatus(rawOutput, level);
      case "log":
        return filterGitLog(rawOutput, level);
      case "diff":
        return filterGitDiff(rawOutput, level);
      case "push":
        return filterGitPush(rawOutput);
      case "pull":
        return filterGitPull(rawOutput);
      case "add":
        return filterGitAdd(rawOutput);
      case "commit":
        return filterGitCommit(rawOutput);
      case "fetch":
        return filterGitFetch(rawOutput);
      case "branch":
        return filterGitBranch(rawOutput, level);
      case "stash":
        return filterGitStash(rawOutput);
      default:
        return { output: rawOutput, strategy: "git:passthrough" };
    }
  }
};
function extractGitSubcommand(command) {
  const match = command.match(/git\s+(\w+)/);
  return match ? match[1] : "";
}
function filterGitStatus(raw, level) {
  const lines = raw.split("\n").filter((l) => l.trim());
  if (raw.includes("nothing to commit") || raw.includes("working tree clean")) {
    return { output: "clean", strategy: "git:status:clean" };
  }
  const staged = [];
  const modified = [];
  const untracked = [];
  const deleted = [];
  const renamed = [];
  let section = "";
  for (const line of lines) {
    if (line.includes("Changes to be committed")) {
      section = "staged";
      continue;
    }
    if (line.includes("Changes not staged")) {
      section = "unstaged";
      continue;
    }
    if (line.includes("Untracked files")) {
      section = "untracked";
      continue;
    }
    if (line.startsWith("  (use")) continue;
    if (line.startsWith("On branch") || line.startsWith("Your branch")) continue;
    const trimmed = line.trim();
    if (!trimmed) continue;
    const shortMatch = trimmed.match(/^([MADRCU?! ]{1,2})\s+(.+)$/);
    if (shortMatch) {
      const [, status, file] = shortMatch;
      if (status.includes("M")) modified.push(file);
      else if (status.includes("A")) staged.push(file);
      else if (status.includes("D")) deleted.push(file);
      else if (status.includes("R")) renamed.push(file);
      else if (status.includes("?")) untracked.push(file);
      continue;
    }
    if (section === "staged") {
      const m = trimmed.match(/(?:new file|modified|deleted|renamed):\s+(.+)/);
      if (m) staged.push(m[1]);
    } else if (section === "unstaged") {
      const m = trimmed.match(/(?:modified|deleted):\s+(.+)/);
      if (m) modified.push(m[1]);
    } else if (section === "untracked") {
      if (!trimmed.startsWith("(")) untracked.push(trimmed);
    }
  }
  if (level === "ultra") {
    const parts2 = [];
    if (staged.length) parts2.push(`S:${staged.length}`);
    if (modified.length) parts2.push(`M:${modified.length}`);
    if (deleted.length) parts2.push(`D:${deleted.length}`);
    if (untracked.length) parts2.push(`?:${untracked.length}`);
    if (renamed.length) parts2.push(`R:${renamed.length}`);
    return { output: parts2.join(" ") || "clean", strategy: "git:status:ultra" };
  }
  const parts = [];
  if (staged.length) parts.push(`Staged (${staged.length}): ${groupByDir(staged, level)}`);
  if (modified.length) parts.push(`Modified (${modified.length}): ${groupByDir(modified, level)}`);
  if (deleted.length) parts.push(`Deleted (${deleted.length}): ${groupByDir(deleted, level)}`);
  if (untracked.length) parts.push(`Untracked (${untracked.length}): ${groupByDir(untracked, level)}`);
  if (renamed.length) parts.push(`Renamed (${renamed.length}): ${groupByDir(renamed, level)}`);
  return { output: parts.join("\n") || raw, strategy: "git:status" };
}
function filterGitLog(raw, level) {
  const lines = raw.split("\n");
  if (lines.every((l) => !l.startsWith("commit ") && !l.startsWith("Author:"))) {
    const filtered = lines.filter((l) => l.trim()).slice(0, level === "ultra" ? 10 : 20);
    return { output: filtered.join("\n"), strategy: "git:log:oneline" };
  }
  const commits = [];
  let currentHash = "";
  let currentMsg = "";
  for (const line of lines) {
    if (line.startsWith("commit ")) {
      if (currentHash && currentMsg) {
        commits.push(`${currentHash.slice(0, 7)} ${currentMsg.trim()}`);
      }
      currentHash = line.replace("commit ", "").trim();
      currentMsg = "";
    } else if (!line.startsWith("Author:") && !line.startsWith("Date:") && !line.startsWith("Merge:")) {
      if (line.trim()) currentMsg = currentMsg || line.trim();
    }
  }
  if (currentHash && currentMsg) {
    commits.push(`${currentHash.slice(0, 7)} ${currentMsg.trim()}`);
  }
  const limit = level === "ultra" ? 10 : 20;
  const output = commits.slice(0, limit).join("\n");
  return { output: output || raw, strategy: "git:log" };
}
function filterGitDiff(raw, level) {
  const lines = raw.split("\n");
  const result = [];
  let contextLines = level === "ultra" ? 1 : level === "aggressive" ? 2 : 3;
  let inHunk = false;
  let afterChange = 0;
  let contextBuffer = [];
  for (const line of lines) {
    if (line.startsWith("diff --git") || line.startsWith("---") || line.startsWith("+++")) {
      result.push(line);
      inHunk = false;
      contextBuffer = [];
      continue;
    }
    if (line.startsWith("@@")) {
      result.push(line);
      inHunk = true;
      afterChange = 0;
      contextBuffer = [];
      continue;
    }
    if (!inHunk) continue;
    if (line.startsWith("+") || line.startsWith("-")) {
      if (contextBuffer.length > 0) {
        const keep = contextBuffer.slice(-contextLines);
        result.push(...keep);
        contextBuffer = [];
      }
      result.push(line);
      afterChange = 0;
      continue;
    }
    afterChange++;
    if (afterChange <= contextLines) {
      result.push(line);
    } else {
      contextBuffer.push(line);
    }
  }
  return { output: result.join("\n"), strategy: "git:diff" };
}
function filterGitPush(raw) {
  const branchMatch = raw.match(/(\S+)\s*->\s*(\S+)/);
  if (branchMatch) {
    return { output: `ok ${branchMatch[1]} \u2192 ${branchMatch[2]}`, strategy: "git:push" };
  }
  if (raw.includes("Everything up-to-date")) {
    return { output: "ok (up-to-date)", strategy: "git:push" };
  }
  const meaningful = raw.split("\n").filter((l) => l.trim() && !l.includes("Enumerating") && !l.includes("Counting") && !l.includes("Compressing") && !l.includes("Writing"));
  return { output: meaningful.pop() || "ok", strategy: "git:push" };
}
function filterGitPull(raw) {
  if (raw.includes("Already up to date")) {
    return { output: "ok (up-to-date)", strategy: "git:pull" };
  }
  const filesMatch = raw.match(/(\d+)\s+file/);
  const insertMatch = raw.match(/(\d+)\s+insertion/);
  const deleteMatch = raw.match(/(\d+)\s+deletion/);
  const files = filesMatch ? filesMatch[1] : "0";
  const ins = insertMatch ? `+${insertMatch[1]}` : "+0";
  const del = deleteMatch ? `-${deleteMatch[1]}` : "-0";
  return { output: `ok ${files} files ${ins} ${del}`, strategy: "git:pull" };
}
function filterGitAdd(raw) {
  if (!raw.trim()) return { output: "ok", strategy: "git:add" };
  return { output: raw.trim() || "ok", strategy: "git:add" };
}
function filterGitCommit(raw) {
  const hashMatch = raw.match(/\[[\w/]+\s+([a-f0-9]+)\]/);
  if (hashMatch) {
    return { output: `ok ${hashMatch[1]}`, strategy: "git:commit" };
  }
  const firstLine = raw.split("\n").find((l) => l.trim());
  return { output: firstLine || "ok", strategy: "git:commit" };
}
function filterGitFetch(raw) {
  if (!raw.trim()) return { output: "ok", strategy: "git:fetch" };
  const lines = raw.split("\n").filter((l) => l.trim() && !l.startsWith("remote:"));
  return { output: lines.length > 0 ? lines.join("\n") : "ok", strategy: "git:fetch" };
}
function filterGitBranch(raw, level) {
  const lines = raw.split("\n").filter((l) => l.trim());
  const current = lines.find((l) => l.startsWith("*"));
  const others = lines.filter((l) => !l.startsWith("*")).map((l) => l.trim());
  if (level === "ultra") {
    return {
      output: `* ${current?.replace("*", "").trim() || "?"} (+${others.length} branches)`,
      strategy: "git:branch:ultra"
    };
  }
  const limit = level === "aggressive" ? 10 : 20;
  const shown = others.slice(0, limit);
  const parts = [current || "* (unknown)"];
  parts.push(...shown);
  if (others.length > limit) parts.push(`  \u2026and ${others.length - limit} more`);
  return { output: parts.join("\n"), strategy: "git:branch" };
}
function filterGitStash(raw) {
  const lines = raw.split("\n").filter((l) => l.trim());
  if (lines.length === 0) return { output: "ok", strategy: "git:stash" };
  return { output: lines.slice(0, 10).join("\n"), strategy: "git:stash" };
}
function groupByDir(files, level) {
  if (level === "aggressive" || level === "ultra") {
    const dirs = /* @__PURE__ */ new Map();
    for (const f of files) {
      const dir = f.includes("/") ? f.slice(0, f.lastIndexOf("/")) : ".";
      dirs.set(dir, (dirs.get(dir) || 0) + 1);
    }
    return [...dirs.entries()].map(([d, c]) => `${d}/ (${c})`).join(", ");
  }
  if (files.length <= 5) return files.join(", ");
  return files.slice(0, 5).join(", ") + ` \u2026+${files.length - 5} more`;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  gitFilter
});
//# sourceMappingURL=git.js.map
