/**
 * terminal-links — OSC 8 clickable hyperlink support for terminal UIs.
 *
 * Modern terminals (iTerm2, WezTerm, Windows Terminal, VS Code integrated
 * terminal, Hyper, Tabby, Ghostty, most xterm-compatible) support the OSC 8
 * escape sequence for clickable hyperlinks:
 *
 *   \x1b]8;;URL\x1b\\displayed text\x1b]8;;\x1b\\
 *
 * Terminals that don't support OSC 8 simply ignore the escape and show the
 * display text, so this is safe to use unconditionally.
 */
// ---------------------------------------------------------------------------
// Hyperlink wrapper
// ---------------------------------------------------------------------------
/**
 * Wrap text in an OSC 8 clickable hyperlink escape sequence.
 *
 * If `text` is omitted, the URL itself is used as display text.
 * The escape sequence is ignored by terminals that don't support it,
 * so the display text always shows through.
 */
export function hyperlink(url, text) {
    const displayText = text || url;
    // OSC 8 hyperlink: \x1b]8;;URL\x1b\\text\x1b]8;;\x1b\\
    return `\x1b]8;;${url}\x1b\\${displayText}\x1b]8;;\x1b\\`;
}
// ---------------------------------------------------------------------------
// Detection
// ---------------------------------------------------------------------------
/** Known terminals that support OSC 8 hyperlinks. */
const SUPPORTED_TERMINALS = [
    'iterm.app',
    'wezterm',
    'vscode',
    'hyper',
    'tabby',
    'windows terminal',
    'ghostty',
    'alacritty',
    'kitty',
    'contour',
    'rio',
];
/**
 * Check if the current terminal likely supports OSC 8 hyperlinks.
 *
 * This is a best-effort heuristic. OSC 8 is safe to emit even in
 * unsupported terminals (they just ignore the escape), so this is
 * mainly useful for deciding whether to show a "(click to open)" hint.
 */
export function supportsHyperlinks() {
    const termProgram = (process.env.TERM_PROGRAM || '').toLowerCase();
    const wtSession = process.env.WT_SESSION; // Windows Terminal sets this
    if (wtSession)
        return true;
    if (SUPPORTED_TERMINALS.some((t) => termProgram.includes(t)))
        return true;
    if (process.env.TERM?.includes('xterm'))
        return true;
    // Ghostty sets TERM_PROGRAM=ghostty
    if (process.env.GHOSTTY_RESOURCES_DIR)
        return true;
    return false;
}
// ---------------------------------------------------------------------------
// URL detection in plain text
// ---------------------------------------------------------------------------
/**
 * Regex that matches bare URLs in text (http/https).
 * Captures the full URL including path, query, and fragment.
 */
const URL_REGEX = /https?:\/\/[^\s<>\"')\]]+/g;
/**
 * Wrap all bare URLs found in `text` with OSC 8 hyperlinks.
 *
 * Non-URL text is left unchanged. This is useful for system messages
 * where URLs appear inline but aren't in markdown link syntax.
 */
export function linkifyUrls(text) {
    return text.replace(URL_REGEX, (url) => hyperlink(url));
}
//# sourceMappingURL=terminal-links.js.map