/**
 * ClarificationCard - Renders a single clarification item.
 *
 * Layout:
 * ┌─────────────────────────────────────────┐
 * │ [Source Label bar]                      │
 * ├─────────────────────────────────────────┤
 * │ [Status Icon] [Badge] [Tags] [Actions] │
 * │               {target_label}           │
 * ├─────────────────────────────────────────┤
 * │ ClarificationItemBody (issue + response)│
 * └─────────────────────────────────────────┘
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React, { useState, useCallback } from 'react';
import { cn } from '../../utils/cn.js';
import { ClarificationItemBody } from './clarification_item_body.js';
import { STATUS_CONFIG } from './clarification_status_config.js';
import { CollapsibleCard } from '../shared/collapsible_card/index.js';
export const ClarificationCard = React.memo(function ClarificationCard({ item, 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, source_label, file_classification_tags, file_validation, response_validation, on_validation_click, file_validation_details, file_pipeline_status, compact, collapsible, card_label, class_name, }) {
    const config = STATUS_CONFIG[item.status] ?? STATUS_CONFIG.pending;
    const is_closed = item.status === 'resolved' || item.status === 'dismissed';
    const is_responded = item.status === 'responded' || is_closed;
    const StatusIcon = config.Icon;
    // Collapsible state — default: expanded if pending, collapsed if responded/resolved
    const [manual_expanded, set_manual_expanded] = useState(null);
    const [just_responded, set_just_responded] = useState(false);
    // Track initial status to detect when it changes from pending to responded
    const [initial_status] = useState(item.status);
    const was_pending = initial_status === 'pending';
    // Keep expanded if user just responded (via on_respond) OR if item transitioned from pending to responded in this session
    const status_changed_to_responded = was_pending && is_responded && manual_expanded === null;
    const show_ok = just_responded || status_changed_to_responded;
    const expanded = show_ok || (manual_expanded ?? (item.status === 'pending'));
    const toggle = useCallback(() => set_manual_expanded((prev) => prev === null ? !expanded : !prev), [expanded]);
    const dismiss_responded = useCallback(() => {
        set_just_responded(false);
        set_manual_expanded(false);
    }, []);
    // Collapsible mode — accordion style using shared CollapsibleCard
    if (collapsible) {
        // Badge: "Action Required" for pending items with thread (resent), standard badges otherwise
        const has_thread = item.thread && item.thread.length > 0;
        const is_resent_pending = has_thread && item.status === 'pending';
        const status_badge = is_resent_pending ? (_jsx("span", { className: "inline-flex items-center px-1.5 py-0.5 rounded text-[10px] font-semibold shrink-0 bg-amber-100 text-amber-700", children: "Action Required" })) : 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'
                : item.status === 'resolved'
                    ? 'bg-green-100 text-green-700'
                    : 'bg-gray-100 text-gray-600'), children: item.status === 'responded' ? 'Responded' : item.status === 'resolved' ? 'Resolved' : 'Dismissed' })) : undefined;
        return (_jsxs("div", { className: "cls_clarification_card", "data-clarification-id": item.id, "data-clarification-field": item.target_field_id, children: [card_label && (_jsx("div", { className: "px-4 py-2 border-b bg-amber-50/50 dark:bg-amber-900/10 rounded-t-lg border-2 border-b-0 border-border/60", children: _jsx("span", { className: "text-sm font-semibold text-amber-800 dark:text-amber-400", children: card_label }) })), _jsx(CollapsibleCard, { expanded: expanded, on_expanded_change: (next) => set_manual_expanded(next), icon: _jsx(StatusIcon, { className: cn('w-4 h-4 shrink-0', is_responded ? 'text-blue-500' : 'text-red-500') }), title: item.issue_description || 'Issue', title_class_name: is_responded ? 'text-muted-foreground' : 'text-foreground', tag: item.rule_name, trailing: status_badge, show_dismiss_button: show_ok, on_dismiss: dismiss_responded, muted: is_responded, dimmed: is_closed, class_name: cn(card_label && 'rounded-t-none border-t-0', class_name), children: _jsx(ClarificationItemBody, { item: item, show_doc_references: true, 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(true);
                        } : 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_doc_delete: on_doc_delete, on_field_click: on_field_click, on_response_change: on_response_change, file_validation: file_validation, response_validation: response_validation, on_validation_click: on_validation_click, file_validation_details: file_validation_details, file_pipeline_status: file_pipeline_status }) })] }));
    }
    // Default (non-collapsible) mode — resolved/responded items render as accordion (collapsed by default)
    const [body_expanded, set_body_expanded] = useState(!is_responded);
    return (_jsxs("div", { className: cn('cls_clarification_card rounded-lg overflow-hidden border border-l-[3px] transition-colors', config.card_border, config.card_left_border, class_name), "data-clarification-id": item.id, "data-clarification-field": item.target_field_id, 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 && source_label && (_jsxs("div", { className: "px-4 py-1.5 bg-muted/10 border-b text-xs text-muted-foreground", children: ["Original question: ", _jsx("span", { className: "font-medium text-foreground", children: source_label })] })), is_responded ? (_jsxs(_Fragment, { children: [_jsxs("button", { type: "button", onClick: () => set_body_expanded(prev => !prev), className: "w-full flex items-center gap-2 px-4 py-2.5 text-left hover:bg-muted/30 transition-colors", children: [_jsx("span", { className: "text-muted-foreground select-none text-xs", children: body_expanded ? '▼' : '▶' }), _jsx(StatusIcon, { className: cn('h-4 w-4 flex-shrink-0', config.icon_color) }), _jsxs("span", { className: cn('text-xs font-semibold uppercase tracking-wide', config.text), children: [item.status === 'resolved' ? 'Issue Resolved' : 'Issue (Responded)', " \u00B7 ", item.rule_name ?? 'Clarification'] })] }), body_expanded && (_jsx(ClarificationItemBody, { item: item, show_doc_references: true, read_only: read_only, hide_submit: hide_submit, hide_response_form: hide_response_form, compact: compact, file_manager: file_manager, on_respond: on_respond, 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_doc_delete: on_doc_delete, on_field_click: on_field_click, on_response_change: on_response_change, file_validation: file_validation, response_validation: response_validation, on_validation_click: on_validation_click, file_validation_details: file_validation_details, file_pipeline_status: file_pipeline_status }))] })) : (_jsx(ClarificationItemBody, { item: item, show_doc_references: true, read_only: read_only, hide_submit: hide_submit, hide_response_form: hide_response_form, compact: compact, file_manager: file_manager, on_respond: on_respond, 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_doc_delete: on_doc_delete, on_field_click: on_field_click, on_response_change: on_response_change, file_validation: file_validation, response_validation: response_validation, on_validation_click: on_validation_click, file_validation_details: file_validation_details, file_pipeline_status: file_pipeline_status }))] }));
});
//# sourceMappingURL=clarification_card.js.map