"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 files_exports = {};
__export(files_exports, {
  filesFilter: () => filesFilter
});
module.exports = __toCommonJS(files_exports);
const filesFilter = {
  name: "files",
  matches(command) {
    return /^\s*(ls|find|tree|dir)\b/.test(command);
  },
  filter(command, rawOutput, level) {
    if (/^\s*tree\b/.test(command)) return filterTree(rawOutput, level);
    if (/^\s*find\b/.test(command)) return filterFind(rawOutput, level);
    if (/^\s*ls\b/.test(command)) return filterLs(rawOutput, level);
    return { output: rawOutput, strategy: "files:passthrough" };
  }
};
function filterLs(raw, level) {
  const lines = raw.split("\n").filter((l) => l.trim());
  if (lines.length <= 10) {
    return { output: raw, strategy: "ls:short" };
  }
  const isLongFormat = lines.some((l) => /^[drwx-]{10}/.test(l));
  if (isLongFormat) {
    const dirs = [];
    const files = [];
    for (const line of lines) {
      if (line.startsWith("total")) continue;
      const parts2 = line.split(/\s+/);
      const name = parts2[parts2.length - 1];
      if (!name || name === "." || name === "..") continue;
      if (line.startsWith("d")) {
        dirs.push(name + "/");
      } else {
        files.push(name);
      }
    }
    if (level === "ultra") {
      return {
        output: `${dirs.length} dirs, ${files.length} files`,
        strategy: "ls:ultra"
      };
    }
    if (level === "aggressive") {
      const dirList = dirs.length > 0 ? `Dirs (${dirs.length}): ${dirs.slice(0, 10).join(", ")}${dirs.length > 10 ? "\u2026" : ""}` : "";
      const fileList = files.length > 0 ? `Files (${files.length}): ${groupFilesByExt(files)}` : "";
      return { output: [dirList, fileList].filter(Boolean).join("\n"), strategy: "ls:grouped" };
    }
    const parts = [];
    if (dirs.length > 0) {
      parts.push(`Directories (${dirs.length}): ${dirs.join(", ")}`);
    }
    if (files.length > 0) {
      const maxFiles = 20;
      parts.push(`Files (${files.length}):`);
      parts.push(...files.slice(0, maxFiles).map((f) => `  ${f}`));
      if (files.length > maxFiles) parts.push(`  \u2026+${files.length - maxFiles} more`);
    }
    return { output: parts.join("\n"), strategy: "ls:structured" };
  }
  if (level === "ultra") {
    return { output: `${lines.length} entries`, strategy: "ls:count:ultra" };
  }
  const maxLines = level === "aggressive" ? 20 : 40;
  if (lines.length <= maxLines) return { output: raw, strategy: "ls:short" };
  const shown = lines.slice(0, maxLines).join("\n");
  return { output: `${shown}
\u2026+${lines.length - maxLines} more entries`, strategy: "ls:truncated" };
}
function filterFind(raw, level) {
  const lines = raw.split("\n").filter((l) => l.trim());
  if (lines.length === 0) return { output: "no matches", strategy: "find:empty" };
  if (lines.length <= 10) return { output: raw, strategy: "find:short" };
  const byDir = /* @__PURE__ */ new Map();
  for (const line of lines) {
    const dir = line.includes("/") ? line.slice(0, line.lastIndexOf("/")) : ".";
    if (!byDir.has(dir)) byDir.set(dir, []);
    byDir.get(dir).push(line.slice(line.lastIndexOf("/") + 1));
  }
  if (level === "ultra") {
    const summary = [...byDir.entries()].sort((a, b) => b[1].length - a[1].length).slice(0, 5).map(([d, f]) => `${d}/ (${f.length})`).join(", ");
    return { output: `${lines.length} matches: ${summary}`, strategy: "find:ultra" };
  }
  if (level === "aggressive") {
    const grouped = [...byDir.entries()].sort((a, b) => b[1].length - a[1].length).slice(0, 10).map(([dir, files]) => `${dir}/ (${files.length} files)`).join("\n");
    const extra = byDir.size > 10 ? `
\u2026+${byDir.size - 10} more directories` : "";
    return { output: `${lines.length} matches in ${byDir.size} directories:
${grouped}${extra}`, strategy: "find:grouped" };
  }
  const parts = [`${lines.length} matches in ${byDir.size} directories:
`];
  let shown = 0;
  for (const [dir, files] of [...byDir.entries()].sort((a, b) => b[1].length - a[1].length)) {
    if (shown >= 15) break;
    parts.push(`${dir}/ (${files.length}):`);
    for (const f of files.slice(0, 3)) parts.push(`  ${f}`);
    if (files.length > 3) parts.push(`  \u2026+${files.length - 3} more`);
    shown++;
  }
  if (byDir.size > 15) parts.push(`
\u2026+${byDir.size - 15} more directories`);
  return { output: parts.join("\n"), strategy: "find:structured" };
}
function filterTree(raw, level) {
  const lines = raw.split("\n").filter((l) => l.trim());
  if (lines.length <= 20) return { output: raw, strategy: "tree:short" };
  const summaryLine = lines.find((l) => /\d+\s+director/.test(l));
  if (level === "ultra") {
    return { output: summaryLine || `${lines.length} lines`, strategy: "tree:ultra" };
  }
  const maxLines = level === "aggressive" ? 30 : 50;
  if (lines.length <= maxLines) return { output: raw, strategy: "tree:short" };
  const shown = lines.slice(0, maxLines).join("\n");
  const summary = summaryLine ? `
${summaryLine}` : `
\u2026+${lines.length - maxLines} more lines`;
  return { output: `${shown}${summary}`, strategy: "tree:truncated" };
}
function groupFilesByExt(files) {
  const byExt = /* @__PURE__ */ new Map();
  for (const f of files) {
    const ext = f.includes(".") ? f.slice(f.lastIndexOf(".")) : "(no ext)";
    byExt.set(ext, (byExt.get(ext) || 0) + 1);
  }
  return [...byExt.entries()].sort((a, b) => b[1] - a[1]).slice(0, 5).map(([ext, count]) => `${ext}: ${count}`).join(", ");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  filesFilter
});
//# sourceMappingURL=files.js.map
