import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { Box, Text } from '../../ink.js';
import { stringWidth } from '../../ink/stringWidth.js';
import { themeToInkHex } from '../ink-theme.js';
const ACCENT_BAR = '\u258E';
const ELLIPSIS = '\u2026';
const MIN_COLUMNS = 24;
const KIND_LABELS = {
    read: 'READ',
    write: 'WRITE',
    exec: 'EXEC',
    web: 'WEB',
    danger: 'DANGEROUS',
};
function clampColumns(columns) {
    const fallback = process.stdout.columns || 80;
    return Math.max(MIN_COLUMNS, Math.floor(columns ?? fallback));
}
function takeWidth(text, maxWidth) {
    if (maxWidth <= 0)
        return '';
    if (stringWidth(text) <= maxWidth)
        return text;
    const ellipsisWidth = stringWidth(ELLIPSIS);
    if (maxWidth <= ellipsisWidth)
        return ELLIPSIS.slice(0, maxWidth);
    let out = '';
    let width = 0;
    for (const char of Array.from(text)) {
        const charWidth = stringWidth(char);
        if (width + charWidth > maxWidth - ellipsisWidth)
            break;
        out += char;
        width += charWidth;
    }
    return out + ELLIPSIS;
}
function takeEndWidth(text, maxWidth) {
    if (maxWidth <= 0)
        return '';
    if (stringWidth(text) <= maxWidth)
        return text;
    const ellipsisWidth = stringWidth(ELLIPSIS);
    if (maxWidth <= ellipsisWidth)
        return ELLIPSIS.slice(0, maxWidth);
    let out = '';
    let width = 0;
    for (const char of Array.from(text).reverse()) {
        const charWidth = stringWidth(char);
        if (width + charWidth > maxWidth - ellipsisWidth)
            break;
        out = char + out;
        width += charWidth;
    }
    return ELLIPSIS + out;
}
function truncateMiddle(text, maxWidth) {
    if (maxWidth <= 0)
        return '';
    if (stringWidth(text) <= maxWidth)
        return text;
    const ellipsisWidth = stringWidth(ELLIPSIS);
    if (maxWidth <= ellipsisWidth)
        return ELLIPSIS.slice(0, maxWidth);
    const budget = maxWidth - ellipsisWidth;
    const headWidth = Math.ceil(budget / 2);
    const tailWidth = Math.floor(budget / 2);
    return `${takeWidth(text, headWidth).replace(ELLIPSIS, '')}${ELLIPSIS}${takeEndWidth(text, tailWidth).replace(ELLIPSIS, '')}`;
}
function kindColorToken(kind) {
    if (kind === 'danger')
        return 'error';
    if (kind === 'write' || kind === 'exec')
        return 'warning';
    if (kind === 'web')
        return 'info';
    return 'owl';
}
export function PermissionCard({ kind, action, target, risk, choices, selectedIndex = 0, columns, }) {
    const width = clampColumns(columns);
    const textWidth = Math.max(1, width - 2);
    const tierToken = kindColorToken(kind);
    const accentColor = themeToInkHex(tierToken);
    const ink = themeToInkHex('text');
    const inkHi = themeToInkHex('textHi');
    const dim = themeToInkHex('textDim');
    const mute = themeToInkHex('textMute');
    const hairFaint = themeToInkHex('hairFaint');
    const bgInput = themeToInkHex('bgInput');
    const accentSoft = themeToInkHex('accentSoft');
    const errSoft = themeToInkHex('errSoft');
    // Card background: danger tier uses err-soft per the design's
    // `.oc-perm.is-danger { background: linear-gradient(...err-soft...); }`.
    // Other tiers stay transparent so they sit on the composer surface.
    const cardBg = kind === 'danger' ? errSoft : undefined;
    const tag = KIND_LABELS[kind];
    return (_jsxs(Box, { flexDirection: "column", width: width, flexShrink: 0, backgroundColor: cardBg, children: [_jsxs(Box, { width: width, children: [_jsxs(Text, { color: accentColor, children: [ACCENT_BAR, " "] }), _jsx(Text, { color: accentColor, bold: true, children: tag }), _jsx(Text, { color: dim, children: "  requires approval" })] }), _jsxs(Box, { width: width, children: [_jsxs(Text, { color: accentColor, children: [ACCENT_BAR, " "] }), _jsx(Text, { color: inkHi, wrap: "truncate-end", children: takeWidth(action, textWidth) })] }), target ? (_jsxs(Box, { width: width, children: [_jsxs(Text, { color: accentColor, children: [ACCENT_BAR, " "] }), _jsx(Box, { backgroundColor: bgInput, flexGrow: 1, paddingX: 1, children: _jsx(Text, { color: ink, wrap: "truncate-end", children: takeWidth(target, textWidth - 2) }) })] })) : null, risk ? (_jsxs(Box, { width: width, children: [_jsxs(Text, { color: accentColor, children: [ACCENT_BAR, " "] }), _jsx(Text, { color: kind === 'danger' ? accentColor : themeToInkHex('warning'), children: "\u26A0 " }), _jsx(Text, { color: mute, wrap: "truncate-end", children: takeWidth(risk, Math.max(1, textWidth - 3)) })] })) : null, _jsxs(Box, { width: width, marginTop: 1, children: [_jsxs(Text, { color: accentColor, children: [ACCENT_BAR, " "] }), _jsx(Box, { flexGrow: 1, flexDirection: "row", flexWrap: "wrap", children: choices.map((choice, i) => {
                            const isSelected = i === selectedIndex;
                            const isPrimary = !!choice.primary;
                            const isDanger = !!choice.danger;
                            const chipColor = isDanger
                                ? themeToInkHex('error')
                                : isPrimary
                                    ? accentColor
                                    : ink;
                            const chipBg = isSelected
                                ? (isDanger ? errSoft : accentSoft)
                                : undefined;
                            const keyColor = isDanger ? themeToInkHex('error') : accentColor;
                            return (_jsxs(Box, { marginRight: 1, backgroundColor: chipBg, children: [_jsx(Text, { color: hairFaint, children: "[" }), _jsx(Text, { color: keyColor, bold: isPrimary, children: choice.key.toUpperCase() }), _jsx(Text, { color: hairFaint, children: "]" }), _jsxs(Text, { color: chipColor, bold: isSelected, children: [" ", choice.label] })] }, `${choice.key}-${i}`));
                        }) })] }), _jsxs(Box, { width: width, children: [_jsxs(Text, { color: accentColor, children: [ACCENT_BAR, " "] }), _jsx(Text, { color: mute, children: "\u2191/\u2193 move \u00B7 \u21B5 confirm \u00B7 1/2/3 quick \u00B7 esc deny" })] })] }));
}
// `truncateMiddle` is exported for external callers that may format
// long target paths before passing them in.  Keep it referenced so the
// linter doesn't flag it after the layout change above.
void truncateMiddle;
//# sourceMappingURL=permission-card.js.map