import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { Byline, joinBylineItems } from "./Byline.js";
import { KeyboardShortcutHint } from "./KeyboardShortcutHint.js";
import { Pane } from "./Pane.js";
import { StatusBadge } from "./StatusBadge.js";
import { truncateCells } from "./textLayout.js";
import { toneColor } from "./terminalTheme.js";
import { useTerminalSize } from "../../hooks/useTerminalSize.js";
export function formatDialogGuide(items) {
    return joinBylineItems(items.map((item) => `${item.shortcut} to ${item.action}`));
}
export function clampDialogWidth(width, terminalColumns) {
    const available = Math.max(40, terminalColumns - 4);
    const requested = width ?? Math.min(88, available);
    return Math.max(40, Math.min(requested, available));
}
export function Dialog(props) {
    const { columns } = useTerminalSize();
    const width = clampDialogWidth(props.width, columns);
    const content = (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { flexDirection: "row", justifyContent: "space-between", children: [_jsxs(Box, { flexDirection: "column", children: [renderTitle(props.title, props.tone), props.subtitle ? renderSubtitle(props.subtitle, width) : null] }), props.statusLabel || props.meta ? (_jsxs(Box, { flexDirection: "column", alignItems: "flex-end", children: [props.statusLabel ? (_jsx(StatusBadge, { label: props.statusLabel, tone: props.statusTone ?? props.tone })) : null, props.meta ? _jsx(Text, { color: "gray", children: truncateCells(props.meta, Math.max(12, width - 34)) }) : null] })) : null] }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: props.children }), props.hideInputGuide ? null : renderInputGuide(props)] }));
    if (props.hideBorder) {
        return content;
    }
    return (_jsx(Pane, { width: width, title: props.paneTitle ?? "dialog", tone: props.tone ?? "brand", paddingX: 1, children: content }));
}
function renderTitle(title, tone) {
    if (typeof title === "string" || typeof title === "number") {
        return _jsx(Text, { bold: true, color: toneColor(tone ?? "brand"), children: title });
    }
    return title;
}
function renderSubtitle(subtitle, width) {
    if (typeof subtitle === "string" || typeof subtitle === "number") {
        return _jsx(Text, { color: "gray", children: truncateCells(String(subtitle), Math.max(24, width - 12)) });
    }
    return subtitle;
}
function renderInputGuide(props) {
    if (props.inputGuide) {
        return (_jsx(Box, { marginTop: 1, children: typeof props.inputGuide === "string" || typeof props.inputGuide === "number"
                ? _jsx(Text, { color: "gray", children: props.inputGuide })
                : props.inputGuide }));
    }
    if (!props.guideItems || props.guideItems.length === 0) {
        return null;
    }
    return (_jsx(Box, { marginTop: 1, children: _jsx(Byline, { children: props.guideItems.map((item) => (_jsx(KeyboardShortcutHint, { shortcut: item.shortcut, action: item.action }, `${item.shortcut}:${item.action}`))) }) }));
}
