/**
 * HazoFieldSelectorDialog - A dialog for selecting groups from a form template
 *
 * Features:
 * - Search and select sections, groups, or fields
 * - Expandable groups with field-level selection
 * - Drag and drop reordering (cross-section supported)
 * - Output filtered JSON structure on confirm
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useMemo, useCallback, useEffect } from 'react';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, } from '../shared/dialog/index.js';
import { Button } from '../shared/button/index.js';
import { FieldAutocomplete } from './components/field_autocomplete.js';
import { SelectedFieldsPanel } from './components/selected_fields_panel.js';
import { flatten_form_template, build_output_template, update_item_orders, create_selection_from_search_item, toggle_field_in_group, toggle_group_expansion, add_field_to_existing_group, } from './utils.js';
const fullScreenStyles = {
    position: 'fixed',
    top: '32px',
    left: '32px',
    right: '32px',
    bottom: '32px',
    width: 'calc(100vw - 64px)',
    height: 'calc(100vh - 64px)',
    maxWidth: 'none',
    maxHeight: 'none',
    transform: 'none',
    display: 'flex',
    flexDirection: 'column',
};
/**
 * HazoFieldSelectorDialog - Dialog with enhanced search and field-level selection
 */
export function HazoFieldSelectorDialog({ open, on_open_change, form_template, on_confirm, on_cancel, initial_selection, title = 'Select Fields', description, confirm_text = 'Confirm', cancel_text = 'Cancel', include_icons_behaviour = true, }) {
    const searchable_items = useMemo(() => flatten_form_template(form_template), [form_template]);
    const [selected_items, set_selected_items] = useState(initial_selection || []);
    useEffect(() => {
        if (open) {
            set_selected_items(initial_selection || []);
        }
    }, [open, initial_selection]);
    const selected_group_ids = useMemo(() => new Set(selected_items.map(i => i.id)), [selected_items]);
    // Track which sections have been fully selected (when user clicked on a section)
    const [selected_section_names, set_selected_section_names] = useState(new Set());
    // Reset selected sections when dialog opens
    useEffect(() => {
        if (open) {
            set_selected_section_names(new Set());
        }
    }, [open]);
    const handle_search_select = useCallback((item) => {
        // Track if a section was selected
        if (item.item_type === 'section') {
            set_selected_section_names(prev => new Set([...prev, item.section_name]));
        }
        set_selected_items(prev => {
            if (item.item_type === 'field' && item.parent_group_id) {
                const existing = prev.find(p => p.id === item.parent_group_id);
                if (existing) {
                    return add_field_to_existing_group(prev, item.parent_group_id, item.field_config.id);
                }
            }
            const new_items = create_selection_from_search_item(item, form_template, prev);
            if (new_items.length === 0)
                return prev;
            return update_item_orders([...prev, ...new_items]);
        });
    }, [form_template]);
    const handle_remove = useCallback((id) => {
        set_selected_items(prev => {
            const item_to_remove = prev.find(i => i.id === id);
            // If removing an item from a "fully selected" section, unmark that section
            if (item_to_remove && selected_section_names.has(item_to_remove.section_name)) {
                set_selected_section_names(s => {
                    const next = new Set(s);
                    next.delete(item_to_remove.section_name);
                    return next;
                });
            }
            return update_item_orders(prev.filter(i => i.id !== id));
        });
    }, [selected_section_names]);
    const handle_reorder = useCallback((items) => {
        set_selected_items(items);
    }, []);
    const handle_toggle_field = useCallback((group_id, field_id) => {
        set_selected_items(prev => toggle_field_in_group(prev, group_id, field_id));
    }, []);
    const handle_toggle_expand = useCallback((group_id) => {
        set_selected_items(prev => toggle_group_expansion(prev, group_id));
    }, []);
    const handle_confirm = () => {
        const output = build_output_template(form_template, selected_items, include_icons_behaviour);
        on_confirm(output);
        on_open_change(false);
    };
    const handle_cancel = () => {
        on_cancel?.();
        on_open_change(false);
    };
    const total_fields_selected = useMemo(() => {
        return selected_items.reduce((acc, item) => {
            const total = item.field_config.fields?.length ?? 0;
            const selected = item.field_selection?.selected_field_ids.size ?? total;
            return acc + selected;
        }, 0);
    }, [selected_items]);
    const has_partial_selection = useMemo(() => selected_items.some(i => i.field_selection), [selected_items]);
    return (_jsx(Dialog, { open: open, onOpenChange: on_open_change, children: _jsx(DialogContent, { style: fullScreenStyles, className: "p-0 overflow-hidden", children: _jsxs("div", { className: "flex flex-col h-full", children: [_jsxs(DialogHeader, { className: "px-6 py-4 border-b space-y-1", children: [_jsx(DialogTitle, { children: title }), description && _jsx(DialogDescription, { children: description })] }), _jsx("div", { className: "px-6 py-4 border-b", children: _jsx(FieldAutocomplete, { items: searchable_items, selected_group_ids: selected_group_ids, selected_section_names: selected_section_names, on_select: handle_search_select, placeholder: "Search sections, groups, or fields..." }) }), _jsx("div", { className: "flex-1 overflow-hidden", children: _jsx(SelectedFieldsPanel, { items: selected_items, on_remove: handle_remove, on_reorder: handle_reorder, on_toggle_expand: handle_toggle_expand, on_toggle_field: handle_toggle_field }) }), _jsxs("div", { className: "px-6 py-4 border-t flex items-center justify-between w-full", children: [_jsxs("span", { className: "text-sm text-muted-foreground", children: [selected_items.length, " group", selected_items.length !== 1 ? 's' : '', " selected", has_partial_selection && (_jsxs("span", { className: "ml-1", children: ["(", total_fields_selected, " field", total_fields_selected !== 1 ? 's' : '', " total)"] }))] }), _jsxs(DialogFooter, { className: "flex-row gap-3", children: [_jsx(Button, { onClick: handle_confirm, disabled: selected_items.length === 0, children: confirm_text }), _jsx(Button, { variant: "outline", onClick: handle_cancel, children: cancel_text })] })] })] }) }) }));
}
//# sourceMappingURL=hazo_field_selector_dialog.js.map