"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__, {
    ModelPicker: ()=>ModelPicker
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const Add_namespaceObject = require("@mui/icons-material/Add");
var Add_default = /*#__PURE__*/ __webpack_require__.n(Add_namespaceObject);
const Box_namespaceObject = require("@mui/material/Box");
var Box_default = /*#__PURE__*/ __webpack_require__.n(Box_namespaceObject);
const ButtonBase_namespaceObject = require("@mui/material/ButtonBase");
var ButtonBase_default = /*#__PURE__*/ __webpack_require__.n(ButtonBase_namespaceObject);
const CircularProgress_namespaceObject = require("@mui/material/CircularProgress");
var CircularProgress_default = /*#__PURE__*/ __webpack_require__.n(CircularProgress_namespaceObject);
const Typography_namespaceObject = require("@mui/material/Typography");
var Typography_default = /*#__PURE__*/ __webpack_require__.n(Typography_namespaceObject);
const apollo_core_namespaceObject = require("@uipath/apollo-core");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const index_cjs_namespaceObject = require("../../../i18n/index.cjs");
const FolderSwitcher_cjs_namespaceObject = require("./primitives/FolderSwitcher.cjs");
const ModelOptionRow_cjs_namespaceObject = require("./primitives/ModelOptionRow.cjs");
const OptionList_cjs_namespaceObject = require("./primitives/OptionList.cjs");
const PickerPopup_cjs_namespaceObject = require("./primitives/PickerPopup.cjs");
const PickerSearchInput_cjs_namespaceObject = require("./primitives/PickerSearchInput.cjs");
const PickerTrigger_cjs_namespaceObject = require("./primitives/PickerTrigger.cjs");
const external_useModelPickerState_cjs_namespaceObject = require("./useModelPickerState.cjs");
const external_usePlatformAccess_cjs_namespaceObject = require("./usePlatformAccess.cjs");
const REQUIRED_INDICATOR = '*';
const AUTO_VIRTUALIZE_THRESHOLD = 120;
const ModelPicker = /*#__PURE__*/ external_react_default().forwardRef(({ models, value, onChange, label, required, placeholder, disabled, invalid, errorText, variant = 'searchable', groupBy = "subscription", allowGroupingChange = true, homeRegion, recommendedModelIds, previewModelIds, filter, badgesFor, customTagsFor, customTagVariants, canManageByo, onUseCustomModel, requestContext, enableFolders = false, folders, folder = null, onFolderChange, allFoldersLabel, loading, error, showGroupHeaders = true, slots }, forwardedRef)=>{
    const { _ } = (0, index_cjs_namespaceObject.useSafeLingui)();
    const i18n = external_react_default().useMemo(()=>({
            _
        }), [
        _
    ]);
    const resolvedLabel = label ?? _({
        id: 'modelPicker.label.default',
        message: 'Model'
    });
    const resolvedPlaceholder = placeholder ?? _({
        id: 'modelPicker.placeholder.selectAModel',
        message: 'Select a model'
    });
    const initiallyCollapsedGroups = external_react_default().useMemo(()=>[], []);
    const state = (0, external_useModelPickerState_cjs_namespaceObject.useModelPickerState)({
        models,
        value,
        onChange,
        groupBy,
        recommendedModelIds,
        previewModelIds,
        filter,
        initiallyCollapsedGroups,
        i18n
    });
    const { open, setOpen, query, setQuery, groupBy: activeGroupBy, setGroupBy, filtered, groupCounts, collapsedGroups, toggleGroup, selected, unknownValue, activeIndex, setActiveIndex, onSearchKeyDown, choose, id, triggerRef, searchRef } = state;
    const listboxId = `${id}-listbox`;
    const List = 'virtualized' === variant || filtered.length > AUTO_VIRTUALIZE_THRESHOLD ? OptionList_cjs_namespaceObject.VirtualOptionList : OptionList_cjs_namespaceObject.GroupedOptionList;
    const handleTriggerRef = external_react_default().useCallback((node)=>{
        triggerRef.current = node;
        if ('function' == typeof forwardedRef) forwardedRef(node);
        else if (forwardedRef) forwardedRef.current = node;
    }, [
        forwardedRef,
        triggerRef
    ]);
    const closePopup = external_react_default().useCallback(()=>setOpen(false), [
        setOpen
    ]);
    const slotCtx = external_react_default().useMemo(()=>({
            selected,
            close: closePopup
        }), [
        selected,
        closePopup
    ]);
    const { canManage: isOrgAdmin } = (0, external_usePlatformAccess_cjs_namespaceObject.useCanManageByo)(void 0 === canManageByo && requestContext ? requestContext : null);
    const effectiveCanManageByo = canManageByo ?? isOrgAdmin ?? false;
    const { folders: fetchedFolders } = (0, external_usePlatformAccess_cjs_namespaceObject.useUserFolders)(enableFolders && !folders && requestContext ? requestContext : null);
    const effectiveFolders = folders ?? (enableFolders ? fetchedFolders : void 0);
    const selectedFolderNumericId = external_react_default().useMemo(()=>effectiveFolders?.find((f)=>f.id === folder)?.numericId, [
        effectiveFolders,
        folder
    ]);
    const navigateToLlmConfigurations = external_react_default().useMemo(()=>{
        if (!requestContext) return;
        return (link)=>{
            external_usePlatformAccess_cjs_namespaceObject.platformNavigation.assign((0, external_usePlatformAccess_cjs_namespaceObject.buildLlmConfigurationsUrl)(requestContext, {
                ...link,
                folderNumericId: selectedFolderNumericId
            }));
        };
    }, [
        requestContext,
        selectedFolderNumericId
    ]);
    external_react_default().useEffect(()=>{
        if ("u" < typeof process || 'production' === process.env['NODE_ENV'] || !effectiveFolders) return;
        const seen = new Set();
        for (const f of effectiveFolders){
            if (seen.has(f.id)) return void console.warn(`[ModelPicker] Duplicate folder id "${f.id}" — folder selection will misbehave.`);
            seen.add(f.id);
        }
    }, [
        effectiveFolders
    ]);
    const tagContext = external_react_default().useMemo(()=>({
            i18n,
            homeRegion,
            recommendedModelIds,
            previewModelIds,
            badgesFor,
            customTagsFor
        }), [
        i18n,
        homeRegion,
        recommendedModelIds,
        previewModelIds,
        badgesFor,
        customTagsFor
    ]);
    const rowHideTagKinds = external_react_default().useMemo(()=>"subscription" === activeGroupBy ? [
            'recommended',
            'preview'
        ] : void 0, [
        activeGroupBy
    ]);
    const renderRowActions = external_react_default().useMemo(()=>{
        if (slots?.optionActions) return slots.optionActions;
        if (!effectiveCanManageByo || !navigateToLlmConfigurations) return ()=>null;
        return (m)=>(0, ModelOptionRow_cjs_namespaceObject.defaultRowActions)(m, {
                i18n,
                onEdit: (model)=>navigateToLlmConfigurations({
                        intent: 'edit',
                        configurationId: model.byomDetails?.productLlmConfigurationId
                    })
            });
    }, [
        slots?.optionActions,
        effectiveCanManageByo,
        navigateToLlmConfigurations,
        i18n
    ]);
    const footerNode = external_react_default().useMemo(()=>{
        if (slots && Object.hasOwn(slots, 'popupFooter')) return slots.popupFooter ? slots.popupFooter(slotCtx) : null;
        if (!effectiveCanManageByo) return null;
        const activate = onUseCustomModel ?? (navigateToLlmConfigurations ? ()=>navigateToLlmConfigurations({
                intent: 'add'
            }) : void 0);
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(UseCustomModelFooter, {
            onActivate: ()=>{
                closePopup();
                activate?.();
            },
            disabled: !activate
        });
    }, [
        slots,
        effectiveCanManageByo,
        onUseCustomModel,
        navigateToLlmConfigurations,
        slotCtx,
        closePopup
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(Box_default(), {
        className: "apollo-model-picker",
        sx: {
            width: '100%',
            fontFamily: apollo_core_namespaceObject.FontFamily.FontNormal
        },
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(Typography_default(), {
                component: "label",
                htmlFor: `${id}-trigger`,
                sx: {
                    display: 'block',
                    fontSize: 13,
                    fontWeight: 600,
                    color: `var(--color-foreground-de-emp, ${apollo_core_namespaceObject.Colors.ColorGray550})`,
                    mb: 0.875
                },
                children: [
                    resolvedLabel,
                    required && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Box_default(), {
                        component: "span",
                        "aria-hidden": true,
                        sx: {
                            color: `var(--color-error-text, ${apollo_core_namespaceObject.Colors.ColorRed700})`,
                            ml: 0.25
                        },
                        children: REQUIRED_INDICATOR
                    })
                ]
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(PickerTrigger_cjs_namespaceObject.PickerTrigger, {
                id: `${id}-trigger`,
                triggerRef: handleTriggerRef,
                selected: selected,
                unknownValue: unknownValue,
                placeholder: resolvedPlaceholder,
                disabled: disabled,
                invalid: !!invalid || !!error || !!unknownValue,
                required: required,
                open: open,
                controlsId: listboxId,
                describedById: errorText ?? error ? `${id}-error` : void 0,
                tagContext: tagContext,
                tagVariants: customTagVariants,
                extra: slots?.triggerExtra?.(selected),
                onClick: ()=>setOpen(!open)
            }),
            (errorText ?? error) && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Typography_default(), {
                id: `${id}-error`,
                variant: "caption",
                role: "alert",
                sx: {
                    display: 'block',
                    color: `var(--color-error-text, ${apollo_core_namespaceObject.Colors.ColorRed700})`,
                    mt: 0.5,
                    fontSize: 12
                },
                children: errorText ?? error?.message
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(PickerPopup_cjs_namespaceObject.PickerPopup, {
                open: open,
                anchorEl: triggerRef.current,
                onClose: closePopup,
                header: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                    children: [
                        slots?.popupHeader?.(),
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(PickerSearchInput_cjs_namespaceObject.PickerSearchInput, {
                            value: query,
                            onChange: (next)=>{
                                setQuery(next);
                                setActiveIndex(0);
                            },
                            onKeyDown: onSearchKeyDown,
                            inputRef: searchRef,
                            listboxId: listboxId,
                            activeDescendantId: filtered[activeIndex] ? (0, OptionList_cjs_namespaceObject.optionDomId)(listboxId, filtered[activeIndex].modelId) : void 0,
                            leading: slots?.searchLeading?.() ?? (effectiveFolders && effectiveFolders.length > 0 && onFolderChange ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(FolderSwitcher_cjs_namespaceObject.FolderSwitcher, {
                                folders: effectiveFolders,
                                value: folder ?? null,
                                onChange: onFolderChange,
                                allFoldersLabel: allFoldersLabel
                            }) : void 0),
                            trailing: allowGroupingChange ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(GroupBySegmented, {
                                value: activeGroupBy,
                                onChange: setGroupBy
                            }) : void 0
                        })
                    ]
                }),
                footer: footerNode,
                children: [
                    loading && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Box_default(), {
                        role: "status",
                        "aria-live": "polite",
                        "aria-label": _({
                            id: 'modelPicker.loading.label',
                            message: 'Loading models'
                        }),
                        sx: {
                            py: 3,
                            display: 'flex',
                            justifyContent: 'center',
                            color: `var(--color-foreground-de-emp, ${apollo_core_namespaceObject.Colors.ColorGray550})`
                        },
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CircularProgress_default(), {
                            size: 20
                        })
                    }),
                    error && !loading && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Box_default(), {
                        role: "alert",
                        sx: {
                            py: 2,
                            px: 2,
                            textAlign: 'center',
                            color: `var(--color-error-text, ${apollo_core_namespaceObject.Colors.ColorRed700})`,
                            fontSize: 13
                        },
                        children: error.message
                    }),
                    !loading && !error && 0 === filtered.length && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Box_default(), {
                        role: "status",
                        "aria-live": "polite",
                        sx: {
                            py: 3,
                            textAlign: 'center',
                            color: `var(--color-foreground-de-emp, ${apollo_core_namespaceObject.Colors.ColorGray550})`,
                            fontSize: 13
                        },
                        children: query.trim() ? _({
                            id: 'modelPicker.empty.noMatch',
                            message: 'No models match "{query}".',
                            values: {
                                query: query.trim()
                            }
                        }) : _({
                            id: 'modelPicker.empty.noModels',
                            message: 'No models available.'
                        })
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Box_default(), {
                        "aria-live": "polite",
                        "aria-atomic": "true",
                        sx: {
                            position: 'absolute',
                            width: '1px',
                            height: '1px',
                            margin: '-1px',
                            padding: 0,
                            overflow: 'hidden',
                            clip: 'rect(0 0 0 0)',
                            whiteSpace: 'nowrap',
                            border: 0
                        },
                        children: loading || error || !(filtered.length > 0) ? '' : 1 === filtered.length ? _({
                            id: 'modelPicker.count.one',
                            message: '{n} model',
                            values: {
                                n: filtered.length
                            }
                        }) : _({
                            id: 'modelPicker.count.many',
                            message: '{n} models',
                            values: {
                                n: filtered.length
                            }
                        })
                    }),
                    !loading && !error && filtered.length > 0 && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                        children: [
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(List, {
                                id: listboxId,
                                options: filtered,
                                activeIndex: activeIndex,
                                setActiveIndex: setActiveIndex,
                                selectedId: value ?? null,
                                onSelect: choose,
                                tagContext: tagContext,
                                tagVariants: customTagVariants,
                                groupCounts: groupCounts,
                                collapsedGroups: collapsedGroups,
                                onGroupToggle: toggleGroup,
                                hideGroupHeaders: !showGroupHeaders || 'flat' === activeGroupBy,
                                hideTagKinds: rowHideTagKinds,
                                renderRowActions: renderRowActions,
                                renderRowMeta: slots?.optionMeta
                            }),
                            slots?.listFooter?.(slotCtx)
                        ]
                    })
                ]
            })
        ]
    });
});
ModelPicker.displayName = 'ModelPicker';
const GroupBySegmented = ({ value, onChange })=>{
    const { _ } = (0, index_cjs_namespaceObject.useSafeLingui)();
    const groupByOptions = [
        {
            key: "subscription",
            label: _({
                id: 'modelPicker.groupBy.category',
                message: 'Category'
            })
        },
        {
            key: 'vendor',
            label: _({
                id: 'modelPicker.groupBy.provider',
                message: 'Provider'
            })
        }
    ];
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Box_default(), {
        role: "group",
        "aria-label": _({
            id: 'modelPicker.groupBy.ariaLabel',
            message: 'Group models by'
        }),
        sx: {
            display: 'inline-flex',
            backgroundColor: `var(--color-background-secondary, ${apollo_core_namespaceObject.Colors.ColorGray150})`,
            borderRadius: '8px',
            p: '3px',
            gap: '3px'
        },
        children: groupByOptions.map((opt)=>{
            const active = opt.key === value;
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ButtonBase_default(), {
                onClick: ()=>onChange(opt.key),
                "aria-pressed": active,
                sx: {
                    fontSize: 12.5,
                    fontWeight: 600,
                    lineHeight: 1.2,
                    px: 1.25,
                    py: 0.625,
                    borderRadius: '6px',
                    color: active ? `var(--color-primary, ${apollo_core_namespaceObject.Colors.ColorBlue500})` : `var(--color-foreground-de-emp, ${apollo_core_namespaceObject.Colors.ColorGray550})`,
                    backgroundColor: active ? `var(--color-background-raised, ${apollo_core_namespaceObject.Colors.ColorWhite})` : 'transparent',
                    boxShadow: active ? '0 1px 2px rgba(16, 24, 40, 0.14)' : 'none',
                    transition: 'background-color 120ms, color 120ms, box-shadow 120ms',
                    '&:hover': {
                        backgroundColor: active ? `var(--color-background-raised, ${apollo_core_namespaceObject.Colors.ColorWhite})` : "var(--color-background-hover, rgba(82, 96, 105, 0.078))"
                    }
                },
                children: opt.label
            }, opt.key);
        })
    });
};
const UseCustomModelFooter = ({ onActivate, disabled })=>{
    const { _ } = (0, index_cjs_namespaceObject.useSafeLingui)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(ButtonBase_default(), {
        onClick: disabled ? void 0 : onActivate,
        disabled: disabled,
        title: disabled ? _({
            id: 'modelPicker.useCustomModel.disabledHint',
            message: 'Pass onUseCustomModel or a requestContext to the picker to wire this action.'
        }) : void 0,
        sx: {
            width: '100%',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'flex-start',
            gap: 1.25,
            px: 2,
            py: 1.5,
            textAlign: 'left',
            color: `var(--color-primary, ${apollo_core_namespaceObject.Colors.ColorBlue500})`,
            transition: 'background-color 120ms',
            '&:hover': {
                backgroundColor: disabled ? 'transparent' : "var(--color-primary-lighter, rgba(0, 103, 223, 0.06))"
            },
            '&:focus-visible': {
                outline: 'none',
                boxShadow: "inset 0 0 0 2px var(--color-primary-focused, rgba(0, 103, 223, 0.2))"
            },
            '&.Mui-disabled': {
                opacity: 0.55
            }
        },
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Box_default(), {
                sx: {
                    flexShrink: 0,
                    width: 30,
                    height: 30,
                    borderRadius: '8px',
                    display: 'flex',
                    alignItems: 'center',
                    justifyContent: 'center',
                    backgroundColor: "var(--color-primary-lighter, rgba(0, 103, 223, 0.10))",
                    color: `var(--color-primary, ${apollo_core_namespaceObject.Colors.ColorBlue500})`
                },
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Add_default(), {
                    sx: {
                        fontSize: 18
                    }
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(Box_default(), {
                sx: {
                    display: 'flex',
                    flexDirection: 'column',
                    minWidth: 0
                },
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Box_default(), {
                        component: "span",
                        sx: {
                            fontSize: 13.5,
                            fontWeight: 600,
                            lineHeight: 1.3
                        },
                        children: _({
                            id: 'modelPicker.useCustomModel.title',
                            message: 'Use custom model'
                        })
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Box_default(), {
                        component: "span",
                        sx: {
                            fontSize: 12,
                            color: `var(--color-foreground-de-emp, ${apollo_core_namespaceObject.Colors.ColorGray550})`,
                            fontWeight: 400,
                            lineHeight: 1.3
                        },
                        children: _({
                            id: 'modelPicker.useCustomModel.subtitle',
                            message: 'Bring a model from your own connection'
                        })
                    })
                ]
            })
        ]
    });
};
exports.ModelPicker = __webpack_exports__.ModelPicker;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "ModelPicker"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
