/**
 * ClarificationSection - Renders as a form section after regular sections.
 *
 * Groups clarification items by status: pending first, then responded, then resolved/dismissed.
 * Dismissed/resolved items stay visible with their status badge as the decision label.
 * Only visible when enable_clarifications=true AND clarifications exist.
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useMemo } from 'react';
import { IoAlertCircle } from 'react-icons/io5';
import { cn } from '../../utils/cn.js';
import { ClarificationCard } from './clarification_card.js';
import { ClarificationGroupCard } from './clarification_group_card.js';
/** Status sort order: pending → responded → resolved → dismissed */
const STATUS_ORDER = {
    pending: 0,
    responded: 1,
    resolved: 2,
    dismissed: 3,
};
/**
 * Group items that share identical doc reference sets.
 * Items with 0 doc_references stay standalone.
 * Items with a unique doc set stay standalone.
 * Items sharing the same doc set (2+) are grouped into an array.
 */
export function group_by_document(items) {
    const key_map = new Map();
    const standalone = [];
    for (const item of items) {
        if (item.doc_references.length === 0) {
            standalone.push(item);
            continue;
        }
        const key = item.doc_references
            .map(r => r.file_id)
            .sort()
            .join(',');
        const existing = key_map.get(key);
        if (existing) {
            existing.push(item);
        }
        else {
            key_map.set(key, [item]);
        }
    }
    const entries = [];
    for (const item of standalone) {
        entries.push(item);
    }
    for (const group of key_map.values()) {
        if (group.length === 1) {
            entries.push(group[0]);
        }
        else {
            entries.push(group);
        }
    }
    return entries;
}
export const ClarificationSection = React.memo(function ClarificationSection({ items, counts, read_only = false, callbacks, on_doc_click, on_doc_view, on_field_click, file_manager, hide_submit, submit_label, hide_response_form, on_response_change, hide_header, compact, file_validation, response_validations, on_validation_click, file_validation_details, preserve_order, collapsible, card_label, file_pipeline_status, class_name, }) {
    // Don't render if no clarifications
    if (items.length === 0)
        return null;
    // Group by doc, optionally sort by status (pending first)
    const all_entries = useMemo(() => {
        if (preserve_order) {
            return group_by_document(items);
        }
        const sorted = [...items].sort((a, b) => (STATUS_ORDER[a.status] ?? 9) - (STATUS_ORDER[b.status] ?? 9));
        return group_by_document(sorted);
    }, [items, preserve_order]);
    // Build header label
    const header_label = counts.pending > 0
        ? `Clarifications (${counts.pending} pending)`
        : `Clarifications (${counts.total})`;
    return (_jsxs("div", { className: cn('cls_clarification_section rounded-lg overflow-hidden', class_name), children: [!hide_header && !compact && (_jsxs("div", { className: "flex items-center gap-3 p-4 border-b bg-amber-50/50 dark:bg-amber-900/10", children: [_jsx("div", { className: "flex items-center justify-center w-10 h-10 rounded-full bg-amber-100 dark:bg-amber-900/30", children: _jsx(IoAlertCircle, { className: "w-5 h-5 text-amber-600 dark:text-amber-400" }) }), _jsxs("div", { children: [_jsx("h3", { className: "font-semibold text-lg text-foreground", children: header_label }), counts.responded > 0 && (_jsxs("p", { className: "text-xs text-muted-foreground", children: [counts.responded, " responded, awaiting review"] }))] })] })), _jsx("div", { className: "p-4 space-y-4", children: all_entries.map((entry) => {
                    const is_closed = Array.isArray(entry)
                        ? entry.every(i => i.status === 'resolved' || i.status === 'dismissed')
                        : entry.status === 'resolved' || entry.status === 'dismissed';
                    return Array.isArray(entry) ? (_jsx(ClarificationGroupCard, { items: entry, read_only: read_only || is_closed, on_respond: callbacks?.on_respond, on_resolve: callbacks?.on_resolve, on_dismiss: callbacks?.on_dismiss, on_reopen: callbacks?.on_reopen, on_doc_click: on_doc_click, on_doc_view: on_doc_view, on_doc_delete: callbacks?.on_doc_delete, on_field_click: on_field_click, file_manager: file_manager, hide_submit: hide_submit, submit_label: submit_label, hide_response_form: hide_response_form, on_response_change: on_response_change, compact: compact, collapsible: collapsible, card_label: card_label, file_validation: file_validation, response_validations: response_validations, on_validation_click: on_validation_click, file_validation_details: file_validation_details, file_pipeline_status: file_pipeline_status }, entry.map(i => i.id).join('+'))) : (_jsx(ClarificationCard, { item: entry, read_only: read_only || is_closed, on_respond: callbacks?.on_respond, on_resolve: callbacks?.on_resolve, on_dismiss: callbacks?.on_dismiss, on_reopen: callbacks?.on_reopen, on_doc_click: on_doc_click, on_doc_view: on_doc_view, on_doc_delete: callbacks?.on_doc_delete, on_field_click: on_field_click, file_manager: file_manager, hide_submit: hide_submit, submit_label: submit_label, hide_response_form: hide_response_form, on_response_change: on_response_change, compact: compact, collapsible: collapsible, card_label: card_label, file_validation: file_validation, response_validation: response_validations?.[entry.id], on_validation_click: on_validation_click, file_validation_details: file_validation_details, file_pipeline_status: file_pipeline_status }, entry.id));
                }) })] }));
});
//# sourceMappingURL=clarification_section.js.map