/**
 * ClarificationGroupCard - Renders multiple ClarificationItems that share the same document references.
 *
 * Layout:
 * ┌──────────────────────────────────────────────────┐
 * │ [Status Badge]  Clarifications: Label1, Label2   │
 * ├──────────────────────────────────────────────────┤
 * │ REFERENCED DOCUMENTS (shared, shown once)        │
 * │ 📄 document.pdf                                  │
 * ├── Per-item ClarificationItemBody ────────────────┤
 * │ (sub-header + issue zone + response zone)        │
 * ├── Per-item ClarificationItemBody ────────────────┤
 * │ (sub-header + issue zone + response zone)        │
 * └──────────────────────────────────────────────────┘
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React, { useCallback, useMemo, useState } from 'react';
import { cn } from '../../utils/cn.js';
import { ClarificationDocReferenceList } from './clarification_doc_reference.js';
import { ClarificationItemBody } from './clarification_item_body.js';
import { ClarificationResponseForm } from './clarification_response_form.js';
import { STATUS_CONFIG, STATUS_BADGE_STYLES, get_worst_status } from './clarification_status_config.js';
export const ClarificationGroupCard = React.memo(function ClarificationGroupCard({ items, read_only = false, on_respond, on_resolve, on_dismiss, on_reopen, on_doc_click, on_doc_view, on_doc_delete, on_field_click, file_manager, hide_submit, submit_label, hide_response_form, on_response_change, compact, collapsible, card_label, file_validation, response_validations, on_validation_click, file_validation_details, file_pipeline_status, class_name, }) {
    const worst_status = get_worst_status(items);
    const status_style = STATUS_BADGE_STYLES[worst_status] ?? STATUS_BADGE_STYLES.pending;
    const worst_config = STATUS_CONFIG[worst_status] ?? STATUS_CONFIG.pending;
    // Build comma-joined unique target labels
    const unique_labels = [];
    const seen_labels = new Set();
    for (const item of items) {
        if (!seen_labels.has(item.target_label)) {
            seen_labels.add(item.target_label);
            unique_labels.push(item.target_label);
        }
    }
    // Use doc_references from first item (all items in group share the same set)
    const shared_doc_references = items[0]?.doc_references ?? [];
    // Handle doc delete: call on_doc_delete for every item that references the file
    const handle_doc_delete = useCallback((file_id) => {
        if (!on_doc_delete)
            return;
        for (const item of items) {
            if (item.doc_references.some(ref => ref.file_id === file_id)) {
                on_doc_delete(item.id, file_id);
            }
        }
    }, [on_doc_delete, items]);
    const all_closed = items.every(i => i.status === 'resolved' || i.status === 'dismissed');
    const is_actionable = items.some(i => i.status === 'pending' || i.status === 'responded');
    // Collapsible state — track which items are manually expanded/collapsed
    const [manual_expanded, set_manual_expanded] = useState({});
    // Track items that just responded — keep expanded until OK is clicked
    const [just_responded, set_just_responded] = useState(new Set());
    const toggle_item = useCallback((id) => {
        set_manual_expanded((prev) => ({ ...prev, [id]: !prev[id] }));
    }, []);
    const dismiss_responded = useCallback((id) => {
        set_just_responded((prev) => { const next = new Set(prev); next.delete(id); return next; });
        set_manual_expanded((prev) => ({ ...prev, [id]: false }));
    }, []);
    const is_item_expanded = useCallback((item) => {
        if (just_responded.has(item.id))
            return true;
        if (item.id in manual_expanded)
            return manual_expanded[item.id];
        // Default: expanded if pending, collapsed if responded/resolved/dismissed
        return item.status === 'pending';
    }, [manual_expanded, just_responded]);
    // Merge response options from all items, deduplicating by value
    const merged_options = useMemo(() => {
        const seen = new Set();
        const options = [];
        for (const item of items) {
            for (const opt of item.response_options ?? []) {
                if (!seen.has(opt.value)) {
                    seen.add(opt.value);
                    options.push(opt);
                }
            }
        }
        return options;
    }, [items]);
    // Shared response handler — applies to all items
    const handle_shared_response = useCallback((response) => {
        if (on_response_change) {
            for (const item of items) {
                on_response_change(item.id, response);
            }
        }
    }, [on_response_change, items]);
    const handle_shared_submit = useCallback((response) => {
        if (on_respond) {
            for (const item of items) {
                on_respond(item.id, response);
            }
        }
    }, [on_respond, items]);
    return (_jsxs("div", { className: cn('cls_clarification_group_card rounded-lg overflow-hidden', collapsible
            ? 'border-2 border-border/60 shadow-sm bg-card'
            : cn('border border-l-[3px]', worst_config.card_border, worst_config.card_left_border), all_closed && 'opacity-70', class_name), children: [card_label && (_jsx("div", { className: "px-4 py-2 border-b bg-amber-50/50 dark:bg-amber-900/10", children: _jsx("span", { className: "text-sm font-semibold text-amber-800 dark:text-amber-400", children: card_label }) })), !compact && (_jsx("div", { className: "flex items-center justify-between gap-2 px-4 py-2.5 bg-muted/20 border-b", children: _jsxs("div", { className: "flex items-center gap-2 min-w-0", children: [_jsx("span", { className: cn('inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium', status_style.bg, status_style.text), children: status_style.label }), _jsxs("span", { className: "text-sm text-muted-foreground truncate", children: ["Clarifications: ", unique_labels.join(', ')] })] }) })), shared_doc_references.length > 0 && (_jsx("div", { className: "px-4 py-3 bg-muted/30 border-b", children: _jsx(ClarificationDocReferenceList, { references: shared_doc_references, on_click: on_doc_click, on_view: on_doc_view, on_delete: on_doc_delete && !all_closed ? handle_doc_delete : undefined, file_validation: file_validation }) })), collapsible ? (_jsx("div", { className: "divide-y", children: items.map((item, index) => {
                    const expanded = is_item_expanded(item);
                    const is_responded = item.status === 'responded' || item.status === 'resolved';
                    const item_config = STATUS_CONFIG[item.status] ?? STATUS_CONFIG.pending;
                    const ItemIcon = item_config.Icon;
                    return (_jsxs("div", { children: [_jsxs("button", { type: "button", onClick: () => toggle_item(item.id), className: cn('w-full flex items-center gap-2.5 px-4 py-3 text-left transition-colors', 'hover:bg-muted/30', is_responded && 'opacity-60'), children: [_jsx("svg", { className: cn('w-3.5 h-3.5 shrink-0 transition-transform text-muted-foreground', !expanded && '-rotate-90'), fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: _jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M19 9l-7 7-7-7" }) }), _jsx(ItemIcon, { className: cn('w-4 h-4 shrink-0', is_responded ? 'text-blue-500' : 'text-red-500') }), _jsx("div", { className: "flex-1 min-w-0", children: _jsxs("span", { className: cn('text-sm font-medium', is_responded ? 'text-muted-foreground' : 'text-foreground'), children: ["Issue ", index + 1] }) }), item.rule_name && (_jsx("span", { className: "text-[10px] text-muted-foreground/60 uppercase tracking-wide shrink-0", children: item.rule_name })), is_responded && (_jsx("span", { className: cn('inline-flex items-center px-1.5 py-0.5 rounded text-[10px] font-semibold shrink-0', item.status === 'responded'
                                            ? 'bg-blue-100 text-blue-700'
                                            : 'bg-green-100 text-green-700'), children: item.status === 'responded' ? 'Responded' : 'Resolved' }))] }), expanded && (_jsxs("div", { className: "px-4 pb-4", children: [_jsx(ClarificationItemBody, { item: item, show_doc_references: false, read_only: read_only, hide_submit: hide_submit, submit_label: submit_label, hide_response_form: hide_response_form, compact: compact, file_manager: file_manager, on_respond: on_respond ? (id, response) => {
                                            on_respond(id, response);
                                            set_just_responded((prev) => new Set(prev).add(id));
                                        } : undefined, on_resolve: on_resolve, on_dismiss: on_dismiss, on_reopen: on_reopen, on_doc_click: on_doc_click, on_doc_view: on_doc_view, on_field_click: on_field_click, on_response_change: on_response_change, file_validation: file_validation, response_validation: response_validations?.[item.id], on_validation_click: on_validation_click, file_validation_details: file_validation_details, file_pipeline_status: file_pipeline_status }), just_responded.has(item.id) && (_jsx("div", { className: "mt-3 flex justify-end", children: _jsx("button", { type: "button", onClick: () => dismiss_responded(item.id), className: "px-4 py-1.5 text-sm font-medium rounded-md bg-primary text-primary-foreground hover:bg-primary/90 transition-colors", children: "OK" }) }))] }))] }, item.id));
                }) })) : (_jsxs(_Fragment, { children: [_jsx("div", { className: "px-4 py-3 space-y-2.5", children: items.map((item, idx) => {
                            const is_responded = item.status === 'responded' || item.status === 'resolved';
                            return (_jsxs("div", { className: "flex items-start gap-3", children: [_jsx("span", { className: cn('flex-shrink-0 w-5 h-5 rounded-full flex items-center justify-center text-[10px] font-bold text-white mt-0.5', is_responded ? 'bg-blue-500' : 'bg-red-500'), children: idx + 1 }), _jsxs("div", { className: "min-w-0 flex-1", children: [!compact && item.rule_name && (_jsx("span", { className: "text-xs font-semibold text-foreground/80 block mb-0.5", children: item.rule_name })), _jsx("p", { className: cn('text-sm leading-relaxed', is_responded ? 'text-muted-foreground' : 'text-red-700'), children: item.issue_description }), item.validation_details && item.validation_details !== item.issue_description && (_jsx("p", { className: "text-xs mt-1 italic leading-relaxed text-muted-foreground/60", children: item.validation_details }))] })] }, item.id));
                        }) }), !hide_response_form && is_actionable && merged_options.length > 0 && (_jsx("div", { className: "px-4 py-3.5", children: _jsxs("div", { className: cn('rounded-lg border p-3.5', worst_config.response_bg, worst_config.response_border), children: [_jsx("div", { className: cn('text-[10px] font-bold uppercase tracking-[0.08em] mb-3', worst_config.response_label_color), children: worst_config.divider_label }), _jsx(ClarificationResponseForm, { action_description: items[0]?.action_description, response_options: merged_options, initial_choice: items[0]?.response_choice, initial_comment: items[0]?.user_comment, initial_files: items[0]?.response_files, read_only: read_only || !is_actionable, on_submit: handle_shared_submit, hide_submit: hide_submit, on_change: handle_shared_response, file_manager: file_manager, radio_accent: worst_config.response_radio_accent, submit_bg: worst_config.submit_bg })] }) }))] }))] }));
});
//# sourceMappingURL=clarification_group_card.js.map