import { color } from "../components/color.js";
import { symbols } from "../components/symbols.js";
import { resolveOutputFormat } from "../internal/output-format.js";
import { getTheme } from "../internal/theme-detect.js";
function renderMarkdownInline(value) {
    return value.replaceAll("\r\n", " ").replaceAll("\n", " ").replaceAll("\r", " ");
}
export function renderMenu(opts) {
    const format = resolveOutputFormat();
    const selectedIndex = opts.selectedIndex ?? 0;
    if (!Number.isInteger(selectedIndex) || !Number.isFinite(selectedIndex)) {
        throw new Error("selectedIndex must be a finite integer.");
    }
    if (format === "markdown") {
        return [
            `**${renderMarkdownInline(opts.message)}**`,
            ...opts.options.map((option, index) => `- [${index === selectedIndex ? "x" : " "}] ${renderMarkdownInline(option.label)}`)
        ].join("\n");
    }
    if (format === "json") {
        return JSON.stringify({
            type: "menu",
            message: opts.message,
            options: opts.options,
            selected: selectedIndex
        });
    }
    const theme = getTheme();
    const bar = color.gray(symbols.bar);
    const lines = [];
    lines.push(`${color.cyan(symbols.active)}  ${opts.message}`);
    lines.push(bar);
    opts.options.forEach((option, index) => {
        const isSelected = index === selectedIndex;
        const prefix = isSelected ? color.cyan(symbols.active) : color.gray(symbols.inactive);
        const label = isSelected ? theme.accent(option.label) : option.label;
        const hint = option.hint ? color.dim(` (${option.hint})`) : "";
        lines.push(`${bar}  ${prefix} ${label}${hint}`);
    });
    lines.push(`${bar}`);
    return lines.join("\n");
}
