"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__, {
    ResourcePickerDropdown: ()=>ResourcePickerDropdown
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const react_namespaceObject = require("@lingui/react");
const CircularProgress_namespaceObject = require("@mui/material/CircularProgress");
var CircularProgress_default = /*#__PURE__*/ __webpack_require__.n(CircularProgress_namespaceObject);
const Menu_namespaceObject = require("@mui/material/Menu");
var Menu_default = /*#__PURE__*/ __webpack_require__.n(Menu_namespaceObject);
const MenuItem_namespaceObject = require("@mui/material/MenuItem");
var MenuItem_default = /*#__PURE__*/ __webpack_require__.n(MenuItem_namespaceObject);
const styles_namespaceObject = require("@mui/material/styles");
const apollo_core_namespaceObject = require("@uipath/apollo-core");
var apollo_core_default = /*#__PURE__*/ __webpack_require__.n(apollo_core_namespaceObject);
const external_index_cjs_namespaceObject = require("../../../../index.cjs");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const external_react_window_namespaceObject = require("react-window");
const index_cjs_namespaceObject = require("../../../ap-skeleton/index.cjs");
const chat_state_provider_cjs_namespaceObject = require("../../providers/chat-state-provider.cjs");
const resource_picker_provider_cjs_namespaceObject = require("../../providers/resource-picker-provider.cjs");
const external_service_index_cjs_namespaceObject = require("../../service/index.cjs");
const icon_button_cjs_namespaceObject = require("../common/icon-button.cjs");
const tooltip_cjs_namespaceObject = require("../common/tooltip.cjs");
const ellipsisStyle = {
    overflow: 'hidden',
    textOverflow: 'ellipsis',
    whiteSpace: 'nowrap'
};
const StyledMenuItem = (0, styles_namespaceObject.styled)(MenuItem_default())({
    display: 'flex',
    alignItems: 'center',
    gap: apollo_core_default().Spacing.SpacingXs,
    justifyContent: 'space-between'
});
const ResourceItemContent = (0, styles_namespaceObject.styled)('div')({
    display: 'flex',
    alignItems: 'center',
    gap: apollo_core_default().Spacing.SpacingXs,
    flex: 1,
    minWidth: 0
});
const ErrorContainer = (0, styles_namespaceObject.styled)('div')(()=>({
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'center',
        gap: apollo_core_default().Spacing.SpacingS,
        padding: apollo_core_default().Padding.PadXxxl,
        color: 'var(--color-error-text)'
    }));
const EmptyContainer = (0, styles_namespaceObject.styled)('div')(()=>({
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        padding: `${apollo_core_default().Padding.PadXxl} ${apollo_core_default().Padding.PadXxxl}`,
        color: 'var(--color-foreground-de-emp)'
    }));
const DrillDownHeader = (0, styles_namespaceObject.styled)('div')(()=>({
        display: 'flex',
        alignItems: 'center',
        gap: apollo_core_default().Spacing.SpacingMicro,
        padding: `${apollo_core_default().Padding.PadXs} ${apollo_core_default().Padding.PadXxxl} ${apollo_core_default().Padding.PadXs} ${apollo_core_default().Padding.PadXxl}`,
        backgroundColor: 'var(--color-background-secondary)'
    }));
