import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text, useInput } from 'ink';
import { PendingApprovalService, } from '../services/approvals/pending-approval-service.js';
export function ApprovalPanel({ approval, resolving, onResolve }) {
    const choices = PendingApprovalService.resolveAvailableChoices(approval);
    const detail = PendingApprovalService.resolveInputDetail(approval.input);
    const payload = detail ? undefined : PendingApprovalService.formatPayload(approval.input);
    useInput((input) => {
        if (resolving) {
            return;
        }
        const choice = resolveChoiceKey(input, choices);
        if (!choice) {
            return;
        }
        onResolve(PendingApprovalService.resolveDecision(choice, approval));
    }, { isActive: !resolving });
    return (_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: "yellow", paddingX: 1, marginY: 1, children: [_jsx(Text, { color: "yellow", bold: true, children: "Approval requested" }), _jsxs(Text, { children: [_jsx(Text, { dimColor: true, children: "tool " }), approval.tool] }), _jsxs(Text, { children: [_jsx(Text, { dimColor: true, children: "summary " }), approval.summary] }), detail ? (_jsxs(Text, { children: [_jsxs(Text, { dimColor: true, children: [detail.label, " "] }), detail.value] })) : null, approval.reason ? (_jsxs(Text, { children: [_jsx(Text, { dimColor: true, children: "reason " }), approval.reason] })) : null, approval.editPreview ? (_jsxs(Text, { children: [_jsx(Text, { dimColor: true, children: "edit " }), approval.editPreview.action, ": ", approval.editPreview.path, approval.editPreview.truncated ? ' (truncated)' : ''] })) : null, payload ? (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { dimColor: true, children: "payload" }), payload.split(/\r?\n/).map((line, index) => (_jsx(Text, { children: line || ' ' }, `approval-payload-${index}`)))] })) : null, _jsx(Text, { color: resolving ? 'gray' : 'cyan', children: formatChoiceHint(choices) })] }));
}
function resolveChoiceKey(input, choices) {
    if (input === 'a') {
        return 'approve';
    }
    if (input === 'r' && choices.includes('trust_repo')) {
        return 'trust_repo';
    }
    if (input === 'r' && choices.includes('allow_project')) {
        return 'allow_project';
    }
    if (input === 'm' && choices.includes('allow_project')) {
        return 'allow_project';
    }
    if (input === 'd') {
        return 'deny';
    }
    return undefined;
}
function formatChoiceHint(choices) {
    const trustRepo = choices.includes('trust_repo');
    const allowProject = choices.includes('allow_project');
    if (trustRepo && allowProject) {
        return '[a] approve once  [r] trust this repo  [m] remember request  [d] deny';
    }
    if (trustRepo) {
        return '[a] approve once  [r] trust this repo  [d] deny';
    }
    return allowProject
        ? '[a] approve once  [r] remember for project  [d] deny'
        : '[a] approve once  [d] deny';
}
//# sourceMappingURL=ApprovalPanel.js.map