"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 exec_exports = {};
__export(exec_exports, {
  register: () => register
});
module.exports = __toCommonJS(exec_exports);
var import_child_process = require("child_process");
var import_compression = require("../../compression/index");
var import_tracker = require("../../compression/tracker");
var import_config = require("../../config");
var import_ui = require("../ui");
function register(program) {
  program.command("exec").description("Run a shell command and return token-optimized output").option("-l, --level <level>", "Compression level: normal, aggressive, ultra").option("-t, --timeout <seconds>", "Timeout in seconds", "60").option("--raw", "Also print the raw output for comparison").option("--json", "Output result as JSON").allowUnknownOption(true).allowExcessArguments(true).action(async (opts, cmd) => {
    const args = cmd.args;
    if (args.length === 0) {
      console.error("  Usage: kirograph exec <command...>");
      console.error("  Example: kirograph exec git status");
      process.exit(1);
    }
    const shellCommand = args.join(" ");
    const cwd = process.cwd();
    const timeout = (parseInt(opts.timeout || "60", 10) || 60) * 1e3;
    let defaultLevel = "normal";
    try {
      const config = await (0, import_config.loadConfig)(cwd);
      if (config.shellCompressionLevel && config.shellCompressionLevel !== "off") {
        defaultLevel = config.shellCompressionLevel;
      }
    } catch {
    }
    const level = opts.level || defaultLevel;
    const validLevels = ["normal", "aggressive", "ultra"];
    if (!validLevels.includes(level)) {
      console.error(`  Unknown level: ${level}. Choose from: normal, aggressive, ultra`);
      process.exit(1);
    }
    let rawOutput;
    let exitCode = 0;
    try {
      rawOutput = (0, import_child_process.execSync)(shellCommand, {
        cwd,
        timeout,
        encoding: "utf8",
        stdio: ["pipe", "pipe", "pipe"],
        maxBuffer: 10 * 1024 * 1024
      });
    } catch (err) {
      rawOutput = (err.stdout || "") + (err.stderr || "");
      exitCode = err.status ?? 1;
    }
    const result = (0, import_compression.compress)(shellCommand, rawOutput, { level, preserveErrors: exitCode !== 0 });
    const tracker = new import_tracker.TokenTracker(cwd);
    tracker.record(shellCommand, result.originalTokens, result.compressedTokens, result.strategy);
    if (opts.json) {
      console.log(JSON.stringify({
        command: shellCommand,
        exitCode,
        level,
        strategy: result.strategy,
        commandFamily: result.commandFamily,
        originalTokens: result.originalTokens,
        compressedTokens: result.compressedTokens,
        savings: result.savings,
        output: result.output
      }, null, 2));
      process.exit(exitCode);
      return;
    }
    if (opts.raw) {
      console.log(`${import_ui.dim}\u2500\u2500 raw (${result.originalTokens} tokens) \u2500\u2500${import_ui.reset}`);
      console.log(rawOutput);
      console.log(`${import_ui.dim}\u2500\u2500 compressed (${result.compressedTokens} tokens) \u2500\u2500${import_ui.reset}`);
    }
    if (exitCode !== 0) {
      process.stderr.write(result.output + "\n");
    } else {
      console.log(result.output);
    }
    if (result.savings > 5) {
      console.log(`
${import_ui.dim}[${import_ui.green}${result.savings}% saved${import_ui.reset}${import_ui.dim} | ${result.originalTokens}\u2192${result.compressedTokens} tokens | ${import_ui.violet}${result.strategy}${import_ui.reset}${import_ui.dim}]${import_ui.reset}`);
    }
    process.exit(exitCode);
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  register
});
//# sourceMappingURL=exec.js.map
