/**
 * ThreadForm file bar — displays a file with inline doc check status,
 * AI classification (agent), validation checks, PDF split sub-docs,
 * client response mechanism for issues, and info dialog.
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useState, useMemo } from 'react';
import { AiOutlineEye } from 'react-icons/ai';
import { cn, PdfIcon } from './shared.js';
import { FileStatusIcon } from '../../shared/file_status_icon.js';
import { DocumentTypeEditor } from '../../shared/document_type_editor.js';
import { FileInfoDialog } from './file_info_dialog.js';
import { HazoCollabFormRFIbox } from '../../hazo_rfi_box/index.js';
import { IssueGroupTree } from './issue_group_tree.js';
import { RuleDecisionRow } from './rule_decision_row.js';
import { make_clarification_id, normalize_validation_rule_results } from '../../../utils/validation_result.js';
export function FileBar({ item, doc_check, ai_result, role, pipeline_status, pipeline_rule_count, pipeline_rule_names, classification_result, validation_rule_results, on_view, on_remove, on_respond, on_accept, on_dismiss, on_send_to_client, on_clarification_respond, on_send_back_issues, on_submit_decisions, sent_back, on_update_content_item, file_manager, on_replacement_files, replaced_by_name, replaces_name, nesting_depth = 0, view_button_variant = 'icon', show_info_icon, task_resolved, sent_back_resolved, available_document_types, on_change_document_type, keep_selected, on_keep_change, show_keep_checkbox, }) {
    const info_icon_visible = show_info_icon ?? role === 'agent';
    const [show_response, set_show_response] = useState(false);
    const [info_dialog_open, set_info_dialog_open] = useState(false);
    const [rfi_expanded, set_rfi_expanded] = useState(false);
    // Initialize from persisted issue_responses on the ContentItem
    const [rfi_responded, set_rfi_responded] = useState(() => {
        if (!item.issue_responses)
            return new Set();
        return new Set(Object.keys(item.issue_responses));
    });
    const [rfi_responses, set_rfi_responses] = useState(() => {
        if (!item.issue_responses)
            return new Map();
        const map = new Map();
        for (const [id, resp] of Object.entries(item.issue_responses)) {
            map.set(id, { response_choice: resp.response_choice, user_comment: resp.user_comment });
        }
        return map;
    });
    if (!item.file)
        return null;
    const has_issue = doc_check?.status === 'issues';
    const is_passed = doc_check?.status === 'passed';
    const is_processing = pipeline_status === 'classifying' || pipeline_status === 'validating';
    const effective_sent_back = sent_back || item.sent_back || false;
    const effective_accepted = item.accepted ?? false;
    // Merge validation results: pipeline prop (live) takes priority, fall back to persisted on item.
    // Clients never see backoffice-phase results — those are for the agent review only.
    const all_validation_results = normalize_validation_rule_results((validation_rule_results && validation_rule_results.length > 0)
        ? validation_rule_results
        : item.validation_rule_results);
    const effective_validation_results = role === 'agent'
        ? all_validation_results
        : all_validation_results.filter(r => r.check_type !== 'backoffice');
    const has_info = !!classification_result || !!item.classification_result || effective_validation_results.length > 0;
    // Derive classification from either direct prop or item's stored result
    const effective_classification = classification_result || item.classification_result || null;
    const is_replaced = !!item.replaced_by_content_id;
    const is_replacement = !!item.replaces_content_id;
    const issue_rows = useMemo(() => {
        const rows = [];
        for (const rule of effective_validation_results) {
            for (const issue of rule.issues) {
                rows.push({
                    rule,
                    issue,
                    clr_id: make_clarification_id(item.content_id, rule.rule_id, issue.issue_id),
                });
            }
        }
        return rows;
    }, [effective_validation_results, item.content_id]);
    const passed_results = useMemo(() => effective_validation_results.filter(r => r.issues.length === 0), [effective_validation_results]);
    const has_validation_issues = issue_rows.length > 0 && !is_processing;
    // Filter the file's validation rules by the agent's per-rule actions.
    // A rule is "visible" (still up for decision) when it has issues AND
    // the agent has not yet decided on it. Already-actioned rules disappear
    // from the panel until rule_actions is cleared (e.g. after doc-type change).
    const rule_actions = item.rule_actions ?? {};
    const visible_rules = useMemo(() => effective_validation_results.filter(r => r.issues.length > 0 && !rule_actions[r.rule_id]), [effective_validation_results, rule_actions]);
    const pending_issue_count = useMemo(() => visible_rules.reduce((n, r) => n + r.issues.length, 0), [visible_rules]);
    const all_validation_passed = passed_results.length > 0 && issue_rows.length === 0 && !is_processing;
    const all_issues_resolved = has_validation_issues && issue_rows.every(row => rfi_responded.has(row.clr_id));
    // Convert validation issues to ClarificationItems for RFI boxes
    const clarification_items = useMemo(() => {
        return issue_rows.map(({ rule, issue, clr_id }) => {
            const is_responded = rfi_responded.has(clr_id);
            const saved_response = rfi_responses.get(clr_id);
            return {
                id: clr_id,
                type: 'additional_info_required',
                status: is_responded ? 'responded' : 'pending',
                target_field_id: '__document',
                target_label: item.file?.file_name || 'Document',
                rule_name: rule.rule_name || rule.rule_id,
                rule_id: rule.rule_id,
                issue_description: issue.issue_description,
                validation_details: issue.issue_description,
                doc_references: [],
                action_description: 'Please review the issue and provide a response.',
                response_options: [
                    { value: 'acknowledge', label: 'Acknowledge', description: 'I understand the issue' },
                    { value: 'replace_document', label: 'Replace Document', description: 'I will upload a corrected document', shows_file_upload: true },
                    { value: 'provide_explanation', label: 'Provide Explanation', description: 'I have additional context', shows_additional_input: true },
                ],
                // Persist response data from previous submit
                response_choice: saved_response?.response_choice,
                user_comment: saved_response?.user_comment,
                response_files: saved_response?.response_files || [],
                created_at: new Date().toISOString(),
            };
        });
    }, [issue_rows, item.file?.file_name, rfi_responded, rfi_responses]);
    return (_jsxs("div", { "data-content-id": item.content_id, className: cn('rounded-md border overflow-hidden', is_replaced ? 'border-[#E2E8F0]/60' : 'border-[#E2E8F0]'), children: [_jsxs("div", { className: cn('flex items-center gap-2.5 px-3 py-2 text-[13px] group', is_replaced ? 'bg-[#F8FAFC]' : 'bg-white', has_issue && !is_replaced && !task_resolved && 'border-b border-amber-200'), style: nesting_depth > 0 ? { paddingLeft: `${12 + nesting_depth * 20}px` } : undefined, children: [is_replacement && (_jsx("span", { className: "text-[#94A3B8] flex-shrink-0 -ml-1", title: replaces_name ? `Replaces ${replaces_name}` : 'Replacement', children: _jsxs("svg", { className: "w-4 h-4", viewBox: "0 0 16 16", fill: "none", stroke: "currentColor", strokeWidth: 1.5, strokeLinecap: "round", strokeLinejoin: "round", children: [_jsx("path", { d: "M4 2v8h8" }), _jsx("path", { d: "M9 7l3 3-3 3" })] }) })), _jsx(FileStatusIcon, { status: pipeline_status ?? null, is_replaced: is_replaced, className: "flex-shrink-0" }), _jsx("span", { className: cn('font-medium truncate flex-1', is_replaced ? 'text-[#94A3B8] line-through' : 'text-[#1a1a1a]'), children: item.file.file_name }), item.file.file_size && _jsx("span", { className: cn('text-[11px]', is_replaced ? 'text-[#CBD5E1]' : 'text-[#999]'), children: item.file.file_size }), is_replaced && (_jsxs("span", { className: "inline-flex items-center gap-0.5 px-1.5 py-px rounded text-[9px] font-medium text-[#94A3B8] bg-[#F1F5F9] flex-shrink-0", title: replaced_by_name ? `Replaced by ${replaced_by_name}` : 'Replaced', children: [_jsx("svg", { className: "w-2.5 h-2.5", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", strokeWidth: 2, children: _jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M19 7l-7 7-7-7" }) }), "Replaced"] })), all_validation_passed && !task_resolved && (_jsxs("span", { className: "inline-flex items-center gap-0.5 px-1.5 py-px rounded text-[9px] font-medium text-green-600 bg-green-50 flex-shrink-0", title: "All validation checks passed", children: [_jsx("svg", { className: "w-2.5 h-2.5", fill: "currentColor", viewBox: "0 0 20 20", children: _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" }) }), "Passed"] })), has_validation_issues && all_issues_resolved && role === 'agent' && !effective_sent_back && !effective_accepted && !task_resolved && (_jsxs("button", { type: "button", onClick: () => set_rfi_expanded(prev => !prev), className: "inline-flex items-center gap-0.5 px-1.5 py-px rounded text-[9px] font-medium cursor-pointer transition-colors flex-shrink-0 text-amber-600 bg-amber-50 hover:bg-amber-100", title: "Client responded \u2014 click to review", children: [_jsx("svg", { className: "w-2.5 h-2.5", fill: "currentColor", viewBox: "0 0 20 20", children: _jsx("path", { fillRule: "evenodd", d: "M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z", clipRule: "evenodd" }) }), "Pending Review"] })), effective_accepted && !effective_sent_back && !task_resolved && (_jsxs("span", { className: "inline-flex items-center gap-0.5 px-1.5 py-px rounded text-[9px] font-medium text-green-600 bg-green-50 flex-shrink-0", title: "Agent accepted the client's response", children: [_jsx("svg", { className: "w-2.5 h-2.5", fill: "currentColor", viewBox: "0 0 20 20", children: _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" }) }), "Accepted"] })), has_validation_issues && all_issues_resolved && role === 'client' && !task_resolved && (_jsxs("button", { type: "button", onClick: () => set_rfi_expanded(prev => !prev), className: "inline-flex items-center gap-0.5 px-1.5 py-px rounded text-[9px] font-medium cursor-pointer transition-colors flex-shrink-0 text-amber-600 bg-amber-50 hover:bg-amber-100", title: "All issues resolved \u2014 click to view", children: [_jsx("svg", { className: "w-2.5 h-2.5", fill: "currentColor", viewBox: "0 0 20 20", children: _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" }) }), issue_rows.length, " Resolved"] })), has_validation_issues && !all_issues_resolved && !task_resolved && !effective_sent_back && !effective_accepted && (_jsxs("button", { type: "button", onClick: () => set_rfi_expanded(prev => !prev), className: "inline-flex items-center gap-0.5 px-1.5 py-px rounded text-[9px] font-medium cursor-pointer transition-colors flex-shrink-0 text-red-600 bg-red-50 hover:bg-red-100", title: "Click to view and respond to validation issues", children: [_jsx("svg", { className: "w-2.5 h-2.5", fill: "currentColor", viewBox: "0 0 20 20", children: _jsx("path", { fillRule: "evenodd", d: "M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z", clipRule: "evenodd" }) }), pending_issue_count, " issue", pending_issue_count !== 1 ? 's' : ''] })), effective_sent_back && !task_resolved && !sent_back_resolved && (_jsxs("span", { className: "inline-flex items-center gap-0.5 px-1.5 py-px rounded text-[9px] font-medium text-blue-600 bg-blue-50 flex-shrink-0", title: "Issues sent back to client", children: [_jsx("svg", { className: "w-2.5 h-2.5", fill: "currentColor", viewBox: "0 0 20 20", children: _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" }) }), "Sent Back"] })), effective_sent_back && sent_back_resolved && !task_resolved && (_jsxs("span", { className: "inline-flex items-center gap-0.5 px-1.5 py-px rounded text-[9px] font-medium text-[#059669] bg-[#ECFDF5] flex-shrink-0", title: "Issues addressed in the spawned question", children: [_jsx("svg", { className: "w-2.5 h-2.5", fill: "currentColor", viewBox: "0 0 20 20", children: _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" }) }), "Resolved"] })), !is_processing && doc_check?.relevance && !task_resolved && ((() => {
                        const rel = doc_check.relevance;
                        const cfg = rel === 'matches'
                            ? { label: 'Matches', tone: 'text-[#059669] bg-[#ECFDF5]' }
                            : rel === 'mismatch'
                                ? { label: 'Wrong receipt', tone: 'text-[#B45309] bg-[#FFF7ED]' }
                                : rel === 'unclear'
                                    ? { label: 'Unclear', tone: 'text-[#475569] bg-[#F1F5F9]' }
                                    : { label: 'Off-topic', tone: 'text-[#DC2626] bg-[#FEE2E2]' };
                        return (_jsx("span", { className: cn('inline-flex items-center gap-0.5 px-1.5 py-px rounded text-[9px] font-medium flex-shrink-0', cfg.tone), title: doc_check.relevance_reason, children: cfg.label }));
                    })()), is_passed && !is_processing && !has_validation_issues && !all_validation_passed && !all_issues_resolved && !effective_sent_back && !task_resolved && !doc_check?.relevance && (_jsxs("span", { className: "text-[11px] text-[#059669] font-medium flex items-center gap-1", children: [_jsx("span", { className: "w-1.5 h-1.5 rounded-full bg-[#059669]" }), "Verified"] })), has_issue && !is_processing && !has_validation_issues && !all_issues_resolved && !task_resolved && !doc_check?.relevance && (_jsxs("span", { className: "text-[11px] text-amber-600 font-medium flex items-center gap-1", children: [_jsx("span", { className: "w-1.5 h-1.5 rounded-full bg-amber-500" }), "Issue found"] })), task_resolved && !is_processing && (_jsxs("span", { className: "text-[11px] text-[#059669] font-medium flex items-center gap-1", children: [_jsx("span", { className: "w-1.5 h-1.5 rounded-full bg-[#059669]" }), "Resolved"] })), role === 'agent' && effective_classification && !is_processing && (_jsx(DocumentTypeEditor, { current: effective_classification.document_type, available: available_document_types ?? [], on_change: on_change_document_type
                            ? (new_type) => on_change_document_type(item, new_type)
                            : undefined, disabled: task_resolved })), !is_processing && info_icon_visible && (_jsx("button", { onClick: () => set_info_dialog_open(true), className: "p-0.5 rounded text-[#94A3B8] hover:text-blue-500 transition-colors flex-shrink-0 cursor-pointer", title: "File details", children: _jsx("svg", { className: "w-3.5 h-3.5", fill: "currentColor", viewBox: "0 0 20 20", children: _jsx("path", { fillRule: "evenodd", d: "M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z", clipRule: "evenodd" }) }) })), view_button_variant === 'icon' ? (_jsx("button", { onClick: () => on_view?.(item), className: "p-0.5 rounded text-[#94A3B8] hover:text-[#2563EB] transition-colors flex-shrink-0 cursor-pointer", title: "View file", children: _jsx(AiOutlineEye, { className: "w-4 h-4" }) })) : (_jsx("button", { onClick: () => on_view?.(item), className: "text-[11px] text-[#2563EB] font-medium opacity-0 group-hover:opacity-100 transition-opacity cursor-pointer", children: "View" })), on_remove && !task_resolved && (_jsx("button", { onClick: () => on_remove(item), className: "p-0.5 rounded text-[#94A3B8] hover:text-[#EF4444] transition-colors flex-shrink-0 opacity-0 group-hover:opacity-100 cursor-pointer", title: "Remove file", children: _jsx("svg", { className: "w-3.5 h-3.5", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: _jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M6 18L18 6M6 6l12 12" }) }) })), keep_selected !== undefined && show_keep_checkbox !== false && (_jsx("input", { type: "checkbox", checked: keep_selected, onChange: () => on_keep_change?.({ [item.content_id]: !keep_selected }), disabled: task_resolved, className: "ml-1 rounded border-[#CBD5E1] text-[#2563EB] focus:ring-[#2563EB] flex-shrink-0 disabled:cursor-not-allowed disabled:opacity-60 cursor-pointer", title: task_resolved
                            ? 'Locked — task is resolved'
                            : keep_selected
                                ? 'Kept — uncheck to drop from collected data'
                                : 'Mark as kept for collected data' }))] }), is_processing && (_jsxs("div", { className: "px-3 py-1.5 bg-[#FAFBFC] border-t border-[#F1F5F9] flex items-center gap-1.5", children: [_jsxs("svg", { className: "h-2.5 w-2.5 flex-shrink-0 text-blue-500 animate-spin", 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" })] }), _jsx("span", { className: "text-[11px] italic text-[#94A3B8]", title: pipeline_status === 'validating' && pipeline_rule_names && pipeline_rule_names.length > 0
                            ? pipeline_rule_names.join('\n')
                            : undefined, children: pipeline_status === 'classifying'
                            ? 'Classifying document...'
                            : pipeline_rule_count
                                ? `Running ${pipeline_rule_count} validation rule${pipeline_rule_count !== 1 ? 's' : ''}…`
                                : 'Running validation rules...' })] })), pipeline_status === 'error' && (_jsx("div", { className: "px-3 py-1.5 bg-red-50/50 border-t border-red-100 flex items-center gap-1.5", children: _jsx("span", { className: "text-[11px] italic text-red-400", children: "Processing failed \u2014 classification or validation unavailable" }) })), has_issue && !has_validation_issues && !all_issues_resolved && !task_resolved && !doc_check?.relevance && (_jsxs("div", { className: "bg-amber-50/60 px-3 py-2 space-y-1", children: [doc_check?.summary && (_jsxs("div", { className: "flex items-start gap-2 text-[12px] text-amber-900", children: [_jsx("span", { className: "mt-0.5 text-amber-500 flex-shrink-0", children: "\u26A0" }), _jsx("p", { className: "flex-1 font-semibold", children: doc_check.summary })] })), doc_check?.issues?.map((issue, i) => (_jsxs("div", { className: cn('flex items-start gap-2 text-[12px] text-amber-800', doc_check?.summary && 'pl-5'), children: [!doc_check?.summary && _jsx("span", { className: "mt-0.5 text-amber-500 flex-shrink-0", children: "\u26A0" }), _jsxs("p", { className: "flex-1", children: [_jsxs("span", { className: "font-medium", children: [item.file.file_name, ":"] }), " ", issue.description] })] }, i)))] })), rfi_expanded && has_validation_issues && role === 'client' && clarification_items.length > 0 && (_jsx("div", { className: "border-t border-[#E2E8F0] bg-[#FAFBFC]", children: clarification_items.map(clr_item => (_jsx("div", { className: "px-3 py-2", children: _jsx(HazoCollabFormRFIbox, { item: clr_item, mode: "respond", display: "collapsed", file_manager: file_manager, on_respond: (id, response) => {
                            set_rfi_responded(prev => new Set([...prev, id]));
                            set_rfi_responses(prev => {
                                const next = new Map(prev);
                                next.set(id, response);
                                return next;
                            });
                            on_clarification_respond?.(id, response);
                            // Persist to ContentItem for localStorage survival
                            const updated_responses = { ...(item.issue_responses || {}) };
                            updated_responses[id] = {
                                response_choice: response.response_choice,
                                user_comment: response.user_comment,
                                responded_at: new Date().toISOString(),
                            };
                            on_update_content_item?.(item.content_id, { issue_responses: updated_responses });
                            // Add replacement files to the main thread for pipeline processing
                            if (response.response_choice === 'replace_document' && response.response_files && response.response_files.length > 0) {
                                on_replacement_files?.(item.content_id, response.response_files);
                            }
                            const new_responded = new Set([...rfi_responded, id]);
                            const all_done = issue_rows.every(row => new_responded.has(row.clr_id));
                            if (all_done)
                                set_rfi_expanded(false);
                        }, on_accept: (id) => {
                            set_rfi_responded(prev => new Set([...prev, id]));
                        } }) }, clr_item.id))) })), rfi_expanded && has_validation_issues && role === 'agent' && !task_resolved && visible_rules.length > 0 && (() => {
                // File-level Accept all stamps every visible rule as accepted.
                const accept_all_label = visible_rules.length === 1 ? 'Accept' : 'Accept all';
                const handle_accept_all = () => {
                    on_submit_decisions?.(item, visible_rules.map(r => ({
                        rule_id: r.rule_id,
                        action: 'accepted',
                        rule_result: r,
                    })));
                    set_rfi_expanded(false);
                };
                // Single-rule fast path — keep today's UX entirely.
                if (visible_rules.length === 1) {
                    const rule_result = visible_rules[0];
                    return (_jsxs("div", { className: "border-t border-[#E2E8F0] bg-[#FAFBFC] px-3 py-3 space-y-3", children: [_jsxs("div", { className: "flex items-center justify-between", children: [_jsx("div", { className: "text-[10px] font-semibold text-[#64748B] uppercase tracking-wider", children: "Client Responses \u2014 Pending Review" }), _jsx("button", { type: "button", onClick: handle_accept_all, className: "text-[11px] font-medium text-[#059669] border border-[#059669]/30 bg-green-50 px-3 py-1 rounded hover:bg-green-100 cursor-pointer", children: accept_all_label })] }), _jsx(IssueGroupTree, { mode: "agent", content_id: item.content_id, result: rule_result, on_accept: () => {
                                    on_submit_decisions?.(item, [{
                                            rule_id: rule_result.rule_id,
                                            action: 'accepted',
                                            rule_result,
                                        }]);
                                    set_rfi_expanded(false);
                                }, on_send_back: (send_back_payloads) => {
                                    // The IssueGroupTree's on_send_back delivers the agent's
                                    // edited comment in send_back_payloads[0].agent_feedback.
                                    const payload = send_back_payloads[0];
                                    on_submit_decisions?.(item, [{
                                            rule_id: rule_result.rule_id,
                                            action: 'sent_back',
                                            comment: payload?.agent_feedback,
                                            rule_result,
                                        }]);
                                    set_rfi_expanded(false);
                                } })] }));
                }
                // Multi-rule batch UI.
                return (_jsx(BatchDecisionPanel, { item: item, visible_rules: visible_rules, on_submit: (decisions) => {
                        on_submit_decisions?.(item, decisions);
                        set_rfi_expanded(false);
                    }, on_accept_all: handle_accept_all, accept_all_label: accept_all_label }));
            })(), role === 'agent' && ai_result?.split_items && ai_result.split_items.length > 0 && (_jsxs(_Fragment, { children: [_jsxs("div", { className: "px-3 py-1.5 bg-[#F1F5F9] text-[10px] text-[#64748B] uppercase tracking-wider font-medium border-t border-[#E2E8F0]", children: [ai_result.split_items.length, " documents detected in this file"] }), ai_result.split_items.map(split => (_jsxs("div", { className: "flex items-center gap-2.5 px-3 py-2 text-[12px] border-t border-[#F1F5F9] bg-white group hover:bg-[#FAFBFC]", children: [_jsx(PdfIcon, { className: "w-3.5 h-3.5" }), _jsx("span", { className: "text-[#94A3B8] text-[11px] w-16 flex-shrink-0", children: split.split_info?.page_range }), _jsx("span", { className: "text-[#0F172A] font-medium flex-1", children: split.split_info?.label }), _jsx("button", { onClick: () => on_view?.(split), className: "text-[#2563EB] text-[11px] opacity-0 group-hover:opacity-100 cursor-pointer", children: "View" })] }, split.content_id))), ai_result.review_status === 'pending' && (_jsxs("div", { className: "px-3 py-2 bg-[#F8FAFC] flex gap-2 border-t border-[#E2E8F0]", children: [_jsx("button", { onClick: () => on_accept?.(ai_result.content_ref), className: "text-[11px] font-medium text-[#2563EB] hover:underline cursor-pointer", children: "Accept splits" }), _jsx("button", { className: "text-[11px] text-[#94A3B8] hover:underline cursor-pointer", children: "Adjust" })] }))] })), _jsx(FileInfoDialog, { open: info_dialog_open, on_open_change: set_info_dialog_open, file_name: item.file.file_name, classification: effective_classification, validation_results: effective_validation_results })] }));
}
function BatchDecisionPanel({ item, visible_rules, on_submit, on_accept_all, accept_all_label }) {
    const [decisions, set_decisions] = useState({});
    const [comments, set_comments] = useState({});
    // Default comment per rule: the LLM's client_comment from the rule's first
    // issue, falling back to the issue_description (commit 8295db2 pattern).
    const default_comment_for = (rule) => {
        const first = rule.issues[0];
        if (!first)
            return '';
        const c = first.client_comment;
        return (c && c.trim()) ? c : first.issue_description;
    };
    const decided_count = visible_rules.filter(r => {
        const v = decisions[r.rule_id];
        return v === 'accepted' || v === 'sent_back';
    }).length;
    const can_submit = decided_count > 0;
    const handle_submit = () => {
        const payload = visible_rules
            .map(r => {
            const v = decisions[r.rule_id];
            if (v !== 'accepted' && v !== 'sent_back')
                return null;
            const comment = v === 'sent_back'
                ? (comments[r.rule_id] || default_comment_for(r))
                : undefined;
            return { rule_id: r.rule_id, action: v, comment, rule_result: r };
        })
            .filter((d) => d !== null);
        on_submit(payload);
    };
    return (_jsxs("div", { className: "border-t border-[#E2E8F0] bg-[#FAFBFC] px-3 py-3 space-y-3", children: [_jsxs("div", { className: "flex items-center justify-between", children: [_jsx("div", { className: "text-[10px] font-semibold text-[#64748B] uppercase tracking-wider", children: "Client Responses \u2014 Pending Review" }), _jsxs("div", { className: "flex items-center gap-2", children: [_jsx("button", { type: "button", onClick: on_accept_all, className: "text-[11px] font-medium text-[#059669] border border-[#059669]/30 bg-green-50 px-3 py-1 rounded hover:bg-green-100 cursor-pointer", children: accept_all_label }), _jsxs("button", { type: "button", disabled: !can_submit, onClick: handle_submit, className: "text-[11px] font-medium text-white bg-[#2563EB] hover:bg-[#1D4ED8] px-3 py-1 rounded cursor-pointer disabled:bg-[#94A3B8] disabled:cursor-not-allowed", children: ["Submit Decisions", decided_count > 0 ? ` (${decided_count})` : ''] })] })] }), visible_rules.map(rule => {
                const value = decisions[rule.rule_id] ?? 'pending';
                const default_comment = default_comment_for(rule);
                const comment = comments[rule.rule_id] ?? '';
                return (_jsx(RuleDecisionRow, { content_id: item.content_id, rule_result: rule, value: value, comment: comment, default_comment: default_comment, on_value_change: (next) => {
                        set_decisions(prev => ({ ...prev, [rule.rule_id]: next }));
                        if (next === 'sent_back') {
                            // Pre-fill with the LLM-suggested comment so the agent sees
                            // editable text rather than a placeholder-only empty textarea.
                            set_comments(prev => prev[rule.rule_id] !== undefined
                                ? prev
                                : { ...prev, [rule.rule_id]: default_comment_for(rule) });
                        }
                        else {
                            // Discard the in-flight comment when toggling away from Send Back.
                            set_comments(prev => {
                                const { [rule.rule_id]: _, ...rest } = prev;
                                return rest;
                            });
                        }
                    }, on_comment_change: (next) => {
                        set_comments(prev => ({ ...prev, [rule.rule_id]: next }));
                    } }, rule.rule_id));
            })] }));
}
//# sourceMappingURL=file_bar.js.map