import { jsx } from "react/jsx-runtime";
import { useLingui } from "@lingui/react";
import react from "react";
import { ApTreeView } from "../../../../ap-tree-view/index.js";
const ApolloChatTreeRenderer = ({ span, onItemSelect, transformSpanToTreeItem: customTransformSpanToTreeItem, transformToTreeItems: customTransformToTreeItems })=>{
    const { _ } = useLingui();
    const defaultTransformToTreeItems = ()=>{
        const items = [];
        if (span) {
            const rootItem = transformSpanToTreeItem(span, 'root');
            if (rootItem) items.push(rootItem);
        }
        return items;
    };
    const transformToTreeItems = customTransformToTreeItems || defaultTransformToTreeItems;
    const defaultTransformSpanToTreeItem = (spanNode, key)=>{
        const spanData = spanNode?.data;
        if (!spanData) return null;
        let children;
        if (Array.isArray(spanNode?.children) && spanNode.children.length > 0) children = spanNode.children.map((child, index)=>(customTransformSpanToTreeItem || defaultTransformSpanToTreeItem)(child, `${key}-${index}`)).filter((item)=>null !== item);
        const getAdditionalInfo = (data)=>{
            if (data.additionalInfo) return data.additionalInfo;
        };
        const item = {
            id: spanData.id ?? key,
            title: spanNode.name ?? _({
                id: 'autopilot-chat.tree.unknown'
            }),
            description: spanData.attributes?.description ?? spanData.attributes?.type ?? _({
                id: "autopilot-chat.tree.no-description"
            }),
            icon: spanData.icon,
            expanded: spanData.expanded,
            titleColor: spanData.titleColor,
            customIcon: spanData.customIcon,
            additionalInfo: getAdditionalInfo(spanData),
            children
        };
        return item;
    };
    const transformSpanToTreeItem = customTransformSpanToTreeItem || defaultTransformSpanToTreeItem;
    const treeItems = transformToTreeItems(span);
    const getExpandedItems = (items)=>{
        const ids = [];
        const traverse = (item)=>{
            if (false !== item.expanded) ids.push(item.id);
            if (item.children) item.children.forEach(traverse);
        };
        items.forEach(traverse);
        return ids;
    };
    const expandedItems = getExpandedItems(treeItems);
    const handleItemSelect = react.useCallback((itemId)=>{
        if (itemId && onItemSelect) {
            const findItemById = (items, id)=>{
                for (const item of items){
                    if (item.id === id) return item;
                    if (item.children) {
                        const found = findItemById(item.children, id);
                        if (found) return found;
                    }
                }
                return null;
            };
            const selectedItem = findItemById(treeItems, itemId);
            if (selectedItem) onItemSelect(itemId, selectedItem);
        }
    }, [
        onItemSelect,
        treeItems
    ]);
    if (!treeItems || 0 === treeItems.length) return /*#__PURE__*/ jsx("div", {
        className: "apollo-chat-tree-renderer",
        children: _({
            id: 'autopilot-chat.tree.no-items'
        })
    });
    return /*#__PURE__*/ jsx("div", {
        className: "apollo-chat-tree-renderer",
        children: /*#__PURE__*/ jsx(ApTreeView, {
            items: treeItems,
            expanded: expandedItems,
            disableExpandCollapse: false,
            showSelectedChevron: true,
            useApIcon: true,
            onSelectedItemsChange: handleItemSelect
        })
    });
};
export { ApolloChatTreeRenderer };
