/**
 * FileBar — Reusable file row with consistent columnar layout.
 *
 * Layout: [FileIcon] [Filename...] [FileSize] [InfoBtn] [ClassTag] [ViewIcon] [ValidationIcon] [RemoveIcon(hover)]
 *
 * Below the main row (when active):
 *   ⟳ Status text...   (small italic during LLM calls)
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from 'react';
import { cn } from '../../../utils/cn.js';
import { format_file_size, get_file_icon } from '../../hazo_collab_form_file_textbox/utils.js';
import { FileBarValidation } from './file_bar_validation.js';
import { FileBarTag } from './file_bar_tag.js';
import { FileBarValidationDialog } from './file_bar_validation_dialog.js';
import { HiX, HiEye, HiClock, HiCheckCircle, HiXCircle } from 'react-icons/hi';
import { IoInformationCircle } from 'react-icons/io5';
/** Derive validation props from raw clarification data (convenience helper). */
export function derive_validation(file_id, opts) {
    const { validating_file_ids, validated_file_ids, validation_errors, file_response_labels, responded_ids } = opts;
    if (validating_file_ids?.has(file_id))
        return { state: 'validating' };
    const errors = validation_errors?.[file_id];
    if (errors?.length) {
        const response_label = file_response_labels?.[file_id];
        if (response_label) {
            return { state: 'resolved', issue_count: errors.length };
        }
        // Count only unresolved errors when we have individual responded IDs
        const pending_count = responded_ids
            ? errors.filter(e => !responded_ids.has(e.id)).length
            : errors.length;
        const responded_count = errors.length - pending_count;
        if (pending_count === 0) {
            return { state: 'resolved', issue_count: errors.length };
        }
        if (responded_count > 0) {
            // Partial: show only pending issues count
            return { state: 'issues', issue_count: pending_count };
        }
        return { state: 'issues', issue_count: errors.length };
    }
    if (validated_file_ids?.has(file_id))
        return { state: 'passed' };
    return null;
}
export function FileBar({ attachment, muted, status_icon, validation, on_validation_click, validation_details, validation_responded_map, pipeline_status_text, max_width, classification_tags, classification_meta, on_download, on_view, on_remove, on_classify, on_send_back_clarification, on_accept_rule, on_accept_all_rules, on_view_file, disabled, class_name, info_validations, }) {
    const Icon = get_file_icon(attachment.mime_type);
    const [validation_dialog_open, set_validation_dialog_open] = useState(false);
    const [info_panel_open, set_info_panel_open] = useState(false);
    const render_icon = () => {
        if (status_icon === 'autofilling') {
            return (_jsxs("svg", { className: "h-3.5 w-3.5 flex-shrink-0 text-emerald-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" })] }));
        }
        if (status_icon === 'queued') {
            return _jsx(HiClock, { className: "h-3.5 w-3.5 flex-shrink-0 text-muted-foreground animate-pulse", title: "Queued" });
        }
        return _jsx(Icon, { className: cn('h-3.5 w-3.5 flex-shrink-0', muted ? 'text-muted-foreground/30' : 'text-violet-400') });
    };
    const tag_count = classification_tags?.length ?? 0;
    const tags_below = tag_count > 1;
    // Determine validation click handler: built-in dialog takes priority over callback
    const handle_validation_click = validation_details
        ? () => set_validation_dialog_open(true)
        : on_validation_click
            ? () => on_validation_click(attachment.file_id)
            : undefined;
    // Auto-derive status text from status_icon when pipeline_status_text is not explicitly set
    const effective_status_text = pipeline_status_text
        ?? (status_icon === 'classifying' ? 'Classifying document...'
            : status_icon === 'validating' ? 'Validating document...'
                : status_icon === 'autofilling' ? 'Autofilling fields...'
                    : status_icon === 'queued' ? 'Queued for processing...'
                        : null);
    const width_class = max_width ?? 'max-w-[400px]';
    return (_jsxs("div", { className: cn('group/chip rounded-md border shadow-sm transition-colors', width_class, (status_icon === 'classifying' || status_icon === 'validating')
            ? 'bg-orange-50 border-orange-200/70'
            : status_icon === 'autofilling'
                ? 'bg-emerald-50 border-emerald-200/70'
                : muted
                    ? 'bg-muted/30 border-muted-foreground/15'
                    : 'bg-white border-violet-200/70 hover:border-violet-300', class_name), children: [_jsxs("div", { className: "flex items-center gap-1.5 px-2 py-1.5", children: [render_icon(), _jsx("button", { type: "button", onClick: () => on_download?.(attachment), className: cn('text-xs truncate min-w-0 flex-1 text-left', muted
                            ? 'line-through text-muted-foreground/40 decoration-muted-foreground/30'
                            : 'text-foreground/70 hover:text-primary hover:underline'), title: attachment.file_name, children: attachment.file_name }), _jsx("span", { className: cn('text-[10px] flex-shrink-0 tabular-nums', muted ? 'text-muted-foreground/25' : 'text-muted-foreground/40'), children: format_file_size(attachment.file_size) }), !tags_below && _jsx(FileBarTag, { tags: classification_tags, meta: classification_meta }), on_view && (_jsx("button", { type: "button", onClick: () => on_view(attachment), className: "p-0.5 rounded text-muted-foreground/40 hover:text-primary transition-colors flex-shrink-0", title: "View", children: _jsx(HiEye, { className: "h-3 w-3" }) })), info_validations && info_validations.length > 0 && (_jsx("button", { type: "button", onClick: () => set_info_panel_open(p => !p), className: cn('p-0.5 rounded transition-colors flex-shrink-0', info_panel_open ? 'text-blue-500' : 'text-muted-foreground/40 hover:text-blue-500'), title: "Validation details", children: _jsx(IoInformationCircle, { className: "h-3.5 w-3.5" }) })), validation && (_jsx(FileBarValidation, { state: validation.state, issue_count: validation.issue_count, on_click: handle_validation_click })), !disabled && on_remove && (_jsx("button", { type: "button", onClick: on_remove, className: "p-0.5 rounded text-muted-foreground/40 hover:text-destructive transition-colors opacity-0 group-hover/chip:opacity-100 flex-shrink-0", children: _jsx(HiX, { className: "h-3 w-3" }) }))] }), tags_below && (_jsx("div", { className: "px-2 pb-1.5 -mt-0.5", children: _jsx(FileBarTag, { tags: classification_tags, meta: classification_meta }) })), effective_status_text && (_jsxs("div", { className: "px-2 pb-1.5 -mt-0.5 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-[10px] italic text-muted-foreground/50", children: effective_status_text })] })), info_validations && info_validations.length > 0 && info_panel_open && (_jsx("div", { className: "px-2 pb-2 space-y-1.5", children: info_validations.map((v, i) => (_jsxs("div", { className: "flex items-start gap-1.5 text-[11px]", children: [v.status === 'passed' ? (_jsx(HiCheckCircle, { className: "h-3 w-3 text-green-500 flex-shrink-0 mt-0.5" })) : (_jsx(HiXCircle, { className: "h-3 w-3 text-red-500 flex-shrink-0 mt-0.5" })), _jsxs("div", { className: "min-w-0", children: [_jsx("span", { className: "font-medium uppercase tracking-wide text-muted-foreground", children: v.rule_key.replace(/_/g, ' ') }), _jsxs("span", { className: cn('ml-1', v.status === 'passed' ? 'text-muted-foreground' : 'text-foreground'), children: ["\u2014 ", v.details] })] })] }, i))) })), validation_details && (_jsx(FileBarValidationDialog, { open: validation_dialog_open, on_open_change: set_validation_dialog_open, details: validation_details, responded_map: validation_responded_map, on_send_back_clarification: on_send_back_clarification, on_accept_rule: on_accept_rule, on_accept_all_rules: on_accept_all_rules, file_info: {
                    file_id: attachment.file_id,
                    file_name: attachment.file_name,
                    file_size: attachment.file_size,
                    classification: classification_tags?.[0]?.label,
                }, on_view_file: on_view_file }))] }));
}
//# sourceMappingURL=file_bar.js.map