/**
 * FileInfoDialog — shows classification tags, document type, validation results,
 * and debug JSON for a file in the thread form.
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useState } from 'react';
import { cn } from './shared.js';
import { HazoUiDialog, Tooltip, TooltipTrigger, TooltipContent, TooltipProvider } from 'hazo_ui';
/** Get a human-readable name for a validation rule */
function get_rule_display_name(r, index) {
    // Prefer rule_name if it's a real name (not a UUID)
    if (r.rule_name && !/^[0-9a-f]{8}-/i.test(r.rule_name))
        return r.rule_name;
    // Try rule_id if it's human-friendly
    if (r.rule_id && !/^[0-9a-f]{8}-/i.test(r.rule_id))
        return r.rule_id;
    return `Validation Rule ${index + 1}`;
}
export function FileInfoDialog({ open, on_open_change, file_name, classification, validation_results = [], }) {
    const [debug_expanded, set_debug_expanded] = useState(false);
    const [copied, set_copied] = useState(false);
    const has_classification = !!classification;
    const has_validations = validation_results.length > 0;
    const debug_payload = JSON.stringify({
        ...(has_classification ? { classification } : {}),
        ...(has_validations ? { validation_results } : {}),
    }, null, 2);
    const handle_copy_debug = async (e) => {
        e.stopPropagation();
        try {
            await navigator.clipboard.writeText(debug_payload);
            set_copied(true);
            setTimeout(() => set_copied(false), 1500);
        }
        catch {
            // Clipboard API unavailable (e.g., insecure context) — silently ignore.
        }
    };
    const issues_count = validation_results.filter(r => r.issues.length > 0).length;
    const passed_count = validation_results.filter(r => r.issues.length === 0).length;
    const document_type_label = classification
        ? classification.document_type.replace(/_/g, ' ').replace(/\b\w/g, c => c.toUpperCase())
        : 'Not classified yet';
    return (_jsx(HazoUiDialog, { open: open, onOpenChange: on_open_change, title: "File Details", description: file_name, footerContent: _jsx(_Fragment, {}), footerClassName: "!p-0 !border-0", children: _jsxs("div", { className: "space-y-5 py-2", children: [_jsxs("div", { className: "flex items-center gap-3 bg-[#EFF6FF] border border-[#BFDBFE] rounded-md px-4 py-3", children: [_jsxs("div", { className: "flex-1 min-w-0", children: [_jsx("div", { className: "text-[10px] font-semibold text-[#1D4ED8] uppercase tracking-wider mb-0.5", children: "Document Type" }), _jsx("div", { className: cn('text-[16px] font-semibold truncate', classification ? 'text-[#0F172A]' : 'text-[#94A3B8] italic'), children: document_type_label })] }), has_classification && (_jsxs("span", { className: cn('text-[11px] font-medium px-2 py-1 rounded flex-shrink-0', classification.confidence >= 0.8 ? 'bg-green-100 text-green-700' :
                                classification.confidence >= 0.5 ? 'bg-amber-100 text-amber-700' :
                                    'bg-red-100 text-red-700'), children: [Math.round(classification.confidence * 100), "% confidence"] }))] }), has_classification && (classification.document_date || classification.document_nature) && (_jsxs("div", { children: [_jsx("h4", { className: "text-[12px] font-semibold text-[#64748B] uppercase tracking-wider mb-2", children: "Classification Details" }), _jsxs("div", { className: "bg-[#F8FAFC] border border-[#E2E8F0] rounded-md p-3 space-y-2", children: [classification.document_date && (_jsxs("div", { className: "text-[12px] text-[#64748B]", children: [_jsx("span", { className: "font-medium", children: "Date:" }), " ", classification.document_date] })), classification.document_nature && (_jsxs("div", { className: "text-[12px] text-[#64748B]", children: [_jsx("span", { className: "font-medium", children: "Nature:" }), " ", classification.document_nature] }))] })] })), has_classification && classification.tags.length > 0 && (() => {
                    const reason_by_tag = new Map((classification.tag_reasons ?? []).map(tr => [tr.name, tr.reason]));
                    return (_jsxs("div", { children: [_jsx("h4", { className: "text-[12px] font-semibold text-[#64748B] uppercase tracking-wider mb-2", children: "Tags" }), _jsx(TooltipProvider, { delayDuration: 150, children: _jsx("div", { className: "flex flex-wrap gap-1.5", children: classification.tags.map((tag, i) => {
                                        const reason = reason_by_tag.get(tag);
                                        const label = tag.replace(/_/g, ' ');
                                        const pill = (_jsx("span", { className: cn('text-[11px] text-[#64748B] bg-[#F1F5F9] border border-[#E2E8F0] px-2 py-0.5 rounded-full', reason && 'cursor-help'), children: label }));
                                        if (!reason)
                                            return _jsx("span", { children: pill }, i);
                                        return (_jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: pill }), _jsx(TooltipContent, { side: "top", className: "max-w-xs text-[11px] leading-snug", children: reason })] }, i));
                                    }) }) })] }));
                })(), has_validations && (_jsxs("div", { children: [_jsxs("h4", { className: "text-[12px] font-semibold text-[#64748B] uppercase tracking-wider mb-2", children: ["Validation Results", issues_count > 0 && (_jsxs("span", { className: "ml-2 text-[10px] font-medium text-red-600 bg-red-50 px-1.5 py-0.5 rounded", children: [issues_count, " issue", issues_count !== 1 ? 's' : ''] })), passed_count > 0 && (_jsxs("span", { className: "ml-1 text-[10px] font-medium text-green-600 bg-green-50 px-1.5 py-0.5 rounded", children: [passed_count, " passed"] }))] }), _jsx("div", { className: "space-y-1.5", children: validation_results.map((r, i) => {
                                const rule_name = get_rule_display_name(r, i);
                                const has_issue = r.issues.length > 0;
                                const pass_detail = r.summary || 'Passed';
                                return (_jsxs("div", { className: cn('text-[12px] rounded border overflow-hidden', has_issue ? 'bg-red-50/50 border-red-200' : 'bg-green-50/50 border-green-200'), children: [_jsxs("div", { className: cn('flex items-center gap-2 px-3 py-1.5 border-b', has_issue ? 'border-red-200' : 'border-green-200'), children: [has_issue ? (_jsx("span", { className: "text-red-500 flex-shrink-0", children: "\u2715" })) : (_jsx("span", { className: "text-green-500 flex-shrink-0", children: "\u2713" })), _jsx("span", { className: "font-semibold text-[#0F172A]", children: rule_name }), r.issues.length > 1 && (_jsxs("span", { className: "text-[10px] text-[#94A3B8] bg-white border border-[#E2E8F0] px-1.5 py-px rounded", children: [r.issues.length, " issues"] })), r.confidence != null && (_jsxs("span", { className: "ml-auto text-[10px] text-[#94A3B8]", children: ["Confidence: ", Math.round(r.confidence * 100), "%"] }))] }), _jsx("div", { className: cn(r.issues.length > 1 && 'divide-y divide-red-200/40'), children: has_issue ? (r.issues.map(issue => (_jsx("div", { className: "px-3 py-2 text-[#64748B]", children: _jsx("p", { children: issue.issue_description }) }, issue.issue_id)))) : (_jsx("div", { className: "px-3 py-2", children: _jsx("p", { className: "text-[#94A3B8]", children: pass_detail }) })) })] }, i));
                            }) })] })), (has_classification || has_validations) && (_jsxs("div", { className: "border border-[#E2E8F0] rounded-md overflow-hidden", children: [_jsxs("div", { className: "w-full flex items-center gap-2 px-3 py-2 bg-[#F8FAFC] text-[11px] text-[#94A3B8] hover:bg-[#F1F5F9] transition-colors", children: [_jsxs("button", { type: "button", onClick: () => set_debug_expanded(p => !p), className: "flex items-center gap-2 flex-1 cursor-pointer text-left", children: [_jsx("svg", { className: cn('w-3 h-3 transition-transform', debug_expanded && 'rotate-90'), fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: _jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M9 5l7 7-7 7" }) }), _jsx("span", { className: "font-medium", children: "Debug JSON" })] }), _jsx("button", { type: "button", onClick: handle_copy_debug, className: cn('inline-flex items-center gap-1 px-2 py-0.5 rounded border border-[#E2E8F0] bg-white hover:bg-[#F1F5F9] transition-colors cursor-pointer', copied ? 'text-[#059669] border-[#059669]/30' : 'text-[#64748B]'), title: "Copy JSON to clipboard", children: copied ? (_jsxs(_Fragment, { children: [_jsx("svg", { className: "w-3 h-3", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2.4, strokeLinecap: "round", strokeLinejoin: "round", children: _jsx("polyline", { points: "20 6 9 17 4 12" }) }), _jsx("span", { className: "font-medium", children: "Copied" })] })) : (_jsxs(_Fragment, { children: [_jsxs("svg", { className: "w-3 h-3", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round", children: [_jsx("rect", { x: "9", y: "9", width: "13", height: "13", rx: "2", ry: "2" }), _jsx("path", { d: "M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" })] }), _jsx("span", { className: "font-medium", children: "Copy" })] })) })] }), debug_expanded && (_jsxs("div", { className: "bg-[#1E293B] text-[#E2E8F0] overflow-auto max-h-[300px]", children: [has_classification && (_jsxs("div", { className: "px-3 py-2 border-b border-[#334155]", children: [_jsx("div", { className: "text-[10px] font-semibold text-[#94A3B8] uppercase tracking-wider mb-1", children: "Classification Result" }), _jsx("pre", { className: "text-[10px] font-mono whitespace-pre-wrap", children: JSON.stringify(classification, null, 2) })] })), has_validations && (_jsxs("div", { className: "px-3 py-2", children: [_jsx("div", { className: "text-[10px] font-semibold text-[#94A3B8] uppercase tracking-wider mb-1", children: "Validation Results" }), _jsx("pre", { className: "text-[10px] font-mono whitespace-pre-wrap", children: JSON.stringify(validation_results, null, 2) })] }))] }))] }))] }) }));
}
//# sourceMappingURL=file_info_dialog.js.map