const SkeletonItem = (0, styles_namespaceObject.styled)(StyledMenuItem)({
    pointerEvents: 'none'
});
const StyledResourceList = (0, styles_namespaceObject.styled)(external_react_window_namespaceObject.List)({
    '&::-webkit-scrollbar': {
        width: '6px'
    },
    '&::-webkit-scrollbar-track': {
        background: 'transparent'
    },
    '&::-webkit-scrollbar-thumb': {
        background: 'var(--color-border-de-emp)',
        borderRadius: '3px'
    }
});
const loaderRowStyle = {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center'
};
const preventDefaultMouseDown = (e)=>e.preventDefault();
const MENU_SLOT_PROPS = {
    paper: {
        sx: {
            width: external_service_index_cjs_namespaceObject.CHAT_RESOURCE_PICKER_MENU_WIDTH,
            maxHeight: external_service_index_cjs_namespaceObject.CHAT_RESOURCE_PICKER_MENU_MAX_HEIGHT,
            overflowY: 'hidden',
            display: 'flex',
            flexDirection: 'column'
        }
    }
};
const VirtualizedResourceRow = ({ index, style, ariaAttributes, items, selectedIndex, navigationMode, handleItemClick, onMouseEnterItem, tooltipPlacement, fontToken })=>{
    if (index >= items.length) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        style: {
            ...style,
            ...loaderRowStyle
        },
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CircularProgress_default(), {
            size: 20
        })
    });
    const item = items[index];
    const isSelected = index === selectedIndex;
    const isCategory = (0, resource_picker_provider_cjs_namespaceObject.isResourceSelector)(item);
    const tooltipContent = item.tooltip;
    const isKeyboardSelected = isSelected && 'keyboard' === navigationMode;
    const menuItem = /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(StyledMenuItem, {
        id: `resource-item-${item.id}`,
        selected: isSelected,
        onClick: ()=>handleItemClick(item),
        onMouseEnter: ()=>onMouseEnterItem(index),
        style: style,
        ...ariaAttributes,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(ResourceItemContent, {
                children: [
                    item.icon && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApIcon, {
                        variant: "outlined",
                        name: item.icon,
                        size: apollo_core_default().Icon.IconXs
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApTypography, {
                        variant: fontToken,
                        style: ellipsisStyle,
                        children: item.displayName
                    })
                ]
            }),
            isCategory && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApIcon, {
                variant: "outlined",
                name: "chevron_right",
                size: apollo_core_default().Icon.IconXs
            })
        ]
    });
    if (!tooltipContent) return menuItem;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(tooltip_cjs_namespaceObject.AutopilotChatTooltip, {
        title: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApTypography, {
            color: 'var(--color-foreground-inverse)',
            variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
            children: tooltipContent
        }),
        placement: tooltipPlacement,
        open: isKeyboardSelected ? true : void 0,
        enterDelay: isKeyboardSelected ? 0 : external_service_index_cjs_namespaceObject.CHAT_RESOURCE_PICKER_TOOLTIP_ENTER_DELAY,
        enterNextDelay: isKeyboardSelected ? 0 : external_service_index_cjs_namespaceObject.CHAT_RESOURCE_PICKER_TOOLTIP_ENTER_DELAY,
        children: menuItem
    });
};
const chat_input_resource_picker_MenuContent = /*#__PURE__*/ external_react_default().memo(function({ virtualListRef, selectedIndex, navigationMode, onMouseEnterItem, onItemClick }) {
    const { displayedItems: items, drillDown, query, loading, loadingMore, searchInProgress, hasMore, error, previousDisplayCount, goBackOrClose, retryLoad, loadMore } = (0, resource_picker_provider_cjs_namespaceObject.useAutopilotChatResourcePicker)();
    const { theming, spacing } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    const { _ } = (0, react_namespaceObject.useLingui)();
    const drillDownHeaderRef = (0, external_react_namespaceObject.useRef)(null);
    const [drillDownHeaderHeight, setDrillDownHeaderHeight] = (0, external_react_namespaceObject.useState)(0);
    (0, external_react_namespaceObject.useLayoutEffect)(()=>{
        if (!drillDown) return void setDrillDownHeaderHeight(0);
        setDrillDownHeaderHeight(drillDownHeaderRef.current?.offsetHeight ?? 0);
    }, [
        drillDown
    ]);
    const handleRowsRendered = (0, external_react_namespaceObject.useCallback)((visibleRows, _allRows)=>{
        if (!hasMore || loadingMore) return;
        if (visibleRows.stopIndex >= items.length - external_service_index_cjs_namespaceObject.CHAT_RESOURCE_PICKER_LOAD_MORE_THRESHOLD) loadMore();
    }, [
        items.length,
        hasMore,
        loadingMore,
        loadMore
    ]);
    const tooltipPlacement = theming?.chatMenu?.groupItemTooltipPlacement ?? 'left';
    const isLoadingState = loading || searchInProgress;
    const skeletonElements = (0, external_react_namespaceObject.useMemo)(()=>{
        const skeletonCount = Math.min(Math.max(previousDisplayCount, external_service_index_cjs_namespaceObject.CHAT_RESOURCE_PICKER_MIN_SKELETON_COUNT), external_service_index_cjs_namespaceObject.CHAT_RESOURCE_PICKER_MAX_SKELETON_COUNT);
        return Array.from({
            length: skeletonCount
        }).map((_, index)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(SkeletonItem, {
                disabled: true,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApSkeleton, {})
            }, `skeleton-${index}`));
    }, [
        previousDisplayCount
    ]);
    const rowCount = items.length + (loadingMore ? 1 : 0);
    const listHeight = Math.min(external_service_index_cjs_namespaceObject.CHAT_RESOURCE_PICKER_MENU_MAX_HEIGHT - drillDownHeaderHeight, rowCount * external_service_index_cjs_namespaceObject.CHAT_RESOURCE_PICKER_ITEM_HEIGHT);
    const rowProps = (0, external_react_namespaceObject.useMemo)(()=>({
            items,
            selectedIndex,
            navigationMode,
            handleItemClick: onItemClick,
            onMouseEnterItem,
            tooltipPlacement,
            fontToken: spacing.primaryFontToken
        }), [
        items,
        selectedIndex,
        navigationMode,
        onItemClick,
        onMouseEnterItem,
        tooltipPlacement,
        spacing.primaryFontToken
    ]);
    const drillDownHeader = drillDown ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(DrillDownHeader, {
        ref: drillDownHeaderRef,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_button_cjs_namespaceObject.AutopilotChatIconButton, {
                size: "small",
                onClick: goBackOrClose,
                "aria-label": _({
                    id: 'autopilot-chat.resource-picker.back'
                }),
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApIcon, {
                    variant: "outlined",
                    name: "arrow_back",
                    size: apollo_core_default().Icon.IconXs
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApTypography, {
                variant: spacing.primaryBoldFontToken,
                style: ellipsisStyle,
                children: drillDown.category.displayName
            })
        ]
    }) : null;
    if (isLoadingState) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
        children: [
            drillDownHeader,
            skeletonElements
        ]
    });
    if (error) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
        children: [
            drillDownHeader,
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(ErrorContainer, {
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApTypography, {
                        variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
                        children: error
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApButton, {
                        variant: "text",
                        size: "small",
                        onClick: retryLoad,
                        label: _({
                            id: 'autopilot-chat.resource-picker.retry'
                        })
                    })
                ]
            })
        ]
    });
    if (0 === items.length) {
        const emptyMessage = query.trim() ? _({
            id: 'autopilot-chat.resource-picker.no-results'
        }) : _({
            id: 'autopilot-chat.resource-picker.empty'
        });
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
            children: [
                drillDownHeader,
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(EmptyContainer, {
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApTypography, {
                        variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
                        children: emptyMessage
                    })
                })
            ]
        });
    }
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
        children: [
            drillDownHeader,
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(StyledResourceList, {
                listRef: virtualListRef,
                rowProps: rowProps,
                rowComponent: VirtualizedResourceRow,
                rowCount: rowCount,
                rowHeight: external_service_index_cjs_namespaceObject.CHAT_RESOURCE_PICKER_ITEM_HEIGHT,
                overscanCount: 5,
                onRowsRendered: handleRowsRendered,
                style: {
                    height: listHeight
                }
            })
        ]
    });
});
function ResourcePickerDropdownInner(_props, ref) {
    const { isOpen, isOpenRef, anchorPosition, displayedItems: items, drillDown, query, handleItemClick, close, goBackOrClose } = (0, resource_picker_provider_cjs_namespaceObject.useAutopilotChatResourcePicker)();
    const virtualListRef = (0, external_react_window_namespaceObject.useListRef)(null);
    const { _ } = (0, react_namespaceObject.useLingui)();
    const [selectedIndex, setSelectedIndex] = (0, external_react_namespaceObject.useState)(0);
    const [navigationMode, setNavigationMode] = (0, external_react_namespaceObject.useState)('none');
    const prevStateRef = (0, external_react_namespaceObject.useRef)({
        query,
        drillDownId: drillDown?.category?.id
    });
    (0, external_react_namespaceObject.useEffect)(()=>{
        const prev = prevStateRef.current;
        const currentDrillDownId = drillDown?.category?.id;
        if (prev.query !== query || prev.drillDownId !== currentDrillDownId) {
            setSelectedIndex(0);
            setNavigationMode('none');
        }
        prev.query = query;
        prev.drillDownId = currentDrillDownId;
    }, [
        query,
        drillDown
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (isOpen) {
            setSelectedIndex(0);
            setNavigationMode('none');
        }
    }, [
        isOpen
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if ('keyboard' === navigationMode) virtualListRef.current?.scrollToRow({
            index: selectedIndex,
            align: 'auto'
        });
    }, [
        selectedIndex,
        navigationMode,
        virtualListRef
    ]);
    const navigateUp = (0, external_react_namespaceObject.useCallback)(()=>{
        setSelectedIndex((prev)=>Math.max(prev - 1, 0));
        setNavigationMode('keyboard');
    }, []);
    const navigateDown = (0, external_react_namespaceObject.useCallback)(()=>{
        setSelectedIndex((prev)=>Math.min(prev + 1, items.length - 1));
        setNavigationMode('keyboard');
    }, [
        items.length
    ]);
    const handleMouseEnterItem = (0, external_react_namespaceObject.useCallback)((index)=>{
        setSelectedIndex(index);
        setNavigationMode('pointer');
    }, []);
    const selectItem = (0, external_react_namespaceObject.useCallback)(()=>{
        const selectedItem = items[selectedIndex];
        if (selectedItem) handleItemClick(selectedItem);
    }, [
        items,
        selectedIndex,
        handleItemClick
    ]);
    (0, external_react_namespaceObject.useImperativeHandle)(ref, ()=>({
            handleKeyDown: (event)=>{
                if (!isOpenRef.current) return false;
                switch(event.key){
                    case 'ArrowDown':
                        event.preventDefault();
                        navigateDown();
                        return true;
                    case 'ArrowUp':
                        event.preventDefault();
                        navigateUp();
                        return true;
                    case 'Enter':
                    case 'Tab':
                        event.preventDefault();
                        selectItem();
                        return true;
                    case 'Escape':
                        event.preventDefault();
                        drillDown ? goBackOrClose() : close();
                        return true;
                    default:
                        return false;
                }
            }
        }), [
        navigateDown,
        navigateUp,
        selectItem,
        goBackOrClose,
        drillDown,
        close
    ]);
    const handleMenuClose = (0, external_react_namespaceObject.useCallback)((_event, reason)=>{
        if ('escapeKeyDown' === reason && drillDown) return void goBackOrClose();
        close();
    }, [
        drillDown,
        goBackOrClose,
        close
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Menu_default(), {
        open: isOpen,
        anchorReference: "anchorPosition",
        anchorPosition: anchorPosition ?? void 0,
        onClose: handleMenuClose,
        anchorOrigin: {
            vertical: 'top',
            horizontal: 'left'
        },
        transformOrigin: {
            vertical: 'bottom',
            horizontal: 'left'
        },
        disableAutoFocus: true,
        autoFocus: false,
        MenuListProps: {
            'aria-label': _({
                id: 'autopilot-chat.resource-picker.label'
            }),
            dense: true,
            disableListWrap: true,
            onMouseDown: preventDefaultMouseDown
        },
        slotProps: MENU_SLOT_PROPS,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(chat_input_resource_picker_MenuContent, {
            virtualListRef: virtualListRef,
            selectedIndex: selectedIndex,
            navigationMode: navigationMode,
            onMouseEnterItem: handleMouseEnterItem,
            onItemClick: handleItemClick
        })
    });
}
const ResourcePickerDropdown = /*#__PURE__*/ external_react_default().memo(/*#__PURE__*/ (0, external_react_namespaceObject.forwardRef)(ResourcePickerDropdownInner));
exports.ResourcePickerDropdown = __webpack_exports__.ResourcePickerDropdown;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "ResourcePickerDropdown"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
