"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 compression_exports = {};
__export(compression_exports, {
  compress: () => compress,
  detectCommandFamily: () => detectCommandFamily,
  estimateTokens: () => estimateTokens
});
module.exports = __toCommonJS(compression_exports);
var import_git = require("./filters/git");
var import_test = require("./filters/test");
var import_lint = require("./filters/lint");
var import_files = require("./filters/files");
var import_docker = require("./filters/docker");
var import_package = require("./filters/package");
var import_aws = require("./filters/aws");
var import_github = require("./filters/github");
var import_misc = require("./filters/misc");
var import_generic = require("./filters/generic");
const FILTERS = [
  import_git.gitFilter,
  import_test.testFilter,
  import_lint.lintFilter,
  import_misc.miscFilter,
  // grep, diff, curl, wget, playwright, prisma
  import_files.filesFilter,
  import_docker.dockerFilter,
  import_package.packageFilter,
  import_aws.awsFilter,
  import_github.githubFilter,
  import_generic.genericFilter
  // Always last — catches everything
];
function estimateTokens(text) {
  if (!text) return 0;
  return Math.ceil(text.length / 4);
}
const DEFAULT_OPTIONS = {
  level: "normal",
  preserveErrors: true
};
function compress(command, rawOutput, opts) {
  const options = { ...DEFAULT_OPTIONS, ...opts };
  if (!rawOutput || !rawOutput.trim()) {
    return {
      output: rawOutput || "",
      originalTokens: 0,
      compressedTokens: 0,
      savings: 0,
      strategy: "empty",
      commandFamily: "none"
    };
  }
  const originalTokens = estimateTokens(rawOutput);
  const filter = FILTERS.find((f) => f.matches(command));
  if (!filter) {
    return {
      output: rawOutput,
      originalTokens,
      compressedTokens: originalTokens,
      savings: 0,
      strategy: "none",
      commandFamily: "unknown"
    };
  }
  if (options.preserveErrors && isErrorOutput(rawOutput, command)) {
    const result2 = filter.filter(command, rawOutput, "normal");
    const compressedTokens2 = estimateTokens(result2.output);
    return {
      output: result2.output,
      originalTokens,
      compressedTokens: compressedTokens2,
      savings: Math.round((originalTokens - compressedTokens2) / originalTokens * 100),
      strategy: result2.strategy + ":error-preserved",
      commandFamily: filter.name
    };
  }
  const result = filter.filter(command, rawOutput, options.level);
  let output = result.output;
  if (options.maxOutputTokens && estimateTokens(output) > options.maxOutputTokens) {
    const maxChars = options.maxOutputTokens * 4;
    output = output.slice(0, maxChars) + "\n\u2026(truncated to token limit)";
  }
  const compressedTokens = estimateTokens(output);
  const savings = originalTokens > 0 ? Math.round((originalTokens - compressedTokens) / originalTokens * 100) : 0;
  return {
    output,
    originalTokens,
    compressedTokens,
    savings: Math.max(0, savings),
    // Never negative
    strategy: result.strategy,
    commandFamily: filter.name
  };
}
function detectCommandFamily(command) {
  for (const filter of FILTERS) {
    if (filter !== import_generic.genericFilter && filter.matches(command)) {
      return filter.name;
    }
  }
  return "other";
}
function isErrorOutput(output, _command) {
  const errorIndicators = [
    /^error/im,
    /\bfailed\b/i,
    /\bpanic\b/i,
    /\bfatal\b/i,
    /exit\s+code\s+[1-9]/i,
    /\bException\b/,
    /\bTraceback\b/,
    /\bsegfault\b/i,
    /\bSIGSEGV\b/,
    /\bSIGABRT\b/
  ];
  const head = output.slice(0, 500);
  const tail = output.slice(-500);
  const sample = head + tail;
  return errorIndicators.some((re) => re.test(sample));
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  compress,
  detectCommandFamily,
  estimateTokens
});
//# sourceMappingURL=index.js.map
