import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { memo, useMemo } from "react";
import { ClipboardList, Paperclip } from "lucide-react";
import { cn } from "../../../../lib/utils";
import { parseQuestionnaireRecap, } from "../agentQuestionnaireRecap";
function formatFileSize(bytes) {
    if (typeof bytes !== "number" || !Number.isFinite(bytes) || bytes <= 0) {
        return "";
    }
    if (bytes < 1024)
        return `${bytes} B`;
    const units = ["KB", "MB", "GB"];
    let value = bytes / 1024;
    let unitIndex = 0;
    while (value >= 1024 && unitIndex < units.length - 1) {
        value /= 1024;
        unitIndex += 1;
    }
    return `${value >= 10 ? value.toFixed(0) : value.toFixed(1)} ${units[unitIndex]}`;
}
const answerBadgeClassName = "rounded-badge bg-neutral-grey-5 badge-pad-sm text-2xs text-neutral-grey-100 inline-flex max-w-full items-center font-medium";
function QuestionnaireRecapAnswer({ row }) {
    if (!row.answered) {
        return (_jsx("span", { "data-testid": "questionnaire-recap-skipped", className: "text-neutral-grey-50 text-xs italic", children: "Skipped" }));
    }
    return (_jsxs("div", { className: "flex min-w-0 flex-col gap-1", children: [row.selectedOptions.length > 0 && (_jsx("div", { className: "flex flex-wrap gap-1", children: row.selectedOptions.map((option) => (_jsx("span", { "data-testid": "questionnaire-recap-option", className: answerBadgeClassName, children: _jsx("span", { className: "truncate", children: option.label }) }, option.id))) })), row.selectedItems.length > 0 && (_jsx("div", { className: "flex flex-wrap gap-1", children: row.selectedItems.map((item, itemIndex) => (_jsxs("span", { "data-testid": "questionnaire-recap-item", title: item.path, className: answerBadgeClassName, children: [_jsx("span", { className: "truncate", children: item.name || item.path || item.id }), item.includeSubitems && (_jsx("span", { className: "text-neutral-grey-50 ml-1 shrink-0", children: "+ subitems" }))] }, item.id ?? `${row.id}-item-${itemIndex}`))) })), row.uploadedDocuments.length > 0 && (_jsx("div", { className: "flex flex-col gap-0.5", children: row.uploadedDocuments.map((document, documentIndex) => {
                    const size = formatFileSize(document.fileSize);
                    return (_jsxs("span", { "data-testid": "questionnaire-recap-document", className: "text-neutral-grey-100 inline-flex min-w-0 items-center gap-1 text-xs", children: [_jsx(Paperclip, { strokeWidth: 1, size: 12, className: "text-neutral-grey-50 shrink-0" }), _jsx("span", { className: "truncate", children: document.fileName }), size && (_jsxs("span", { className: "text-neutral-grey-50 shrink-0 text-[10px]", children: ["(", size, ")"] }))] }, `${row.id}-document-${documentIndex}`));
                }) })), row.freetext && (_jsx("div", { "data-testid": "questionnaire-recap-freetext", className: "border-border-default text-neutral-grey-100 border-l-2 pl-2 text-xs wrap-break-word whitespace-pre-wrap", children: row.freetext }))] }));
}
/**
 * Read-only recap of a completed (or cancelled) ask-questionnaire tool call,
 * rendered inline in the chat transcript. Derives entirely from the tool
 * call's persisted arguments/result, so it survives reloads.
 */
export const QuestionnaireRecap = memo(function QuestionnaireRecap({ toolCall, }) {
    const recap = useMemo(() => parseQuestionnaireRecap({
        functionName: toolCall.function?.name,
        functionArguments: toolCall.function?.arguments,
        functionResult: toolCall.function?.result,
        functionError: toolCall.function?.error,
    }), [
        toolCall.function?.name,
        toolCall.function?.arguments,
        toolCall.function?.result,
        toolCall.function?.error,
    ]);
    if (!recap) {
        return null;
    }
    const isCancelled = recap.status === "cancelled";
    return (_jsxs("div", { "data-testid": "questionnaire-recap", "data-recap-status": recap.status, className: "border-border-default my-2 overflow-hidden rounded-lg border bg-white", children: [_jsxs("div", { className: "border-border-default flex items-center justify-between gap-2 border-b px-3 py-2", children: [_jsxs("div", { className: "flex min-w-0 items-center gap-2", children: [_jsx(ClipboardList, { strokeWidth: 1, size: 16, className: "text-neutral-grey-50 shrink-0" }), _jsxs("div", { className: "min-w-0", children: [_jsx("div", { "data-testid": "questionnaire-recap-title", className: "text-neutral-grey-100 truncate text-xs font-medium", children: recap.title }), recap.description && (_jsx("div", { className: "text-neutral-grey-50 truncate text-[11px]", children: recap.description }))] })] }), _jsx("span", { className: cn("rounded-badge badge-pad-sm text-2xs inline-flex shrink-0 items-center font-medium", isCancelled
                            ? "bg-neutral-grey-5 text-neutral-grey-50"
                            : "bg-feedback-green-light text-feedback-green"), children: isCancelled ? "Cancelled" : "Completed" })] }), isCancelled && (_jsx("div", { className: "text-neutral-grey-50 px-3 pt-2 text-[11px] italic", children: "Cancelled before submission" })), _jsx("div", { className: cn("flex flex-col gap-3 px-3 py-2.5", isCancelled && "opacity-60"), children: recap.rows.map((row, rowIndex) => (_jsxs("div", { "data-testid": "questionnaire-recap-question", "data-question-id": row.id, className: "flex gap-2", children: [_jsx("span", { className: "bg-neutral-grey-5 text-neutral-grey-50 mt-px flex h-4 w-4 shrink-0 items-center justify-center rounded-full text-[10px] font-medium", children: rowIndex + 1 }), _jsxs("div", { className: "flex min-w-0 flex-1 flex-col gap-1", children: [_jsx("div", { className: "text-neutral-grey-100 text-xs font-medium", children: row.prompt }), !isCancelled && _jsx(QuestionnaireRecapAnswer, { row: row })] })] }, row.id))) })] }));
});
//# sourceMappingURL=QuestionnaireRecap.js.map