/**
 * terminal-images — inline image rendering for iTerm2/WezTerm/mintty/Kitty.
 *
 * Uses the iTerm2 inline image protocol:
 *   \x1b]1337;File=<params>:<base64>\x07
 *
 * Terminals that don't support this protocol will ignore the escape.
 * Falls back to showing [Image: path] for unsupported terminals.
 */
import { readFileSync } from 'fs';
import { basename, extname } from 'path';
/**
 * Render an image inline using the iTerm2 image protocol.
 *
 * Returns the escape sequence string that, when written to stdout,
 * displays the image inline in supported terminals.
 *
 * Falls back to `[Image: filename]` if the file cannot be read.
 */
export function inlineImage(filePath, opts) {
    try {
        const data = readFileSync(filePath);
        const base64 = data.toString('base64');
        const fileName = basename(filePath);
        const fileNameB64 = Buffer.from(fileName).toString('base64');
        const params = [
            `name=${fileNameB64}`,
            `size=${data.length}`,
            'inline=1',
            opts?.width != null ? `width=${opts.width}` : '',
            opts?.height != null ? `height=${opts.height}` : '',
            `preserveAspectRatio=${opts?.preserveAspectRatio !== false ? 1 : 0}`,
        ]
            .filter(Boolean)
            .join(';');
        // iTerm2 image protocol: \x1b]1337;File=<params>:<base64>\x07
        return `\x1b]1337;File=${params}:${base64}\x07`;
    }
    catch {
        return `[Image: ${filePath}]`;
    }
}
// ---------------------------------------------------------------------------
// Detection
// ---------------------------------------------------------------------------
/** Known terminals that support iTerm2 inline images. */
const IMAGE_TERMINALS = ['iterm.app', 'wezterm', 'mintty', 'kitty'];
/**
 * Check if the current terminal supports inline images.
 */
export function supportsInlineImages() {
    const termProgram = (process.env.TERM_PROGRAM || '').toLowerCase();
    return IMAGE_TERMINALS.some((t) => termProgram.includes(t));
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Image file extensions that can be rendered inline. */
const IMAGE_EXTENSIONS = new Set([
    '.png',
    '.jpg',
    '.jpeg',
    '.gif',
    '.bmp',
    '.webp',
    '.svg',
    '.ico',
    '.tiff',
    '.tif',
]);
/**
 * Check if a file path points to an image based on extension.
 */
export function isImageFile(filePath) {
    const ext = extname(filePath).toLowerCase();
    return IMAGE_EXTENSIONS.has(ext);
}
//# sourceMappingURL=terminal-images.js.map