/**
 * RuleResultCard — Shared component for displaying a single validation rule result.
 * Shows rule name, explanation, and resolution details (if resolved).
 * Used by FileBarValidationDialog and ValidationDetailDialog (interim_view).
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useCallback } from 'react';
import { cn } from '../../utils/cn.js';
import { HiCheckCircle, HiExclamationCircle, HiDocument, HiEye } from 'react-icons/hi';
import { get_rule_explanation } from '../../utils/format.js';
/** Build pre-populated message for send-back compose area */
function build_default_message(rr, file_name, choice, comment) {
    const rule_label = rr.rule_name ?? rr.rule_id ?? 'validation';
    let msg = `The validation rule "${rule_label}" flagged an issue with the document ${file_name}.`;
    if (choice) {
        msg += `\n\nYou previously responded "${choice}"`;
        if (comment)
            msg += ` with comment "${comment}"`;
        msg += '.';
    }
    msg += '\n\nHowever, we need further action on this item. ';
    return msg;
}
export function RuleResultCard({ rr, responded_map, on_reject, on_send_back, on_accept, file_info, on_view_file }) {
    const explanation = get_rule_explanation(rr);
    // Check if this rule was resolved — via user_resolved flag or responded_map
    const match = responded_map ? [...responded_map.values()].find((c) => c.rule_id === rr.rule_id && (c.status === 'responded' || c.status === 'resolved')) : undefined;
    const is_resolved = rr.user_resolved || !!match;
    const resolution = match ?? rr.user_resolution;
    const choice = resolution?.response_choice ?? rr.user_resolution?.response_choice;
    const comment = resolution?.user_comment ?? rr.user_resolution?.user_comment;
    const files = match?.response_files;
    // Compose state for send-back
    const [is_composing, set_is_composing] = useState(false);
    const [message, set_message] = useState('');
    const handle_open_compose = useCallback(() => {
        set_message(build_default_message(rr, file_info?.file_name ?? 'this document', choice, comment));
        set_is_composing(true);
    }, [rr, file_info?.file_name, choice, comment]);
    const handle_send = useCallback(() => {
        if (!on_send_back || !message.trim())
            return;
        on_send_back(rr, message.trim());
        set_is_composing(false);
        set_message('');
    }, [on_send_back, rr, message]);
    const handle_cancel = useCallback(() => {
        set_is_composing(false);
        set_message('');
    }, []);
    // Original LLM issue text
    const original_issue = rr.summary ?? rr.issues[0]?.issue_description ?? rr.raw_response ?? explanation;
    const has_issue = rr.issues.length > 0;
    return (_jsxs("div", { className: cn('rounded-md overflow-hidden', rr.accepted
            ? 'border border-green-200'
            : rr.sent_back
                ? 'border border-blue-200'
                : is_resolved
                    ? 'border border-amber-200'
                    : has_issue
                        ? 'border border-destructive/30'
                        : 'border border-green-200'), children: [_jsxs("div", { className: cn('flex items-start gap-2 p-2', rr.accepted
                    ? 'bg-green-50/50'
                    : rr.sent_back
                        ? 'bg-blue-50/50'
                        : is_resolved
                            ? 'bg-amber-50/50'
                            : has_issue
                                ? 'bg-destructive/5'
                                : 'bg-green-50/50'), children: [rr.accepted ? (_jsxs("span", { className: "inline-flex items-center flex-shrink-0 mt-0.5", children: [_jsx(HiCheckCircle, { className: "h-4 w-4 text-green-500" }), _jsx(HiCheckCircle, { className: "h-4 w-4 text-green-500 -ml-2" })] })) : rr.sent_back ? (_jsx(HiCheckCircle, { className: "h-4 w-4 text-blue-500 flex-shrink-0 mt-0.5" })) : is_resolved ? (_jsx(HiCheckCircle, { className: "h-4 w-4 text-amber-500 flex-shrink-0 mt-0.5" })) : has_issue ? (_jsx(HiExclamationCircle, { className: "h-4 w-4 text-destructive flex-shrink-0 mt-0.5" })) : (_jsx(HiCheckCircle, { className: "h-4 w-4 text-green-600 flex-shrink-0 mt-0.5" })), _jsxs("div", { className: "min-w-0 flex-1", children: [_jsxs("div", { className: "flex items-center justify-between gap-2", children: [_jsx("span", { className: "font-medium text-sm", children: rr.rule_name ?? rr.rule_id }), is_resolved && rr.accepted && (_jsx("span", { className: "px-2.5 py-1 text-[11px] font-medium rounded-md bg-green-100 text-green-700 flex-shrink-0", children: "Accepted" })), is_resolved && rr.sent_back && !rr.accepted && (_jsx("span", { className: "px-2.5 py-1 text-[11px] font-medium rounded-md bg-blue-100 text-blue-700 flex-shrink-0", children: "Sent Back" })), is_resolved && !rr.sent_back && !rr.accepted && (_jsxs("div", { className: "flex items-center gap-1.5 flex-shrink-0", children: [on_accept && (_jsx("button", { type: "button", onClick: (e) => { e.stopPropagation(); on_accept(rr); }, className: "px-2.5 py-1 text-[11px] font-medium rounded-md bg-green-600 text-white hover:bg-green-700 transition-colors", children: "Accept" })), on_send_back && !is_composing && (_jsx("button", { type: "button", onClick: (e) => { e.stopPropagation(); handle_open_compose(); }, className: "px-2.5 py-1 text-[11px] font-medium rounded-md bg-blue-600 text-white hover:bg-blue-700 transition-colors", children: "\u2191 Send Back" }))] }))] }), explanation && (_jsx("p", { className: "text-muted-foreground text-xs mt-0.5 leading-relaxed", children: explanation })), is_resolved && choice && (_jsxs("div", { className: "mt-1.5 text-xs", children: [_jsxs("div", { className: "text-amber-600 font-medium", children: ["Client resolved: ", choice] }), comment && (_jsxs("p", { className: "mt-0.5 text-muted-foreground italic pl-2 border-l-2 border-amber-200", children: ["\u201C", comment, "\u201D"] })), files && files.length > 0 && (_jsxs("div", { className: "mt-1 pl-2 border-l-2 border-amber-200", children: [_jsx("span", { className: "text-muted-foreground font-medium", children: "Attached files:" }), _jsx("div", { className: "mt-0.5 space-y-0.5", children: files.map((f) => (_jsxs("div", { className: "flex items-center gap-1.5 text-muted-foreground", children: [_jsx(HiDocument, { className: "h-3 w-3 flex-shrink-0" }), _jsx("span", { className: "truncate", children: f.file_name }), _jsx("span", { className: "text-[10px] text-muted-foreground/60", children: f.file_size ? `${(f.file_size / 1024).toFixed(1)} KB` : '' })] }, f.file_id))) })] })), on_reject && match && !is_composing && !on_send_back && (_jsx("button", { type: "button", onClick: (e) => { e.stopPropagation(); on_reject(match); }, className: "mt-1.5 px-2.5 py-1 text-[11px] font-medium rounded-md bg-destructive text-destructive-foreground hover:bg-destructive/90 transition-colors", children: "Reject" }))] }))] })] }), is_composing && (_jsxs("div", { className: "p-4 bg-blue-50 border-t border-blue-200", children: [file_info && (_jsxs("div", { className: "mb-3", children: [_jsx("div", { className: "text-[11px] uppercase tracking-wide text-blue-800 font-semibold mb-1.5", children: "Referenced Document" }), _jsxs("div", { className: "flex items-center gap-2 p-2 bg-white border border-blue-200 rounded-lg", children: [_jsx(HiDocument, { className: "h-4.5 w-4.5 text-red-500 flex-shrink-0" }), _jsxs("div", { className: "flex-1 min-w-0", children: [_jsx("div", { className: "text-xs font-medium text-gray-900 truncate", children: file_info.file_name }), file_info.file_size != null && (_jsxs("div", { className: "text-[10px] text-gray-400", children: [(file_info.file_size / 1024).toFixed(1), " KB"] }))] }), file_info.classification && (_jsx("span", { className: "px-1.5 py-0.5 bg-blue-50 rounded text-[10px] text-blue-600", children: file_info.classification })), on_view_file && (_jsxs("button", { type: "button", onClick: () => on_view_file(file_info.file_id), className: "flex items-center gap-1 px-2 py-0.5 border border-blue-200 rounded text-[11px] text-blue-600 hover:bg-blue-50 transition-colors", title: "View document", children: [_jsx(HiEye, { className: "h-3 w-3" }), " View"] }))] })] })), _jsxs("div", { className: "mb-3", children: [_jsx("div", { className: "text-[11px] uppercase tracking-wide text-blue-800 font-semibold mb-1.5", children: "Message to Client" }), _jsx("textarea", { value: message, onChange: (e) => set_message(e.target.value), className: "w-full p-3 bg-white border border-blue-200 rounded-lg text-xs leading-relaxed text-gray-700 resize-y min-h-[80px] focus:outline-none focus:ring-2 focus:ring-blue-300 focus:border-blue-300", placeholder: "Describe the issue and what action the client should take..." })] }), _jsxs("div", { className: "mb-4", children: [_jsx("div", { className: "text-[11px] uppercase tracking-wide text-blue-800 font-semibold mb-1.5", children: "Client Will Respond With" }), _jsxs("div", { className: "flex gap-2 flex-wrap", children: [_jsx("span", { className: "px-2.5 py-1 bg-white border border-blue-200 rounded-full text-[11px] text-blue-600", children: "\uD83D\uDCC2 Upload corrected document" }), _jsx("span", { className: "px-2.5 py-1 bg-white border border-blue-200 rounded-full text-[11px] text-blue-600", children: "\uD83D\uDCAC Provide comment" })] })] }), _jsxs("div", { className: "flex justify-end gap-2", children: [_jsx("button", { type: "button", onClick: handle_cancel, className: "px-3 py-1.5 bg-white border border-blue-200 text-gray-500 rounded-md text-xs hover:bg-gray-50 transition-colors", children: "Cancel" }), _jsx("button", { type: "button", onClick: handle_send, disabled: !message.trim(), className: "px-3 py-1.5 bg-blue-600 text-white rounded-md text-xs font-semibold hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors", children: "Add to Send Back" })] })] }))] }));
}
//# sourceMappingURL=rule_result_card.js.map