/**
 * Sortable field list with drag and drop reordering
 */
'use client';
import { jsx as _jsx } from "react/jsx-runtime";
import { DndContext, closestCenter, KeyboardSensor, PointerSensor, useSensor, useSensors, } from '@dnd-kit/core';
import { arrayMove, SortableContext, sortableKeyboardCoordinates, verticalListSortingStrategy, } from '@dnd-kit/sortable';
import { SortableFieldItem } from './sortable_field_item.js';
/**
 * SortableFieldList - Draggable list with expand/collapse and field selection
 */
export function SortableFieldList({ items, on_reorder, on_remove, on_toggle_expand, on_toggle_field, }) {
    const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 8 } }), useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates }));
    const handle_drag_end = (event) => {
        const { active, over } = event;
        if (!over || active.id === over.id)
            return;
        const old_index = items.findIndex(item => item.id === active.id);
        const new_index = items.findIndex(item => item.id === over.id);
        if (old_index === -1 || new_index === -1)
            return;
        const target_item = items[new_index];
        const moved_item = items[old_index];
        const new_items = arrayMove(items, old_index, new_index);
        const updated_items = new_items.map((item, index) => {
            if (item.id === moved_item.id && target_item.section_name !== moved_item.section_name) {
                return {
                    ...item,
                    section_name: target_item.section_name,
                    section_index: target_item.section_index,
                    order: index,
                };
            }
            return { ...item, order: index };
        });
        on_reorder(updated_items);
    };
    if (items.length === 0) {
        return (_jsx("div", { className: "flex items-center justify-center h-32 text-muted-foreground text-sm", children: "No groups selected. Use the search above to add groups." }));
    }
    return (_jsx(DndContext, { sensors: sensors, collisionDetection: closestCenter, onDragEnd: handle_drag_end, children: _jsx(SortableContext, { items: items.map(i => i.id), strategy: verticalListSortingStrategy, children: _jsx("div", { className: "space-y-2", children: items.map(item => (_jsx(SortableFieldItem, { item: item, on_remove: () => on_remove(item.id), on_toggle_expand: () => on_toggle_expand(item.id), on_toggle_field: (field_id) => on_toggle_field(item.id, field_id) }, item.id))) }) }) }));
}
//# sourceMappingURL=sortable_field_list.js.map