"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__, {
    VirtualOptionList: ()=>VirtualOptionList,
    optionDomId: ()=>optionDomId,
    GroupedOptionList: ()=>GroupedOptionList
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const ShieldOutlined_namespaceObject = require("@mui/icons-material/ShieldOutlined");
var ShieldOutlined_default = /*#__PURE__*/ __webpack_require__.n(ShieldOutlined_namespaceObject);
const Box_namespaceObject = require("@mui/material/Box");
var Box_default = /*#__PURE__*/ __webpack_require__.n(Box_namespaceObject);
const react_virtual_namespaceObject = require("@tanstack/react-virtual");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const external_i18n_cjs_namespaceObject = require("../i18n.cjs");
const external_GroupHeader_cjs_namespaceObject = require("./GroupHeader.cjs");
const external_ModelOptionRow_cjs_namespaceObject = require("./ModelOptionRow.cjs");
const BYO_GROUP_KEY = 'byo';
const DEFAULT_LIST_MAX_HEIGHT = 362;
function groupLeadingIcon(groupKey) {
    if (groupKey === BYO_GROUP_KEY) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ShieldOutlined_default(), {
        sx: {
            fontSize: 14
        }
    });
    return null;
}
const GROUP_HINT_DESCRIPTORS = {
    recommended: external_i18n_cjs_namespaceObject.GROUP_LABELS.recommendedHint,
    preview: external_i18n_cjs_namespaceObject.GROUP_LABELS.previewHint,
    byo: external_i18n_cjs_namespaceObject.GROUP_LABELS.byoHint,
    more: external_i18n_cjs_namespaceObject.GROUP_LABELS.moreHint,
    deprecating: external_i18n_cjs_namespaceObject.GROUP_LABELS.deprecatingHint
};
function groupHint(groupKey, i18n) {
    const desc = GROUP_HINT_DESCRIPTORS[groupKey];
    if (!desc) return;
    return i18n ? i18n._(desc) : desc.message;
}
function countLabel(n, i18n) {
    if (null == n || !i18n) return;
    return 1 === n ? i18n._({
        id: 'modelPicker.count.one',
        message: '{n} model',
        values: {
            n
        }
    }) : i18n._({
        id: 'modelPicker.count.many',
        message: '{n} models',
        values: {
            n
        }
    });
}
function optionDomId(listboxId, modelId) {
    const safe = modelId.replace(/[^A-Za-z0-9_-]/g, '-');
    return `${listboxId}-opt-${safe}`;
}
const GroupedOptionList = ({ id, options, activeIndex, setActiveIndex, selectedId, onSelect, tagContext, tagVariants, groupCounts, renderRowActions, renderRowMeta, dense, maxHeight = DEFAULT_LIST_MAX_HEIGHT, hideGroupHeaders, hideTagKinds, collapsedGroups, onGroupToggle, 'aria-label': ariaLabel = 'Models' })=>{
    let lastGroupKey;
    let headerCount = 0;
    const derivedCounts = (0, external_react_namespaceObject.useMemo)(()=>{
        if (groupCounts) return groupCounts;
        const out = {};
        for (const o of options)out[o.groupKey] = (out[o.groupKey] ?? 0) + 1;
        return out;
    }, [
        groupCounts,
        options
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Box_default(), {
        role: "listbox",
        id: id,
        "aria-label": ariaLabel,
        sx: {
            maxHeight,
            overflowY: 'auto',
            py: 0.5
        },
        children: options.map((opt, i)=>{
            const showHeader = !hideGroupHeaders && opt.groupKey !== lastGroupKey;
            const isFirstHeader = showHeader && 0 === headerCount;
            if (showHeader) headerCount += 1;
            lastGroupKey = opt.groupKey;
            const isCollapsible = opt.groupKey === BYO_GROUP_KEY;
            const isCollapsed = isCollapsible && (collapsedGroups?.has(opt.groupKey) ?? false);
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_react_default().Fragment, {
                children: [
                    showHeader && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_GroupHeader_cjs_namespaceObject.GroupHeader, {
                        label: opt.groupLabel,
                        hint: groupHint(opt.groupKey, tagContext?.i18n),
                        count: derivedCounts[opt.groupKey],
                        countLabel: countLabel(derivedCounts[opt.groupKey], tagContext?.i18n),
                        dense: dense,
                        isFirst: isFirstHeader,
                        leadingIcon: groupLeadingIcon(opt.groupKey),
                        collapsible: isCollapsible,
                        collapsed: isCollapsed,
                        onToggle: isCollapsible ? ()=>onGroupToggle?.(opt.groupKey) : void 0
                    }),
                    !isCollapsed && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_ModelOptionRow_cjs_namespaceObject.ModelOptionRow, {
                        id: optionDomId(id, opt.modelId),
                        model: opt,
                        index: i,
                        active: i === activeIndex,
                        selected: opt.modelId === selectedId,
                        onSelect: onSelect,
                        onActivate: setActiveIndex,
                        tagContext: tagContext,
                        tagVariants: tagVariants,
                        hideTagKinds: hideTagKinds,
                        renderMeta: renderRowMeta,
                        renderActions: renderRowActions,
                        dense: dense
                    })
                ]
            }, opt.modelId);
        })
    });
};
const VirtualOptionList = ({ id, options, activeIndex, setActiveIndex, selectedId, onSelect, tagContext, tagVariants, groupCounts, renderRowActions, renderRowMeta, dense, maxHeight = DEFAULT_LIST_MAX_HEIGHT, hideGroupHeaders, hideTagKinds, collapsedGroups, onGroupToggle, 'aria-label': ariaLabel = 'Models' })=>{
    const rows = (0, external_react_namespaceObject.useMemo)(()=>{
        const out = [];
        let lastGroupKey;
        let headerCount = 0;
        const counts = groupCounts ?? {};
        if (!groupCounts) for (const o of options)counts[o.groupKey] = (counts[o.groupKey] ?? 0) + 1;
        options.forEach((opt, i)=>{
            const collapsed = collapsedGroups?.has(opt.groupKey) ?? false;
            if (!hideGroupHeaders && opt.groupKey !== lastGroupKey) {
                out.push({
                    kind: 'header',
                    key: `h-${opt.groupKey}`,
                    groupKey: opt.groupKey,
                    label: opt.groupLabel,
                    isFirst: 0 === headerCount,
                    count: counts[opt.groupKey] ?? 0
                });
                headerCount += 1;
            }
            lastGroupKey = opt.groupKey;
            if (!collapsed) out.push({
                kind: 'option',
                key: opt.modelId,
                model: opt,
                index: i
            });
        });
        return out;
    }, [
        options,
        hideGroupHeaders,
        groupCounts,
        collapsedGroups
    ]);
    const headerHeight = dense ? 32 : 48;
    const optionHeight = dense ? external_ModelOptionRow_cjs_namespaceObject.DENSE_OPTION_HEIGHT : external_ModelOptionRow_cjs_namespaceObject.FULL_OPTION_HEIGHT;
    const parentRef = (0, external_react_namespaceObject.useRef)(null);
    const virtualizer = (0, react_virtual_namespaceObject.useVirtualizer)({
        count: rows.length,
        getScrollElement: ()=>parentRef.current,
        estimateSize: (i)=>rows[i]?.kind === 'header' ? headerHeight : optionHeight,
        overscan: 10
    });
    (0, external_react_namespaceObject.useEffect)(()=>{
        const rowIdx = rows.findIndex((r)=>'option' === r.kind && r.index === activeIndex);
        if (rowIdx >= 0) virtualizer.scrollToIndex(rowIdx, {
            align: 'auto'
        });
    }, [
        activeIndex,
        rows,
        virtualizer
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Box_default(), {
        ref: parentRef,
        role: "listbox",
        id: id,
        "aria-label": ariaLabel,
        sx: {
            maxHeight,
            overflowY: 'auto',
            py: 0.5,
            position: 'relative'
        },
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Box_default(), {
            sx: {
                height: virtualizer.getTotalSize(),
                width: '100%',
                position: 'relative'
            },
            children: virtualizer.getVirtualItems().map((vi)=>{
                const row = rows[vi.index];
                if (!row) return null;
                const baseStyle = {
                    position: 'absolute',
                    top: 0,
                    left: 0,
                    width: '100%',
                    transform: `translateY(${vi.start}px)`
                };
                if ('header' === row.kind) {
                    const isCollapsible = row.groupKey === BYO_GROUP_KEY;
                    const isCollapsed = isCollapsible && (collapsedGroups?.has(row.groupKey) ?? false);
                    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                        style: baseStyle,
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_GroupHeader_cjs_namespaceObject.GroupHeader, {
                            label: row.label,
                            hint: groupHint(row.groupKey, tagContext?.i18n),
                            count: row.count,
                            countLabel: countLabel(row.count, tagContext?.i18n),
                            dense: dense,
                            isFirst: row.isFirst,
                            leadingIcon: groupLeadingIcon(row.groupKey),
                            collapsible: isCollapsible,
                            collapsed: isCollapsed,
                            onToggle: isCollapsible ? ()=>onGroupToggle?.(row.groupKey) : void 0
                        })
                    }, row.key);
                }
                return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    style: baseStyle,
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_ModelOptionRow_cjs_namespaceObject.ModelOptionRow, {
                        id: optionDomId(id, row.model.modelId),
                        model: row.model,
                        index: row.index,
                        active: row.index === activeIndex,
                        selected: row.model.modelId === selectedId,
                        onSelect: onSelect,
                        onActivate: setActiveIndex,
                        tagContext: tagContext,
                        tagVariants: tagVariants,
                        hideTagKinds: hideTagKinds,
                        renderMeta: renderRowMeta,
                        renderActions: renderRowActions,
                        dense: dense
                    })
                }, row.key);
            })
        })
    });
};
exports.GroupedOptionList = __webpack_exports__.GroupedOptionList;
exports.VirtualOptionList = __webpack_exports__.VirtualOptionList;
exports.optionDomId = __webpack_exports__.optionDomId;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "GroupedOptionList",
    "VirtualOptionList",
    "optionDomId"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
