#!/usr/bin/env node
import { spawn } from "child_process";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import chalk from "chalk";
function getEnv(key, defaultValue) {
  const value = process.env[key];
  if (value === void 0) {
    if (defaultValue !== void 0) return defaultValue;
    throw new Error(`Environment variable ${key} is required`);
  }
  return value;
}
function getOptionalEnv(key) {
  return process.env[key];
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const tuiCommand = {
  command: "tui",
  describe: "Launch interactive TUI monitoring dashboard",
  builder: (yargs) => {
    return yargs.option("server", {
      alias: "s",
      type: "boolean",
      description: "Start WebSocket server for real-time updates",
      default: false
    }).option("ws-url", {
      alias: "w",
      type: "string",
      description: "WebSocket server URL",
      default: "ws://localhost:8080"
    }).option("refresh", {
      alias: "r",
      type: "number",
      description: "Auto-refresh interval in milliseconds",
      default: 2e3
    });
  },
  handler: async (argv) => {
    console.log(chalk.cyan("\u{1F680} Launching StackMemory TUI Dashboard..."));
    process.env["STACKMEMORY_WS_URL"] = argv.wsUrl;
    const scriptPath = join(__dirname, "../../../scripts/start-tui.sh");
    const args = [];
    if (argv.server) {
      args.push("--with-server");
    }
    const tui = spawn("bash", [scriptPath, ...args], {
      stdio: "inherit",
      env: {
        ...process.env,
        STACKMEMORY_WS_URL: argv.wsUrl
      }
    });
    tui.on("error", (error) => {
      console.error(chalk.red("Failed to launch TUI:"), error);
      process.exit(1);
    });
    tui.on("exit", (code) => {
      if (code !== 0) {
        console.error(chalk.red(`TUI exited with code ${code}`));
        process.exit(code || 1);
      }
    });
  }
};
if (require.main === module) {
  tuiCommand.handler({
    server: process.argv.includes("--server"),
    wsUrl: process.env["STACKMEMORY_WS_URL"] || "ws://localhost:8080",
    refresh: 2e3
  });
}
export {
  tuiCommand
};
//# sourceMappingURL=tui.js.map
