"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    ApolloChatTreeRenderer: ()=>ApolloChatTreeRenderer
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const react_namespaceObject = require("@lingui/react");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const index_cjs_namespaceObject = require("../../../../ap-tree-view/index.cjs");
const ApolloChatTreeRenderer = ({ span, onItemSelect, transformSpanToTreeItem: customTransformSpanToTreeItem, transformToTreeItems: customTransformToTreeItems })=>{
    const { _ } = (0, react_namespaceObject.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 = external_react_default().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__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: "apollo-chat-tree-renderer",
        children: _({
            id: 'autopilot-chat.tree.no-items'
        })
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: "apollo-chat-tree-renderer",
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTreeView, {
            items: treeItems,
            expanded: expandedItems,
            disableExpandCollapse: false,
            showSelectedChevron: true,
            useApIcon: true,
            onSelectedItemsChange: handleItemSelect
        })
    });
};
exports.ApolloChatTreeRenderer = __webpack_exports__.ApolloChatTreeRenderer;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "ApolloChatTreeRenderer"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
