import { jsx } from "react/jsx-runtime";
import { memo, useCallback, useMemo } from "react";
import { useOptionalNodeTypeRegistry } from "../../core/index.js";
import { usePreviewNode } from "../../hooks/index.js";
import { Toolbox } from "../Toolbox/index.js";
const AddNodePanel_AddNodePanel = /*#__PURE__*/ memo(function({ onNodeSelect, onClose, onSearch, onNodeHover, items, title, loading, renderEmptyState }) {
    const registry = useOptionalNodeTypeRegistry();
    const { previewNodeConnectionInfo } = usePreviewNode();
    const nodeListOptions = useMemo(()=>{
        if (items) return items;
        if (loading || !registry) return [];
        return registry.getNodeOptions({
            connections: previewNodeConnectionInfo,
            flattenSinglePath: true
        });
    }, [
        items,
        registry,
        previewNodeConnectionInfo,
        loading
    ]);
    const handleSearch = useCallback((query, isTopLevelSearch, { currentItems, category })=>{
        if (!onSearch && registry) {
            const listItems = registry.getNodeOptions({
                connections: previewNodeConnectionInfo,
                category,
                search: query,
                flattenAll: true
            });
            return Promise.resolve(listItems);
        }
        return onSearch?.(query, isTopLevelSearch, currentItems ?? [])?.then((nodes)=>nodes) ?? Promise.resolve([]);
    }, [
        registry,
        previewNodeConnectionInfo,
        onSearch
    ]);
    const handleNodeListItemSelect = useCallback((item)=>{
        onNodeSelect(item);
    }, [
        onNodeSelect
    ]);
    return /*#__PURE__*/ jsx(Toolbox, {
        title: title ?? 'Add node',
        initialItems: nodeListOptions,
        loading: loading,
        onItemSelect: handleNodeListItemSelect,
        onSearch: handleSearch,
        onClose: onClose,
        onItemHover: onNodeHover,
        renderEmptyState: renderEmptyState
    });
});
export { AddNodePanel_AddNodePanel as AddNodePanel };
