/**
 * Clarification conversation thread timeline.
 * Displays the history of actions (system issue, client response, author rejection)
 * as a vertical timeline styled by role.
 *
 * All entries except the last are collapsed by default — click to expand.
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from 'react';
import { cn } from '../../utils/cn.js';
import { IoAlertCircle } from 'react-icons/io5';
import { HiUser, HiShieldCheck, HiDocumentText, HiChevronDown, HiChevronRight } from 'react-icons/hi';
import { FileBar } from '../shared/file_bar/index.js';
const role_config = {
    system: {
        bg: 'bg-muted/50',
        border: 'border-border/60',
        icon_bg: 'bg-muted',
        icon: IoAlertCircle,
        icon_color: 'text-muted-foreground',
        label: 'System',
        label_color: 'text-muted-foreground',
        message_color: 'text-foreground',
    },
    client: {
        bg: 'bg-blue-50',
        border: 'border-blue-200',
        icon_bg: 'bg-blue-100',
        icon: HiUser,
        icon_color: 'text-blue-600',
        label: 'Client',
        label_color: 'text-blue-600',
        message_color: 'text-blue-900',
    },
    author: {
        bg: 'bg-amber-50',
        border: 'border-amber-200',
        icon_bg: 'bg-amber-100',
        icon: HiShieldCheck,
        icon_color: 'text-amber-600',
        label: 'Back Office',
        label_color: 'text-amber-700',
        message_color: 'text-amber-900',
    },
};
const action_labels = {
    created: 'Issue found',
    responded: 'Responded',
    rejected: 'Rejected',
    resolved: 'Resolved',
    reopened: 'Reopened',
};
function format_timestamp(ts, locale = 'en-AU') {
    try {
        const d = new Date(ts);
        return d.toLocaleDateString(locale, { day: 'numeric', month: 'short', year: 'numeric' })
            + ' at '
            + d.toLocaleTimeString(locale, { hour: 'numeric', minute: '2-digit', hour12: true });
    }
    catch {
        return ts;
    }
}
export function ClarificationThread({ thread, response_options, on_file_view, locale = 'en-AU', className }) {
    if (!thread || thread.length === 0)
        return null;
    // Split: all entries except the last go into a collapsible "Previous" group
    const previous_entries = thread.slice(0, -1);
    const last_entry = thread[thread.length - 1];
    const [show_previous, set_show_previous] = useState(false);
    return (_jsxs("div", { className: cn('space-y-2', className), children: [_jsx("p", { className: "text-xs font-medium text-muted-foreground uppercase tracking-wide mb-2", children: "History" }), _jsxs("div", { className: "relative space-y-0", children: [_jsx("div", { className: "absolute left-4 top-3 bottom-3 w-px bg-border/60" }), previous_entries.length > 0 && (_jsxs("div", { className: "relative pb-3", children: [_jsxs("button", { type: "button", onClick: () => set_show_previous(p => !p), className: "relative flex gap-3 items-center w-full text-left cursor-pointer hover:bg-muted/30 rounded-md transition-colors py-1", children: [_jsx("div", { className: "relative z-10 flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-full bg-muted", children: show_previous
                                            ? _jsx(HiChevronDown, { className: "h-4 w-4 text-muted-foreground" })
                                            : _jsx(HiChevronRight, { className: "h-4 w-4 text-muted-foreground" }) }), _jsx("span", { className: "text-xs font-medium text-muted-foreground hover:text-foreground transition-colors", children: show_previous ? 'Hide previous' : `Show ${previous_entries.length} previous` })] }), _jsx("div", { className: "flex-1 min-w-0 pl-11", children: show_previous && (_jsx("div", { className: "space-y-2 mt-1", children: previous_entries.map((entry, idx) => (_jsx(ThreadEntry, { entry: entry, response_options: response_options, on_file_view: on_file_view, locale: locale }, idx))) })) })] })), last_entry && (_jsx(ThreadEntry, { entry: last_entry, response_options: response_options, on_file_view: on_file_view, locale: locale }))] })] }));
}
/** Single thread entry renderer */
function ThreadEntry({ entry, response_options, on_file_view, locale = 'en-AU' }) {
    const config = role_config[entry.role] ?? role_config.system;
    const Icon = config.icon;
    const is_input_entry = entry.role === 'system' && entry.action === 'created' && entry.files?.length && !entry.message;
    const action_label = is_input_entry ? 'Input' : (action_labels[entry.action] ?? entry.action);
    const actor = is_input_entry ? 'Input' : (entry.actor_name ?? config.label);
    const EntryIcon = is_input_entry ? HiDocumentText : Icon;
    return (_jsxs("div", { className: "relative flex gap-3 pb-3 last:pb-0", children: [_jsx("div", { className: cn('relative z-10 flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-full', config.icon_bg), children: _jsx(EntryIcon, { className: cn('h-4 w-4', config.icon_color) }) }), _jsxs("div", { className: cn('flex-1 rounded-md border px-3 py-2 min-w-0', config.bg, config.border), children: [_jsxs("div", { className: "flex items-center gap-2 mb-0.5", children: [_jsx("span", { className: cn('text-xs font-semibold uppercase tracking-wide', config.label_color), children: actor }), _jsxs("span", { className: "text-xs text-muted-foreground", children: ["\u00B7 ", action_label] }), _jsx("span", { className: "text-xs text-muted-foreground ml-auto flex-shrink-0", children: format_timestamp(entry.timestamp, locale) })] }), entry.response_choice && (_jsxs("p", { className: cn('text-sm font-medium', config.message_color), children: ["Selected: ", response_options?.find(o => o.value === entry.response_choice)?.label ?? entry.response_choice] })), entry.message && (_jsx("p", { className: cn('text-sm', config.message_color), children: entry.message })), entry.files && entry.files.length > 0 && (_jsx("div", { className: "mt-1.5 space-y-1", children: entry.files.map((f) => (_jsx(FileBar, { attachment: {
                                file_id: f.file_id,
                                ref_id: f.file_id,
                                file_name: f.file_name,
                                file_size: f.file_size ?? 0,
                                mime_type: f.mime_type ?? 'application/octet-stream',
                                visibility: 'public',
                                attached_at: '',
                            }, on_view: on_file_view ? () => on_file_view(f.file_id, f.file_name) : undefined, max_width: "max-w-full", info_validations: entry.validations, classification_tags: entry.classification_tags?.map(t => ({ id: t, label: t })) }, f.file_id))) }))] })] }));
}
//# sourceMappingURL=clarification_thread.js.map