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];
}
class TerminalCompatibility {
  static instance;
  terminalInfo;
  constructor() {
    this.terminalInfo = this.detectTerminal();
  }
  static getInstance() {
    if (!this.instance) {
      this.instance = new TerminalCompatibility();
    }
    return this.instance;
  }
  detectTerminal() {
    const termEnv = process.env["TERM"] || "";
    const termProgram = process.env["TERM_PROGRAM"] || "";
    const isInsideTmux = !!process.env["TMUX"];
    const isGhostty = termEnv.includes("ghostty") || termProgram === "ghostty";
    const isIterm2 = termProgram === "iTerm.app";
    const isTerminalApp = termProgram === "Apple_Terminal";
    let type = "unknown";
    if (isGhostty) {
      type = "ghostty";
    } else if (isInsideTmux) {
      type = "tmux";
    } else if (isIterm2) {
      type = "iterm2";
    } else if (isTerminalApp) {
      type = "terminal";
    } else if (termEnv.includes("xterm")) {
      type = "xterm";
    }
    const supportsTrueColor = this.checkTrueColorSupport();
    const supportsUnicode = this.checkUnicodeSupport();
    const recommendedConfig = this.getRecommendedConfig(type, isInsideTmux);
    return {
      type,
      isInsideTmux,
      supportsTrueColor,
      supportsUnicode,
      termEnv,
      recommendedConfig
    };
  }
  checkTrueColorSupport() {
    const colorterm = process.env["COLORTERM"];
    if (colorterm === "truecolor" || colorterm === "24bit") {
      return true;
    }
    const termProgram = process.env["TERM_PROGRAM"] || "";
    if (["iTerm.app", "Hyper", "vscode"].includes(termProgram)) {
      return true;
    }
    return false;
  }
  checkUnicodeSupport() {
    const lang = process.env["LANG"] || "";
    const lcAll = process.env["LC_ALL"] || "";
    const lcCtype = process.env["LC_CTYPE"] || "";
    const hasUtf8 = [lang, lcAll, lcCtype].some(
      (v) => v.toLowerCase().includes("utf-8") || v.toLowerCase().includes("utf8")
    );
    return hasUtf8;
  }
  getRecommendedConfig(type, isInsideTmux) {
    const baseConfig = {
      smartCSR: true,
      fullUnicode: true,
      forceUnicode: null,
      dockBorders: true,
      terminal: "xterm-256color",
      title: "StackMemory TUI Dashboard",
      warnings: false
    };
    switch (type) {
      case "ghostty":
        return {
          ...baseConfig,
          smartCSR: false,
          // Disable smart cursor restore
          terminal: "xterm",
          // Use basic xterm instead of xterm-256color
          dockBorders: false,
          // Can cause rendering issues
          artificalCursor: true,
          // Use artificial cursor
          grabKeys: false,
          // Don't grab all keys
          sendFocus: false
          // Don't send focus events
        };
      case "tmux":
        return {
          ...baseConfig,
          terminal: isInsideTmux ? "screen-256color" : "xterm-256color",
          smartCSR: !isInsideTmux,
          // Disable inside tmux
          sendFocus: false
        };
      case "iterm2":
        return {
          ...baseConfig,
          terminal: "xterm-256color",
          fullUnicode: true,
          forceUnicode: true
        };
      case "terminal":
        return {
          ...baseConfig,
          terminal: "xterm-256color",
          fullUnicode: true
        };
      case "xterm":
      default:
        return {
          ...baseConfig,
          smartCSR: false,
          fullUnicode: false,
          forceUnicode: false
        };
    }
  }
  getTerminalInfo() {
    return this.terminalInfo;
  }
  getBlessedConfig() {
    return this.terminalInfo.recommendedConfig;
  }
  /**
   * Set optimal TERM environment variable for the detected terminal
   */
  configureEnvironment() {
    const { type, isInsideTmux } = this.terminalInfo;
    if (type === "ghostty") {
      process.env["TERM"] = "xterm";
    } else if (isInsideTmux) {
      process.env["TERM"] = "screen-256color";
    } else if (!process.env["TERM"] || process.env["TERM"] === "dumb") {
      process.env["TERM"] = "xterm-256color";
    }
    process.env["NODE_NO_WARNINGS"] = "1";
  }
  /**
   * Check if terminal is suitable for TUI
   */
  isCompatible() {
    if (process.env["CI"] || process.env["CONTINUOUS_INTEGRATION"]) {
      return false;
    }
    const term = process.env["TERM"] || "";
    if (term === "dumb") {
      return false;
    }
    if (process.env["FORCE_TUI"]) {
      return true;
    }
    const hasAnyTTY = process.stdout.isTTY || process.stdin.isTTY || process.stderr.isTTY;
    if (!hasAnyTTY) {
      const { type } = this.terminalInfo;
      if (type !== "unknown" || term.includes("xterm") || term.includes("screen")) {
        return true;
      }
      return false;
    }
    return true;
  }
  /**
   * Get compatibility warnings for the current terminal
   */
  getWarnings() {
    const warnings = [];
    const { type, supportsUnicode, supportsTrueColor } = this.terminalInfo;
    if (type === "ghostty") {
      warnings.push(
        "Ghostty terminal detected: Using compatibility mode with reduced features"
      );
    }
    if (!supportsUnicode) {
      warnings.push(
        "Unicode support not detected: Some icons may not display correctly"
      );
    }
    if (!supportsTrueColor) {
      warnings.push("True color support not detected: Using 256 color mode");
    }
    if (type === "unknown") {
      warnings.push("Unknown terminal type: Using conservative settings");
    }
    return warnings;
  }
  /**
   * Get terminal capabilities as a string for debugging
   */
  getCapabilitiesString() {
    const { type, isInsideTmux, supportsTrueColor, supportsUnicode, termEnv } = this.terminalInfo;
    return [
      `Terminal Type: ${type}`,
      `TERM: ${termEnv}`,
      `Inside tmux: ${isInsideTmux}`,
      `True Color: ${supportsTrueColor}`,
      `Unicode: ${supportsUnicode}`
    ].join(" | ");
  }
}
const terminalCompat = TerminalCompatibility.getInstance();
export {
  TerminalCompatibility,
  terminalCompat
};
//# sourceMappingURL=terminal-compat.js.map
