/**
 * SendBackMessage — renders a send_back message in the timeline.
 * Shows pinned file reference, ACTION REQUIRED issues, then collapsible history.
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useRef } from 'react';
import { cn, PdfIcon, format_time } from './shared.js';
export function SendBackMessage({ data, resolved, resolution, resolving, on_view_file, source_keep_selected, on_keep_change, show_keep_checkbox, on_reply }) {
    const [history_expanded, set_history_expanded] = useState(false);
    const [replying_idx, set_replying_idx] = useState(null);
    const [reply_text, set_reply_text] = useState('');
    const reply_ref = useRef(null);
    // Normalize to array; head drives the pinned file row and history section.
    const data_list = Array.isArray(data) ? data : [data];
    const head = data_list[0];
    const handle_view = () => {
        if (head.source_file && on_view_file) {
            on_view_file({
                content_id: head.source_content_id,
                type: 'file',
                file: head.source_file,
            });
        }
    };
    return (_jsxs("div", { className: "space-y-3", children: [head.source_file && (_jsxs("div", { className: cn('flex items-center gap-2 px-3 py-2 rounded-md border', resolved ? 'bg-[#F8FAFC] border-[#E2E8F0]' : 'bg-[#FEF2F2] border-[#FECACA]'), children: [_jsx(PdfIcon, { className: "w-4 h-4" }), _jsx("span", { className: "text-[12px] text-[#1E293B] font-medium flex-1 truncate", children: head.source_file.file_name }), head.source_file.file_size && (_jsx("span", { className: "text-[10px] text-[#94A3B8]", children: head.source_file.file_size })), _jsxs("button", { type: "button", onClick: () => {
                            const target = document.querySelector(`[data-content-id="${head.source_content_id}"]`);
                            if (target) {
                                target.scrollIntoView({ behavior: 'smooth', block: 'center' });
                                target.classList.add('ring-2', 'ring-[#F59E0B]', 'ring-offset-2');
                                setTimeout(() => target.classList.remove('ring-2', 'ring-[#F59E0B]', 'ring-offset-2'), 1500);
                            }
                        }, className: cn('text-[9px] px-1.5 py-0.5 rounded font-medium flex items-center gap-1 cursor-pointer hover:opacity-80 transition-opacity', resolved ? 'text-[#64748B] bg-[#F1F5F9]' : 'text-[#DC2626] bg-[#FEE2E2]'), title: "Scroll to original file", children: [_jsx("svg", { className: "w-2.5 h-2.5", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: _jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13" }) }), "ref"] }), on_view_file && (_jsx("button", { onClick: handle_view, className: "text-[10px] text-[#2563EB] hover:underline cursor-pointer", children: "View" })), source_keep_selected !== undefined && show_keep_checkbox !== false && (_jsx("input", { type: "checkbox", checked: source_keep_selected, onChange: () => on_keep_change?.({ [head.source_content_id]: !source_keep_selected }), className: "ml-1 rounded border-[#CBD5E1] text-[#2563EB] focus:ring-[#2563EB] cursor-pointer flex-shrink-0", title: source_keep_selected ? 'Kept — uncheck to drop from collected data' : 'Mark as kept for collected data' }))] })), data_list.map((entry, idx) => (_jsxs("div", { className: "space-y-1.5", children: [_jsxs("div", { className: cn('rounded-md overflow-hidden border', resolved ? 'bg-[#F0FDF4] border-[#BBF7D0]' : 'bg-[#FFFBEB] border-[#FDE68A]'), children: [_jsxs("div", { className: cn('px-3 py-2 border-b flex items-center justify-between', resolved ? 'border-[#BBF7D0]' : 'border-[#FDE68A]'), children: [_jsx("span", { className: cn('text-[10px] font-bold tracking-wide', resolved ? 'text-[#059669]' : 'text-[#D97706]'), children: resolved ? 'RESOLVED' : 'ACTION REQUIRED' }), on_reply && !resolved && replying_idx !== idx && (_jsx("button", { type: "button", onClick: () => {
                                            set_replying_idx(idx);
                                            set_reply_text('');
                                            setTimeout(() => reply_ref.current?.focus(), 0);
                                        }, className: "text-[10px] text-[#64748B] hover:text-[#2563EB] transition-colors cursor-pointer", children: "Reply" }))] }), _jsx("div", { className: cn('divide-y', resolved ? 'divide-[#BBF7D0]/50' : 'divide-[#FDE68A]/50'), children: entry.issues.map((issue, i) => (_jsx(SendBackIssueCard, { issue: issue, resolved: resolved }, i))) })] }), replying_idx === idx && (_jsxs("div", { className: "border border-[#E2E8F0] rounded-md bg-white p-2.5 space-y-2", children: [_jsx("textarea", { ref: reply_ref, value: reply_text, onChange: e => set_reply_text(e.target.value), onKeyDown: e => {
                                    if (e.key === 'Enter' && !e.shiftKey) {
                                        e.preventDefault();
                                        const trimmed = reply_text.trim();
                                        if (!trimmed)
                                            return;
                                        on_reply?.(entry.source_content_id, entry.source_file?.file_name, entry.issues[0]?.rule_name, trimmed);
                                        set_replying_idx(null);
                                        set_reply_text('');
                                    }
                                    if (e.key === 'Escape') {
                                        set_replying_idx(null);
                                        set_reply_text('');
                                    }
                                }, rows: 2, placeholder: "Write your reply\u2026", className: "w-full text-[12px] border border-[#E2E8F0] rounded px-2.5 py-1.5 resize-none focus:outline-none focus:ring-1 focus:ring-[#2563EB] placeholder:text-[#CBD5E1]" }), _jsxs("div", { className: "flex justify-end gap-2", children: [_jsx("button", { type: "button", onClick: () => { set_replying_idx(null); set_reply_text(''); }, className: "text-[11px] text-[#94A3B8] cursor-pointer", children: "Cancel" }), _jsx("button", { type: "button", disabled: !reply_text.trim(), onClick: () => {
                                            const trimmed = reply_text.trim();
                                            if (!trimmed)
                                                return;
                                            on_reply?.(entry.source_content_id, entry.source_file?.file_name, entry.issues[0]?.rule_name, trimmed);
                                            set_replying_idx(null);
                                            set_reply_text('');
                                        }, className: cn('text-[11px] font-medium px-3 py-1 rounded cursor-pointer transition-colors', reply_text.trim() ? 'bg-[#2563EB] text-white hover:bg-[#1D4ED8]' : 'bg-[#F1F5F9] text-[#CBD5E1] cursor-not-allowed'), children: "Send" })] })] }))] }, idx))), (resolution || resolving) && (_jsx(ResolutionStatusPanel, { resolution: resolution ?? null, resolved: !!resolved, resolving: !!resolving })), head.history.length > 0 && (_jsxs("div", { className: "border border-[#E2E8F0] rounded-md overflow-hidden", children: [_jsxs("button", { onClick: () => set_history_expanded(p => !p), className: "w-full flex items-center gap-2 px-3 py-2 bg-[#F1F5F9] text-[11px] text-[#64748B] hover:bg-[#E2E8F0] transition-colors cursor-pointer", children: [_jsx("svg", { className: cn('w-3 h-3 transition-transform', history_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-semibold", children: "Previous History" }), _jsxs("span", { className: "text-[#94A3B8]", children: ["\u00B7 ", head.history.length, " entries"] })] }), history_expanded && (_jsx("div", { className: "px-3 py-2 bg-[#FAFBFC] space-y-1.5", children: head.history.map((hist_entry, i) => (_jsxs("div", { className: "flex gap-2 text-[11px]", children: [_jsx("div", { className: cn('w-[3px] rounded-sm flex-shrink-0', hist_entry.role === 'system' ? 'bg-[#EF4444]' :
                                        hist_entry.role === 'client' ? 'bg-[#2563EB]' :
                                            'bg-[#F59E0B]') }), _jsxs("div", { className: "min-w-0 flex-1", children: [_jsx("span", { className: "text-[#94A3B8]", children: hist_entry.action }), _jsx("span", { className: "text-[#1E293B] ml-1", children: hist_entry.message }), _jsxs("span", { className: "text-[#CBD5E1] ml-1", children: ["\u00B7 ", format_time(hist_entry.time)] })] })] }, i))) }))] }))] }));
}
// ============================================================================
// Per-issue card (with optional collapsible child issues)
// ============================================================================
function SendBackIssueCard({ issue, resolved }) {
    const [expanded, set_expanded] = useState(false);
    const has_children = !!(issue.child_issues && issue.child_issues.length > 0);
    return (_jsx("div", { className: "px-3 py-2.5", children: _jsxs("div", { className: "flex items-start gap-2", children: [_jsx("svg", { className: cn('w-3.5 h-3.5 flex-shrink-0 mt-0.5', resolved ? 'text-[#059669]' : 'text-[#D97706]'), fill: "currentColor", viewBox: "0 0 20 20", children: resolved ? (_jsx("path", { fillRule: "evenodd", d: "M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z", clipRule: "evenodd" })) : (_jsx("path", { fillRule: "evenodd", d: "M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z", clipRule: "evenodd" })) }), _jsxs("div", { className: "flex-1 min-w-0", children: [_jsx("div", { className: cn('text-[12px] leading-relaxed whitespace-pre-wrap', resolved ? 'text-[#065F46]' : 'text-[#92400E]'), children: issue.agent_feedback }), has_children && (_jsxs("button", { type: "button", onClick: () => set_expanded(e => !e), className: "mt-1.5 text-[10px] text-[#64748B] hover:text-[#1E293B] flex items-center gap-1 cursor-pointer", children: [_jsx("svg", { "aria-hidden": "true", className: cn('w-2.5 h-2.5 transition-transform', 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" }) }), expanded ? 'Hide' : 'Show', " ", issue.child_issues.length, " underlying issue", issue.child_issues.length !== 1 ? 's' : ''] })), has_children && expanded && (_jsx("ul", { className: "mt-1.5 pl-3 space-y-0.5 text-[11px] font-mono text-[#64748B] border-l border-[#E2E8F0]", children: issue.child_issues.map((child, idx) => (_jsxs("li", { children: [child.date && _jsx("span", { className: "text-[#94A3B8] mr-2", children: child.date }), child.issue_description] }, `${child.issue_id}-${idx}`))) }))] })] }) }));
}
// ============================================================================
// Resolution Status panel
// ============================================================================
function ResolutionStatusPanel({ resolution, resolved, resolving, }) {
    const passed = !!resolution?.passed || resolved;
    const details = resolution && resolution.details && resolution.details.length > 0
        ? resolution.details
        : resolution
            ? fallback_details(resolution)
            : [];
    // First-ever run with no prior verdict — show a checking row so the user
    // doesn't see an empty panel.
    const initial_check = resolving && !resolution;
    return (_jsxs("div", { className: cn('rounded-md overflow-hidden border', initial_check ? 'bg-[#F8FAFC] border-[#E2E8F0]' :
            passed ? 'bg-[#F0FDF4] border-[#BBF7D0]' : 'bg-[#FFFBEB] border-[#FDE68A]'), children: [_jsxs("div", { className: cn('px-3 py-2 border-b flex items-center gap-2', initial_check ? 'border-[#E2E8F0]' :
                    passed ? 'border-[#BBF7D0]' : 'border-[#FDE68A]'), children: [_jsx("span", { className: cn('text-[10px] font-bold tracking-wide', initial_check ? 'text-[#475569]' :
                            passed ? 'text-[#059669]' : 'text-[#D97706]'), children: initial_check ? 'CHECKING RECEIPTS…' : passed ? 'RESOLVED' : 'RECEIPTS DON’T ADD UP YET' }), resolving && !initial_check && (_jsxs("span", { className: "inline-flex items-center gap-1 text-[10px] font-medium text-[#64748B]", children: [_jsx(Spinner, {}), " updating\u2026"] }))] }), initial_check ? (_jsxs("div", { className: "px-3 py-2 flex items-center gap-2 text-[12px] text-[#64748B]", children: [_jsx(Spinner, {}), " Checking the uploaded files\u2026"] })) : (_jsx("ul", { className: "px-3 py-2 space-y-1.5", children: details.map((d, i) => (_jsx(DetailBullet, { detail: d }, i))) }))] }));
}
function Spinner() {
    return (_jsxs("svg", { className: "w-3 h-3 animate-spin text-[#64748B]", xmlns: "http://www.w3.org/2000/svg", fill: "none", viewBox: "0 0 24 24", children: [_jsx("circle", { className: "opacity-25", cx: "12", cy: "12", r: "10", stroke: "currentColor", strokeWidth: "4" }), _jsx("path", { className: "opacity-75", fill: "currentColor", d: "M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" })] }));
}
function DetailBullet({ detail }) {
    const tone = detail_tone(detail.severity);
    return (_jsxs("li", { className: cn('flex items-start gap-2 text-[12px] leading-relaxed', tone.text), children: [_jsx("span", { className: cn('mt-1 inline-block w-1.5 h-1.5 rounded-full flex-shrink-0', tone.dot) }), _jsxs("span", { className: "flex-1 min-w-0", children: [detail.file_name && _jsx("span", { className: "font-medium", children: detail.file_name }), detail.file_name && _jsx("span", { className: "text-[#94A3B8]", children: " \u2014 " }), _jsx("span", { children: detail.message })] })] }));
}
function detail_tone(severity) {
    switch (severity) {
        case 'success': return { text: 'text-[#065F46]', dot: 'bg-[#059669]' };
        case 'warning': return { text: 'text-[#92400E]', dot: 'bg-[#D97706]' };
        case 'info': return { text: 'text-[#475569]', dot: 'bg-[#94A3B8]' };
    }
}
/** Coarse fallback when an older verdict didn't carry structured details. */
function fallback_details(resolution) {
    return [{
            kind: resolution.passed ? 'success' : 'sum',
            severity: resolution.passed ? 'success' : 'warning',
            message: resolution.summary,
        }];
}
//# sourceMappingURL=send_back_message.js.map