/**
 * Task card — expandable card with checkbox, title, status, thread timeline.
 */
'use client';
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from 'react';
import { cn, format_date } from './shared.js';
import { ThreadTimeline } from './thread_timeline.js';
import { ThreadComposer } from './thread_composer.js';
import { CoverageCard } from './coverage_card.js';
/**
 * Shorten a filename for the back-reference chip so it doesn't dominate the
 * row. Keeps the head and extension, elides the middle.
 */
function format_ref_file_name(name, max = 32) {
    if (name.length <= max)
        return name;
    const dot = name.lastIndexOf('.');
    if (dot <= 0 || dot > name.length - 2)
        return name.slice(0, max - 1) + '…';
    const ext = name.slice(dot);
    const head = name.slice(0, Math.max(1, max - ext.length - 1));
    return `${head}…${ext}`;
}
export function TaskCard({ task, role, expanded, question_number, parent_ref, on_navigate_to_task, user_name, user_id, on_toggle, on_toggle_complete, on_delete_task, on_approve_task, on_reopen_task, on_rename_task, on_send, on_send_comment, on_upload_files, on_add_question, on_view_file, on_remove_file, on_accept, on_dismiss, on_send_to_client, on_edit_message, on_delete_message, upload_file, on_send_back_issues, on_submit_decisions, on_update_content_item, file_statuses, classification_results, validation_results, file_manager, on_replacement_files, view_button_variant, show_info_icon, sent_back_resolved_ids, resolving, available_document_types, on_change_document_type, keep_selections, on_keep_change, on_reply_to_issue, }) {
    const file_count = task.thread.reduce((acc, m) => acc + (m.content_items?.filter(i => i.type === 'file').length || 0), 0);
    const msg_count = task.thread.filter(m => m.type === 'comment' || m.type === 'clarification').length;
    const [editing_title, set_editing_title] = useState(false);
    const [draft_title, set_draft_title] = useState(task.title);
    const can_rename = role === 'agent' && !!on_rename_task && task.status !== 'completed';
    const start_rename = () => {
        set_draft_title(task.title);
        set_editing_title(true);
    };
    const commit_rename = () => {
        const trimmed = draft_title.trim();
        if (trimmed && trimmed !== task.title)
            on_rename_task?.(task.task_id, trimmed);
        set_editing_title(false);
    };
    const cancel_rename = () => {
        set_draft_title(task.title);
        set_editing_title(false);
    };
    return (_jsxs("div", { id: `task-${task.task_id}`, className: cn('transition-all border-b border-[#E2E8F0] group/task rounded'), children: [_jsxs("button", { onClick: on_toggle, className: "w-full flex items-center gap-3 text-left py-3", children: [_jsx("div", { onClick: (e) => { e.stopPropagation(); on_toggle_complete?.(task.task_id); }, className: cn('w-5 h-5 rounded-sm flex items-center justify-center flex-shrink-0 cursor-pointer', task.status === 'completed' ? 'bg-[#059669] text-white' : 'border border-[#CBD5E1]'), children: task.status === 'completed' && _jsx("span", { className: "text-xs", children: "\u2713" }) }), _jsx("div", { className: "flex-1 min-w-0 flex items-center gap-2 group/title", children: editing_title ? (_jsxs(_Fragment, { children: [_jsx("input", { autoFocus: true, value: draft_title, onChange: (e) => set_draft_title(e.target.value), onClick: (e) => e.stopPropagation(), onMouseDown: (e) => e.stopPropagation(), onKeyDown: (e) => {
                                        e.stopPropagation();
                                        if (e.key === 'Enter') {
                                            e.preventDefault();
                                            commit_rename();
                                        }
                                        if (e.key === 'Escape') {
                                            e.preventDefault();
                                            cancel_rename();
                                        }
                                    }, onBlur: commit_rename, className: "flex-1 min-w-0 text-[13px] font-medium text-gray-900 border border-[#2563EB] rounded px-2 py-0.5 outline-none focus:ring-2 focus:ring-[#2563EB]/20" }), _jsx("div", { role: "button", tabIndex: 0, onClick: (e) => { e.stopPropagation(); commit_rename(); }, onMouseDown: (e) => e.stopPropagation(), className: "p-1 text-[#059669] hover:bg-green-50 rounded cursor-pointer flex-shrink-0", title: "Save", 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: "M5 13l4 4L19 7" }) }) }), _jsx("div", { role: "button", tabIndex: 0, onClick: (e) => { e.stopPropagation(); cancel_rename(); }, onMouseDown: (e) => e.stopPropagation(), className: "p-1 text-[#EF4444] hover:bg-red-50 rounded cursor-pointer flex-shrink-0", title: "Cancel", 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" }) }) })] })) : (_jsxs(_Fragment, { children: [_jsxs("div", { className: "flex-1 min-w-0", children: [_jsxs("div", { className: "flex items-center gap-2", children: [typeof question_number === 'number' && (_jsxs("span", { className: "inline-flex items-center text-[10px] font-semibold text-[#64748B] bg-[#F1F5F9] px-1.5 py-0.5 rounded flex-shrink-0", "aria-label": `Question ${question_number}`, children: ["Q", question_number] })), _jsx("span", { className: cn('text-[13px] font-medium truncate', task.status === 'completed' ? 'line-through text-gray-400' : 'text-gray-900'), children: task.title })] }), parent_ref && (_jsxs("span", { role: "button", tabIndex: 0, onClick: (e) => { e.stopPropagation(); on_navigate_to_task?.(parent_ref.parent_task_id); }, onMouseDown: (e) => e.stopPropagation(), onKeyDown: (e) => {
                                                if (e.key === 'Enter' || e.key === ' ') {
                                                    e.preventDefault();
                                                    e.stopPropagation();
                                                    on_navigate_to_task?.(parent_ref.parent_task_id);
                                                }
                                            }, className: "mt-1 inline-flex items-center gap-1.5 max-w-full text-[11px] text-[#64748B] hover:text-[#0369A1] transition-colors cursor-pointer", title: `Go to Q${parent_ref.parent_question_number}`, children: [_jsxs("svg", { className: "w-3 h-3 flex-shrink-0", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round", children: [_jsx("path", { d: "M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71" }), _jsx("path", { d: "M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71" })] }), _jsxs("span", { className: "truncate", children: ["From ", _jsxs("span", { className: "font-medium text-[#334155]", children: ["Q", parent_ref.parent_question_number] }), parent_ref.file_name && (_jsxs(_Fragment, { children: [" \u00B7 ", _jsx("span", { className: "truncate", children: format_ref_file_name(parent_ref.file_name) })] })), parent_ref.rule_names.length > 0 && (_jsxs(_Fragment, { children: [" \u00B7 ", _jsx("span", { className: "font-medium text-[#334155]", children: parent_ref.rule_names[0] }), parent_ref.rule_names.length > 1 && _jsxs("span", { className: "text-[#94A3B8]", children: [" +", parent_ref.rule_names.length - 1] })] }))] })] })), !expanded && !parent_ref && (_jsxs("p", { className: "text-xs text-gray-400 truncate mt-0.5", children: [msg_count > 0 && _jsxs("span", { className: "mr-2", children: [msg_count, " comment", msg_count !== 1 ? 's' : ''] }), file_count > 0 && _jsxs("span", { className: "mr-2", children: ["\u00B7 ", file_count, " file", file_count !== 1 ? 's' : ''] })] }))] }), can_rename && (_jsx("div", { role: "button", tabIndex: 0, onClick: (e) => { e.stopPropagation(); start_rename(); }, onMouseDown: (e) => e.stopPropagation(), className: "p-1 text-[#94A3B8] hover:text-[#2563EB] hover:bg-blue-50 rounded cursor-pointer flex-shrink-0 opacity-0 group-hover/task:opacity-100 transition-opacity", title: "Rename task", 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: 1.5, d: "M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" }) }) }))] })) }), task.completed_at && _jsx("span", { className: "text-xs text-gray-400 hidden sm:inline", children: format_date(task.completed_at) }), task.status === 'needs_attention' && (_jsx("span", { className: "text-xs font-medium text-amber-600 bg-amber-50 px-2 py-0.5 rounded-full", children: "Needs attention" })), role === 'agent' && task.status === 'completed' && task.agent_review !== 'approved' && task.agent_review !== 'reopened' && (_jsxs(_Fragment, { children: [_jsx("span", { className: "text-[9px] font-medium text-amber-600 bg-amber-50 px-1.5 py-0.5 rounded", children: "Needs Review" }), _jsx("div", { onClick: (e) => { e.stopPropagation(); on_approve_task?.(task.task_id); }, className: "text-[10px] font-medium text-[#059669] border border-[#059669]/30 bg-green-50 px-2 py-0.5 rounded hover:bg-green-100 cursor-pointer", title: "Approve this task", children: "\u2713 Approve" }), _jsx("div", { onClick: (e) => { e.stopPropagation(); on_reopen_task?.(task.task_id); }, className: "text-[10px] font-medium text-[#EF4444] border border-[#EF4444]/30 bg-red-50 px-2 py-0.5 rounded hover:bg-red-100 cursor-pointer", title: "Reopen this task", children: "Reopen" })] })), role === 'agent' && task.agent_review === 'approved' && (_jsx("span", { className: "text-[9px] font-medium text-[#059669] bg-[#ECFDF5] px-1.5 py-0.5 rounded", children: "\u2713 Approved" })), task.agent_review === 'reopened' && (_jsx("span", { className: "text-[9px] font-medium text-[#EF4444] bg-[#FEF2F2] px-1.5 py-0.5 rounded", children: "Reopened by agent" })), role === 'agent' && on_delete_task && (_jsx("div", { onClick: (e) => { e.stopPropagation(); on_delete_task(task.task_id); }, className: "p-1 text-[#94A3B8] hover:text-[#EF4444] rounded hover:bg-[#FEF2F2] cursor-pointer opacity-0 group-hover/task:opacity-100 transition-opacity", title: "Delete task", 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: 1.5, d: "M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" }) }) })), _jsx("svg", { className: cn('w-4 h-4 text-gray-300 transition-transform flex-shrink-0', expanded && 'rotate-180'), fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: _jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 1.5, d: "M19 9l-7 7-7-7" }) })] }), expanded && (_jsxs("div", { className: "pb-4", children: [task.description && (_jsx("p", { className: "text-[13px] text-gray-500 my-3 leading-relaxed", children: task.description })), task.due_date && _jsxs("p", { className: "text-xs text-amber-600 mb-3", children: ["Due ", format_date(task.due_date)] }), task.coverage_results && task.coverage_results.length > 0 && (_jsx(CoverageCard, { results: task.coverage_results })), task.thread.length > 0 ? (_jsx(ThreadTimeline, { messages: task.thread, role: role, current_user_id: user_id, on_view_file: on_view_file, on_remove_file: on_remove_file, on_accept: on_accept, on_dismiss: on_dismiss, on_send_to_client: on_send_to_client, on_edit_message: (msg_id, updated) => on_edit_message?.(task.task_id, msg_id, updated), on_delete_message: (msg_id) => on_delete_message?.(task.task_id, msg_id), upload_file: upload_file, on_send_back_issues: on_send_back_issues, on_submit_decisions: on_submit_decisions, on_update_content_item: on_update_content_item, task_status: task.status, file_statuses: file_statuses, classification_results: classification_results, validation_results: validation_results, file_manager: file_manager, on_replacement_files: on_replacement_files, view_button_variant: view_button_variant, show_info_icon: show_info_icon, sent_back_resolved_ids: sent_back_resolved_ids, resolving: resolving, available_document_types: available_document_types, on_change_document_type: on_change_document_type, keep_selections: keep_selections, on_keep_change: on_keep_change, on_reply_to_issue: on_reply_to_issue ? (src, fn, rn, text) => on_reply_to_issue(task.task_id, src, fn, rn, text) : undefined })) : (_jsx("p", { className: "text-[13px] text-gray-400 py-6 text-center", children: "No activity yet" })), _jsx(ThreadComposer, { role: role, user_name: user_name, on_send: (text, files) => on_send?.(task.task_id, text, files), on_send_comment: (text) => on_send_comment?.(task.task_id, text), on_upload_files: (files) => on_upload_files?.(task.task_id, files), on_add_question: () => on_add_question?.(task.task_id) })] }))] }));
}
//# sourceMappingURL=task_card.js.map