/**
 * KeyInfoDrawer — collapsible region above the ThreadForm task list that
 * embeds <HazoCollabFormView mode="edit"> with H.7 confirm-state plumbing.
 *
 * Renders nothing when `sections` is omitted (so consumers that don't use
 * the drawer get zero overhead).
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { forwardRef, useCallback, useImperativeHandle, useMemo, useRef, useState, } from 'react';
import { ChevronDown, ChevronRight } from 'lucide-react';
import { cn } from './shared.js';
import { HazoCollabFormView } from '../../hazo_collab_form_view/index.js';
/** Tally how many fields are in each confirm_state. */
function count_states(field_metadata) {
    const counts = { prefilled: 0, edited: 0, confirmed: 0 };
    if (!field_metadata)
        return counts;
    for (const meta of Object.values(field_metadata)) {
        const state = meta?.confirm_state;
        if (state === 'prefilled')
            counts.prefilled++;
        else if (state === 'edited')
            counts.edited++;
        else if (state === 'confirmed')
            counts.confirmed++;
    }
    return counts;
}
export const KeyInfoDrawer = forwardRef(function KeyInfoDrawer({ sections, form_data, on_form_data_change, field_metadata, on_metadata_change, show_confirm_action, title = 'Key Information', aria_label, chevron_position = 'right', default_expanded = false, field_action_components, enable_field_audit, audit_debounce_ms, field_columns, highlight_empty_required, disable_section_card, hide_section_header, }, ref) {
    const [expanded, set_expanded] = useState(default_expanded);
    const view_ref = useRef(null);
    // Keep a stable ref to the latest field_metadata and on_metadata_change so
    // confirm_all_prefilled works even when the drawer is collapsed (view_ref is null).
    const field_metadata_ref = useRef(field_metadata);
    field_metadata_ref.current = field_metadata;
    const on_metadata_change_ref = useRef(on_metadata_change);
    on_metadata_change_ref.current = on_metadata_change;
    useImperativeHandle(ref, () => ({
        confirm_all_prefilled: () => {
            // Prefer delegating to the embedded view when it is mounted.
            if (view_ref.current) {
                return view_ref.current.confirm_all_prefilled();
            }
            // Fallback: apply the transition directly using the latest prop values.
            const ids = [];
            const meta = field_metadata_ref.current ?? {};
            for (const [field_id, entry] of Object.entries(meta)) {
                if (entry?.confirm_state === 'prefilled') {
                    ids.push(field_id);
                    on_metadata_change_ref.current?.({
                        field_id,
                        changes: { confirm_state: 'confirmed' },
                    });
                }
            }
            return ids;
        },
    }), []);
    const counts = useMemo(() => count_states(field_metadata), [field_metadata]);
    const handle_toggle = useCallback(() => {
        set_expanded((prev) => !prev);
    }, []);
    if (!sections)
        return null;
    const summary_parts = [];
    if (counts.prefilled > 0)
        summary_parts.push(`${counts.prefilled} prefilled`);
    if (counts.edited > 0)
        summary_parts.push(`${counts.edited} edited`);
    if (counts.confirmed > 0)
        summary_parts.push(`${counts.confirmed} confirmed`);
    return (_jsxs("div", { className: "cls_thread_form_key_info_drawer border border-[#E2E8F0] rounded-md mb-3 bg-white", children: [_jsxs("button", { type: "button", onClick: handle_toggle, "aria-expanded": expanded, "aria-label": aria_label ?? (typeof title === 'string' ? title : 'Key information'), className: cn('w-full flex items-center justify-between px-4 py-2 text-left', 'text-[13px] hover:bg-[#F8FAFC] transition-colors cursor-pointer'), children: [_jsxs("span", { className: "flex items-center gap-2", children: [chevron_position === 'left' && (expanded
                                ? _jsx(ChevronDown, { className: "w-4 h-4 text-[#64748B]" })
                                : _jsx(ChevronRight, { className: "w-4 h-4 text-[#64748B]" })), _jsx("span", { className: "font-medium text-[#0F172A]", children: title })] }), _jsxs("span", { className: "flex items-center gap-3", children: [summary_parts.length > 0 && (_jsx("span", { className: "text-[12px] text-[#64748B]", children: summary_parts.join(' • ') })), chevron_position === 'right' && (expanded
                                ? _jsx(ChevronDown, { className: "w-4 h-4 text-[#64748B]" })
                                : _jsx(ChevronRight, { className: "w-4 h-4 text-[#64748B]" }))] })] }), expanded && (_jsx("div", { className: "cls_thread_form_key_info_body border-t border-[#E2E8F0] p-4", children: _jsx(HazoCollabFormView, { ref: view_ref, mode: "edit", sections: sections, form_data: form_data, field_metadata: field_metadata, on_form_data_change: on_form_data_change, on_metadata_change: on_metadata_change, show_confirm_action: show_confirm_action, field_action_components: field_action_components, enable_field_audit: enable_field_audit, audit_debounce_ms: audit_debounce_ms, field_columns: field_columns, highlight_empty_required: highlight_empty_required, disable_section_card: disable_section_card, hide_section_header: hide_section_header }) }))] }));
});
//# sourceMappingURL=key_info_drawer.js.map