/**
 * ClarificationDialog - Modal dialog showing clarifications for a specific field.
 *
 * Opens when clicking the red badge on a field/group.
 * Uses built-in Dialog/DialogContent for proper portal/overlay behavior.
 * When "View" is clicked on a referenced document, expands to show a side panel
 * with the PDF viewer alongside the clarification cards.
 */
'use client';
import { jsxs as _jsxs, jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import React, { useState, useCallback, useEffect, useMemo } from 'react';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, } from '../shared/dialog/index.js';
import { IoClose } from 'react-icons/io5';
import { optional_import } from '../../utils/optional_import.js';
import { ClarificationCard } from './clarification_card.js';
import { ClarificationGroupCard } from './clarification_group_card.js';
import { group_by_document } from './clarification_section.js';
export const ClarificationDialog = React.memo(function ClarificationDialog({ open, on_open_change, field_label, items, read_only = false, callbacks, on_doc_click, get_download_url, on_doc_delete, }) {
    const pending_count = items.filter(i => i.status === 'pending').length;
    const resolved_on_doc_delete = on_doc_delete ?? callbacks?.on_doc_delete;
    const grouped_entries = useMemo(() => group_by_document(items), [items]);
    // PDF viewer state
    const [preview_file, set_preview_file] = useState(null);
    const [PdfViewer, set_pdf_viewer] = useState(null);
    // Load PdfViewer from hazo_pdf (optional peer dependency)
    // webpackIgnore comment prevents Webpack from trying to resolve the optional module
    useEffect(() => {
        if (!get_download_url)
            return;
        let cancelled = false;
        optional_import('hazo_pdf').then((mod) => {
            if (!cancelled && mod.PdfViewer) {
                set_pdf_viewer(() => mod.PdfViewer);
            }
        }).catch(() => { });
        return () => { cancelled = true; };
    }, [get_download_url]);
    // Handle "View" button click on a doc reference
    const handle_doc_view = useCallback((file_id, file_name) => {
        if (!get_download_url)
            return;
        set_preview_file({ file_id, file_name });
    }, [get_download_url]);
    const close_preview = useCallback(() => {
        set_preview_file(null);
    }, []);
    // Reset preview when dialog closes
    useEffect(() => {
        if (!open) {
            set_preview_file(null);
        }
    }, [open]);
    // Only offer view if both PdfViewer and get_download_url are available
    const can_view = !!(PdfViewer && get_download_url);
    const is_previewing = !!(preview_file && can_view);
    // Dialog width adapts: normal when no preview, wider with side panel
    const dialog_class = is_previewing
        ? 'max-w-6xl max-h-[85vh]'
        : 'max-w-2xl max-h-[80vh] overflow-y-auto';
    return (_jsx(Dialog, { open: open, onOpenChange: on_open_change, children: _jsx(DialogContent, { className: dialog_class, children: !is_previewing ? (
            /* Standard layout - clarification cards only */
            _jsxs(_Fragment, { children: [_jsxs(DialogHeader, { children: [_jsxs(DialogTitle, { children: ["Clarifications for ", field_label] }), _jsx(DialogDescription, { children: pending_count > 0
                                    ? `${pending_count} pending clarification${pending_count !== 1 ? 's' : ''} requiring attention`
                                    : `${items.length} clarification${items.length !== 1 ? 's' : ''}` })] }), _jsxs("div", { className: "space-y-4 mt-4", children: [items.length === 0 && (_jsx("p", { className: "text-sm text-muted-foreground italic text-center py-4", children: "No clarifications for this field." })), grouped_entries.map((entry) => Array.isArray(entry) ? (_jsx(ClarificationGroupCard, { items: entry, read_only: read_only, 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: can_view ? handle_doc_view : undefined, on_doc_delete: resolved_on_doc_delete }, entry.map(i => i.id).join('+'))) : (_jsx(ClarificationCard, { item: entry, read_only: read_only, 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: can_view ? handle_doc_view : undefined, on_doc_delete: resolved_on_doc_delete }, entry.id)))] })] })) : (
            /* Side panel layout - cards on left, PDF viewer on right */
            _jsxs(_Fragment, { children: [_jsxs(DialogHeader, { children: [_jsxs(DialogTitle, { children: ["Clarifications for ", field_label] }), _jsx(DialogDescription, { children: pending_count > 0
                                    ? `${pending_count} pending clarification${pending_count !== 1 ? 's' : ''} requiring attention`
                                    : `${items.length} clarification${items.length !== 1 ? 's' : ''}` })] }), _jsxs("div", { className: "flex gap-4 mt-4 min-h-0", style: { height: 'calc(85vh - 120px)' }, children: [_jsx("div", { className: "w-1/2 overflow-y-auto space-y-4 pr-2", children: grouped_entries.map((entry) => Array.isArray(entry) ? (_jsx(ClarificationGroupCard, { items: entry, read_only: read_only, 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: can_view ? handle_doc_view : undefined }, entry.map(i => i.id).join('+'))) : (_jsx(ClarificationCard, { item: entry, read_only: read_only, 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: can_view ? handle_doc_view : undefined }, entry.id))) }), _jsxs("div", { className: "w-1/2 border rounded-lg overflow-hidden flex flex-col bg-muted/20", children: [_jsxs("div", { className: "flex items-center justify-between px-3 py-2 border-b bg-muted/30", children: [_jsx("span", { className: "text-sm font-medium truncate", title: preview_file.file_name, children: preview_file.file_name }), _jsx("button", { type: "button", onClick: close_preview, className: "rounded-sm p-1 hover:bg-muted transition-colors flex-shrink-0", title: "Close preview", children: _jsx(IoClose, { className: "h-4 w-4" }) })] }), _jsx("div", { className: "flex-1 min-h-0", children: _jsx(PdfViewer, { url: get_download_url(preview_file.file_id), display_filename: preview_file.file_name, fit_to_width: true, toolbar_enabled: true, on_close: close_preview, className: "h-full w-full" }) })] })] })] })) }) }));
});
//# sourceMappingURL=clarification_dialog.js.map