/**
 * Grouped files with tree connector — original + linked replacements/additions.
 * Uses FileBar for consistent rendering of each file row.
 * Supports recursive nesting: each linked file can have its own resolution files.
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { FileBar, derive_validation } from '../shared/file_bar/index.js';
/** Find direct children of a file (blocks that `replaces` the given file_id). */
function find_children(file_id, all_blocks) {
    return all_blocks.filter(b => b.type === 'file' && b.replaces === file_id);
}
/** Derive status icon from classifying/validating/autofilling sets. Validating takes priority over classifying. */
function derive_status(file_id, shared) {
    if (shared.validating_file_ids?.has(file_id))
        return 'validating';
    if (shared.classifying_file_ids?.has(file_id))
        return 'classifying';
    if (shared.autofilling_file_ids?.has(file_id))
        return 'autofilling';
    return null;
}
/** Render a linked file row with its own sub-linked children (recursive). */
function LinkedFileNode({ block, index_in_siblings, sibling_count, depth, shared, }) {
    const att = block.attachment;
    const r_idx = shared.all_blocks.indexOf(block);
    const is_last = index_in_siblings === sibling_count - 1;
    const validation = derive_validation(att.file_id, {
        validating_file_ids: shared.validating_file_ids,
        validated_file_ids: shared.validated_file_ids,
        validation_errors: shared.validation_errors,
        file_response_labels: shared.file_response_labels,
        responded_ids: shared.responded_ids,
    });
    const status = derive_status(att.file_id, shared);
    // Find children of THIS linked file (resolution files, recursive)
    const children = find_children(att.file_id, shared.all_blocks);
    return (_jsxs("div", { children: [_jsxs("div", { className: "flex items-center gap-1", style: { marginLeft: `${depth * 16}px` }, children: [_jsx("div", { className: "w-3 flex-shrink-0 flex items-center justify-end", children: _jsx("svg", { className: "w-3 h-4 text-violet-300/50", viewBox: "0 0 12 16", fill: "none", children: _jsx("path", { d: is_last && children.length === 0 ? "M6 0 V8 H12" : "M6 0 V16 M6 8 H12", stroke: "currentColor", strokeWidth: "1.5" }) }) }), _jsx(FileBar, { attachment: att, status_icon: status, validation: validation, on_validation_click: validation?.state === 'passed' ? shared.on_validated_click : shared.on_issue_click, classification_tags: shared.classification_tags?.[att.file_id], classification_meta: shared.classification_metadata?.[att.file_id], on_download: shared.on_download, on_view: shared.on_view, on_remove: !shared.disabled && r_idx >= 0 && shared.on_remove ? () => shared.on_remove(r_idx) : undefined, disabled: shared.disabled, class_name: "flex-1 min-w-0" })] }), children.map((child, ci) => (_jsx(LinkedFileNode, { block: child, index_in_siblings: ci, sibling_count: children.length, depth: depth + 1, shared: shared }, `child_${ci}`)))] }, `linked_d${depth}_${index_in_siblings}`));
}
export function FileGroup({ original_block, original_index, linked_blocks, all_blocks, is_replaced, disabled, classifying_file_ids, autofilling_file_ids, validating_file_ids, validation_errors, validated_file_ids, file_response_labels, classification_tags, classification_metadata, on_download, on_remove, on_view, on_issue_click, on_validated_click, responded_ids, }) {
    const orig_file_id = original_block.attachment.file_id;
    const orig_validation = is_replaced ? null : derive_validation(orig_file_id, {
        validating_file_ids, validated_file_ids, validation_errors, file_response_labels, responded_ids,
    });
    const orig_status = derive_status(orig_file_id, { all_blocks, classifying_file_ids, autofilling_file_ids });
    const shared = {
        all_blocks, disabled, classifying_file_ids, autofilling_file_ids, validating_file_ids,
        validation_errors, validated_file_ids, file_response_labels,
        classification_tags, classification_metadata,
        on_download, on_remove, on_view, on_issue_click, on_validated_click, responded_ids,
    };
    return (_jsx("div", { className: "col-span-1 rounded-lg border border-violet-200/50 bg-white/40 p-1.5", children: _jsxs("div", { className: "flex items-stretch gap-0", children: [_jsx("div", { className: "flex flex-col items-center w-5 flex-shrink-0 pt-3 pb-1", children: _jsx("div", { className: "w-px flex-1 bg-violet-300/50" }) }), _jsxs("div", { className: "flex-1 min-w-0 space-y-1", children: [_jsxs("div", { className: "flex items-center gap-1", children: [_jsx("div", { className: "w-3 h-px bg-violet-300/50 flex-shrink-0" }), _jsx(FileBar, { attachment: original_block.attachment, muted: is_replaced, status_icon: orig_status, validation: orig_validation, on_validation_click: orig_validation?.state === 'passed' ? on_validated_click : on_issue_click, classification_tags: classification_tags?.[orig_file_id], classification_meta: classification_metadata?.[orig_file_id], on_download: on_download, on_view: on_view, disabled: disabled, class_name: "flex-1 min-w-0" })] }), linked_blocks.map((rb, ri) => (_jsx(LinkedFileNode, { block: rb, index_in_siblings: ri, sibling_count: linked_blocks.length, depth: 1, shared: shared }, `linked_${ri}`)))] })] }) }));
}
//# sourceMappingURL=file_group.js.map